Skip to content

Commit

Permalink
Merge pull request #17 from ZakarFin/mermaid-support
Browse files Browse the repository at this point in the history
Initial support for mermaid.js in markdown
  • Loading branch information
testomuli authored Dec 20, 2023
2 parents 210d759 + 7cbb6d7 commit cc30922
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
3 changes: 2 additions & 1 deletion contentlayer.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export default makeSource({
documentTypes: [Docs, Posts, Coordinators, FaqDevelopers, FaqUsers],
markdown: {
remarkPlugins: [remarkGfm],
rehypePlugins: [rehypeHighlight],
// ignore mermaid for highlighting (```mermaid ... ```)
rehypePlugins: [[rehypeHighlight, { plainText: ['mermaid'] }]],
},
})
18 changes: 18 additions & 0 deletions lib/customMarked.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { marked } from 'marked'

marked.use({
gfm: true,
})

export const MERMAID_SNIPPET = '<pre class="mermaid">'


const renderer = new marked.Renderer();
renderer.code = function (code, language) {
if (language === 'mermaid') {
return MERMAID_SNIPPET + code + '</pre>';
} else {
return '<pre><code>' + code + '</code></pre>';
}
};
export const mdToHtml = (src: string) => marked(src, { renderer: renderer })
20 changes: 14 additions & 6 deletions lib/utils.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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,
Expand Down Expand Up @@ -103,6 +99,18 @@ export function generateVersionDocs(
html += compiledHTML
}
}
// inject script to make mermaid js work its magic
if (html.includes(MERMAID_SNIPPET)) {
html += `<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mermaid.min.js"></script>
<script>
mermaid.initialize({
startOnLoad:true,
htmlLabels:true,
theme: 'base'
});
</script>
`
}

topics.push({
title: topic,
Expand Down
2 changes: 1 addition & 1 deletion styles/_md-styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
content: '';
}

& pre {
& pre:not(.mermaid) {
color: var(--color-light);
background-color: rgba(0, 0, 0, 0.75);
overflow-x: auto;
Expand Down

0 comments on commit cc30922

Please sign in to comment.