mirror of
https://github.com/reonokiy/blog.nokiy.net.git
synced 2025-06-16 11:41:17 +02:00
test: add plugins
This commit is contained in:
parent
4341dab741
commit
9e9cdcb206
8 changed files with 257 additions and 4 deletions
50
src/plugins/rehype-component-admonition.ts
Normal file
50
src/plugins/rehype-component-admonition.ts
Normal file
|
@ -0,0 +1,50 @@
|
|||
import type { Element, Properties as HastProperties, Node } from 'hast'
|
||||
/// <reference types="mdast" />
|
||||
import { h } from 'hastscript'
|
||||
|
||||
interface AdmonitionProperties extends HastProperties {
|
||||
'title'?: string
|
||||
'has-directive-label'?: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an admonition component.
|
||||
*
|
||||
* @param properties - The properties of the component.
|
||||
* @param type - The admonition type.
|
||||
* @param children - The children elements of the component.
|
||||
* @returns The created admonition component as a Hast Element.
|
||||
*/
|
||||
export function AdmonitionComponent(
|
||||
properties: AdmonitionProperties,
|
||||
type: 'tip' | 'note' | 'important' | 'caution' | 'warning',
|
||||
children: Node[],
|
||||
): Element {
|
||||
if (!Array.isArray(children) || children.length === 0) {
|
||||
return h(
|
||||
'div',
|
||||
{ class: 'hidden' },
|
||||
'Invalid admonition directive. (Admonition directives must be of block type ":::note{name="name"} <content> :::")',
|
||||
)
|
||||
}
|
||||
|
||||
let label: Element | string | null = null
|
||||
|
||||
if (properties && properties['has-directive-label']) {
|
||||
const [firstChild, ...restChildren] = children
|
||||
children = restChildren
|
||||
|
||||
if (firstChild && firstChild.type === 'element') {
|
||||
label = firstChild as Element
|
||||
label.tagName = 'div' // Change the tag <p> to <div>
|
||||
}
|
||||
else {
|
||||
label = ''
|
||||
}
|
||||
}
|
||||
|
||||
return h('blockquote', { class: `admonition bdm-${type}` }, [
|
||||
h('span', { class: 'bdm-title' }, label || type.toUpperCase()),
|
||||
...(children as Element[]),
|
||||
] as Element[])
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue