-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
1,907 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { unified } from "unified"; | ||
import rehypeParse from "rehype-parse"; | ||
|
||
const parser = unified().use(rehypeParse, { fragment: true }); | ||
|
||
export { parser }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import type { Plugin } from "unified"; | ||
import type { Text, Element, Parent } from "hast"; | ||
import { visit } from "unist-util-visit"; | ||
import { renderMermaid } from "@mermaid-js/mermaid-cli"; | ||
import puppeteer from "puppeteer"; | ||
import { parser } from "./parser"; | ||
|
||
interface MermaidCodeBlock { | ||
textNode: Text; | ||
index: number; | ||
parent: Parent; | ||
} | ||
|
||
const checkIsMermaid = (lang: string): boolean => lang === "mermaid"; | ||
|
||
const rehypeMermaid: Plugin = () => { | ||
const parseLanguage = (classNames: string[]): string => { | ||
for (const className of classNames) { | ||
if (className.startsWith("language-")) { | ||
return className.replace("language-", ""); | ||
} | ||
} | ||
return ""; | ||
}; | ||
|
||
return async (node) => { | ||
const mermaidCodeBlocks: MermaidCodeBlock[] = []; | ||
|
||
visit(node, "element", (node: Element, index, parent) => { | ||
// Check if the node is a pre tag with a single child | ||
if (!(node.tagName === "pre" && Array.isArray(node.children) && node.children.length === 1)) { | ||
return; | ||
} | ||
|
||
// Check if the child is a code tag | ||
const codeElem = node.children[0] as Element; | ||
if (!(codeElem !== null && typeof codeElem === "object" && codeElem.tagName === "code")) { | ||
return; | ||
} | ||
|
||
// Check if the code tag has a className property | ||
const classNames = codeElem.properties?.className; | ||
if ( | ||
!( | ||
Array.isArray(classNames) && | ||
classNames.length > 0 && | ||
classNames.every((className) => typeof className === "string") | ||
) | ||
) { | ||
return; | ||
} | ||
|
||
// Check if the code tag has a text child | ||
const textNode = codeElem.children[0] as Text; | ||
if (typeof textNode.value !== "string") { | ||
return; | ||
} | ||
|
||
// Parse the language from the class names and check if it is supported | ||
const lang = parseLanguage(classNames); | ||
const isMermaid = checkIsMermaid(lang); | ||
|
||
if (isMermaid && index !== null && parent !== null) { | ||
mermaidCodeBlocks.push({ textNode, index, parent }); | ||
} | ||
}); | ||
|
||
if (mermaidCodeBlocks.length === 0) { | ||
return; | ||
} | ||
|
||
const browser = await puppeteer.launch({ | ||
headless: true, | ||
}); | ||
|
||
const decoder = new TextDecoder(); | ||
|
||
await Promise.all( | ||
mermaidCodeBlocks.map(async ({ textNode, index, parent }, blockIndex) => { | ||
const { data: svgBuffer } = await renderMermaid(browser, textNode.value, "svg"); | ||
|
||
const svgElem = decoder.decode(svgBuffer).replaceAll("my-svg", `my-svg-${blockIndex}`); | ||
const parsedRoot = parser.parse(svgElem); | ||
|
||
const content = parsedRoot.children[0] as Element; | ||
content.properties.style = "width: 100%; background-color: white;"; | ||
|
||
parent.children[index] = content; | ||
}), | ||
); | ||
|
||
await browser.close(); | ||
}; | ||
}; | ||
|
||
export default rehypeMermaid; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.