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
1 parent
1b4f0b4
commit f1f17ae
Showing
3 changed files
with
77 additions
and
2 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,49 @@ | ||
// TODO: Proper TypeScript rehaul for this file. | ||
|
||
/** | ||
* Some bits of code are from https://github.com/sjwall/mdx-mermaid | ||
* | ||
* Using Parcel to bundle this plugin because | ||
* Astro uses MJS for its config file. | ||
* (It seems that it support TS too, but it breaks some third-parties) | ||
* | ||
*/ | ||
|
||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import { visit } from 'unist-util-visit'; | ||
import renderDiagram from './render-diagram'; | ||
|
||
const style = `display: flex; justify-content: center; width: 100%;`; | ||
|
||
function plugin() { | ||
return async function transformer(ast) { | ||
// Find all the mermaid diagram code blocks. i.e. ```mermaid | ||
const instances = []; | ||
visit(ast, { type: 'code', lang: 'mermaid' }, (node, index, parent) => { | ||
instances.push([node, index, parent]); | ||
}); | ||
// Replace each Mermaid code block with the server-side rendered SVG | ||
await Promise.all( | ||
instances.map(async ([node, index, parent]) => { | ||
// MDX rendering seems to be already cached. | ||
// or this keep running puppeeter ? | ||
// Also, disabling it prevent a bug which doesn't | ||
// occur in the regular component. | ||
const html = await renderDiagram({ | ||
config: {}, | ||
code: node.value, | ||
}).then((diagram) => diagram.result); | ||
parent.children.splice(index, 1, { | ||
type: 'html', | ||
// TODO: put CSS elsewhere | ||
value: `<div style="${style}">${html}</div>`, | ||
position: node.position, | ||
}); | ||
}), | ||
); | ||
|
||
return ast; | ||
}; | ||
} | ||
|
||
export default plugin; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.