Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial support for mermaid.js in markdown #17

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion contentlayer.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'] }]],
},
})
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/mermaid@10.6.1/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