diff --git a/contentlayer.config.ts b/contentlayer.config.ts index ff82bf8..a0d4b95 100644 --- a/contentlayer.config.ts +++ b/contentlayer.config.ts @@ -157,6 +157,7 @@ export default makeSource({ documentTypes: [Docs, Posts, Coordinators, FaqDevelopers, FaqUsers], markdown: { remarkPlugins: [remarkGfm], - rehypePlugins: [rehypeHighlight], + // ignore mermaid for highlighting (```mermaid ... ```) + rehypePlugins: [[rehypeHighlight, { plainText: ['mermaid'] }]], }, }) diff --git a/lib/customMarked.ts b/lib/customMarked.ts new file mode 100644 index 0000000..53e2f0e --- /dev/null +++ b/lib/customMarked.ts @@ -0,0 +1,18 @@ +import { marked } from 'marked' + +marked.use({ + gfm: true, + }) + +export const MERMAID_SNIPPET = '
'
+
+
+const renderer = new marked.Renderer();
+renderer.code = function (code, language) {
+  if (language === 'mermaid') {
+    return MERMAID_SNIPPET + code + '
'; + } else { + return '
' + code + '
'; + } +}; +export const mdToHtml = (src: string) => marked(src, { renderer: renderer }) \ No newline at end of file diff --git a/lib/utils.ts b/lib/utils.ts index ab215a3..d64b27f 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -1,14 +1,10 @@ import fs from 'fs' import path from 'path' import matter from 'gray-matter' -import { marked } from 'marked' +import { mdToHtml, MERMAID_SNIPPET } from './customMarked' import { insertIdsToHeaders } from './markdownToHtml' import { DocAnchorLinksType, VersionDocType } from '@/types/types' -marked.use({ - gfm: true, -}) - export function slugify(input: string): string { return input .toLowerCase() @@ -24,7 +20,7 @@ function compileMarkdownToHTML(markdown: string): { anchorLinks: VersionDocType['anchorLinks'] } { const { content } = matter(markdown) - const markedHtml = marked(content) + const markedHtml = mdToHtml(content) const { html, anchorLinks } = insertIdsToHeaders(markedHtml) return { html, @@ -103,6 +99,18 @@ export function generateVersionDocs( html += compiledHTML } } + // inject script to make mermaid js work its magic + if (html.includes(MERMAID_SNIPPET)) { + html += ` + + ` + } topics.push({ title: topic, diff --git a/styles/_md-styles.scss b/styles/_md-styles.scss index e103467..a993a21 100644 --- a/styles/_md-styles.scss +++ b/styles/_md-styles.scss @@ -155,7 +155,7 @@ content: ''; } - & pre { + & pre:not(.mermaid) { color: var(--color-light); background-color: rgba(0, 0, 0, 0.75); overflow-x: auto;