Skip to content

Commit

Permalink
Merge pull request #3807 from aloisklink/docs/update-code-highlightin…
Browse files Browse the repository at this point in the history
…g-theme

Use `github-dark` to highlight fence blocks in vitepress docs
  • Loading branch information
sidharthv96 committed Nov 18, 2022
2 parents 804a235 + c30aa6f commit 09ed41b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
1 change: 0 additions & 1 deletion packages/mermaid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
"prettier": "^2.7.1",
"remark": "^14.0.2",
"rimraf": "^3.0.2",
"shiki": "^0.11.1",
"start-server-and-test": "^1.14.0",
"typedoc": "^0.23.18",
"typedoc-plugin-markdown": "^3.13.6",
Expand Down
2 changes: 2 additions & 0 deletions packages/mermaid/src/docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { MermaidMarkdown } from 'vitepress-plugin-mermaid';
import { defineConfig, MarkdownOptions } from 'vitepress';

const allMarkdownTransformers: MarkdownOptions = {
// the shiki theme to highlight code blocks
theme: 'github-dark',
config: async (md) => {
await MermaidExample(md);
MermaidMarkdown(md);
Expand Down
34 changes: 17 additions & 17 deletions packages/mermaid/src/docs/.vitepress/mermaid-markdown-all.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
import type { MarkdownRenderer } from 'vitepress';

// Note: using "import shiki from 'shiki' and then "const highlighter = await shiki.getHighlighter(...) does not work 2022-11-15
import { getHighlighter } from 'shiki';

const MermaidExample = async (md: MarkdownRenderer) => {
const defaultRenderer = md.renderer.rules.fence;

if (!defaultRenderer) {
throw new Error('defaultRenderer is undefined');
}

const highlighter = await getHighlighter({
theme: 'material-palenight',
langs: ['mermaid'],
});

md.renderer.rules.fence = (tokens, index, options, env, slf) => {
const token = tokens[index];

if (token.info.trim() === 'mermaid-example') {
const highlight = highlighter
.codeToHtml(token.content, { lang: 'mermaid' })
.replace(/<span/g, '<span v-pre')
.replace('#2e3440ff', 'transparent')
.replace('#292D3E', 'transparent');

if (!md.options.highlight) {
// this function is always created by vitepress, but we need to check it
// anyway to make TypeScript happy
throw new Error(
'Missing MarkdownIt highlight function (should be automatically created by vitepress'
);
}

// doing ```mermaid-example {line-numbers=5 highlight=14-17} is not supported
const langAttrs = '';
return `<h5>Code:</h5>
<div class="language-mermaid">
<button class="copy"></button>
<span class="lang">mermaid</span>
${highlight}
<button class="copy"></button>
<span class="lang">mermaid</span>
${
// html is pre-escaped by the highlight function
// (it also adds `v-pre` to ignore Vue template syntax)
md.options.highlight(token.content, 'mermaid', langAttrs)
}
</div>
<h5>Diagram:</h5>`;
}
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 09ed41b

Please sign in to comment.