web / frontend /src /lib /remarkSource.ts
Chandima Prabhath
init
22b1735
raw
history blame contribute delete
619 Bytes
import { visit } from 'unist-util-visit'
import type { Plugin } from 'unified'
import type { Root, HTML } from 'mdast'
const remarkSource: Plugin = () => (tree: Root) => {
visit(tree, 'html', (node: HTML, index, parent) => {
if (node.value.startsWith('<source')) {
console.debug('[remarkSource] found source tag:', node.value)
}
const match = node.value.match(/<source\s+path=['"](.+?)['"]\s*\/>/)
if (!match || !parent) return
const [, path] = match
parent.children.splice(index, 1, {
type: 'source',
path,
data: {},
} as any)
})
}
export default remarkSource