learn-flutter/add-mermaid-classname.ts
ChangJoo Park(박창주) 0702fad5a6 Update flutter
2025-05-14 10:29:56 +09:00

30 lines
949 B
TypeScript

import { visit, CONTINUE } from "unist-util-visit"
import type { Plugin } from 'unified';
import type { Root, Element } from 'hast';
const visitor = (node: any) => {
const dataLanguageMermaid = "mermaid"
const typeElement = "element"
const tagNamePre = "pre"
const classMermaid = dataLanguageMermaid
const isPreElement = (node: any) => typeof node.type !== undefined && node.type === typeElement
&& node.tagName !== undefined && node.tagName === tagNamePre
&& node.properties !== undefined && node.properties.dataLanguage === dataLanguageMermaid
if(!isPreElement(node)) {
return CONTINUE
}
const element = node as Element
const properties = element.properties
const className = properties.className as Array<string>
properties.className = [...className, classMermaid]
return CONTINUE
}
const addMermaidClass: Plugin<void[], Root> = () =>
(ast: Root) => visit(ast, visitor)
export default addMermaidClass