-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
feat: support mermaid code blocks in Markdown #7490
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
54ee20f
feat: add mermaid diagram support ootb #1258
sjwall a003697
feat(mdx-loader): mermaid rendering optional
sjwall ed1f044
refactor: move mermaid components to own package
sjwall ed1f21c
refactor: remove theme-classic mermaid dependency
sjwall 777c086
refactor(mdx-loader): markdown config option
sjwall 3a32f60
refactor(theme-mermaid): remove exports
sjwall 11836a0
little refactors
Josh-Cena 5d836f5
Merge branch 'main' into mermaid
Josh-Cena 8c9e005
remove unused
Josh-Cena 722f75b
remark plugin refactor
Josh-Cena c0a17ac
Merge branch 'main' into mermaid
Josh-Cena 0a5e372
greatly simplify
Josh-Cena 73f53a8
simplify API
Josh-Cena ec6f920
fix tests
Josh-Cena 47bec9b
move dogfooding
Josh-Cena be53cf3
Merge branch 'main' into mermaid
sjwall addacc6
Merge branch 'main' into mermaid
Josh-Cena 51b6c48
Merge branch 'main' into mermaid
slorber 5ec7c0b
mermaid.mermaidOptions => mermaid.options
slorber 25cb8aa
validate config.markdown.mermaid
slorber 56ca9c7
do not spread markdown config to mdx loader: be more explicit with at…
slorber 9772c2e
typo
slorber 5d49364
temp better mermaid integration
slorber 062ed20
expose mermaid hooks as client apis instead of themes
slorber 4da56ed
fix snapshots
slorber b6c408d
good mermaid integration
slorber d5ca004
increase random count
slorber 76c8703
add max-width 100% to container/svg
slorber c8d1e2c
try to fix journey bug
slorber bd67df1
fix bad project-words merge
slorber 8c88c55
refactor MDXComponents usage
slorber 57d481c
add mermaid in tabs dogfood, see https://github.com/sjwall/mdx-mermai…
slorber 1377f4a
````mdx-code-block
slorber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
62 changes: 62 additions & 0 deletions
62
packages/docusaurus-mdx-loader/src/remark/mermaid/__tests__/index.test.ts
This file contains hidden or 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,62 @@ | ||
| /** | ||
| * Copyright (c) Facebook, Inc. and its affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| import {createCompiler} from '@mdx-js/mdx'; | ||
| import mermaid from '..'; | ||
|
|
||
| describe('mermaid remark plugin', () => { | ||
| function createTestCompiler() { | ||
| return createCompiler({ | ||
| remarkPlugins: [mermaid], | ||
| }); | ||
| } | ||
|
|
||
| it('no mermaid', async () => { | ||
| const mdxCompiler = createTestCompiler(); | ||
| const result = await mdxCompiler.process( | ||
| '# Heading 1\n\nNo Mermaid diagram :(', | ||
| ); | ||
| expect(result.contents).toBe( | ||
| '\n\n\nconst layoutProps = {\n \n};\nconst MDXLayout = "wrapper"\nexport default function MDXContent({\n components,\n ...props\n}) {\n return <MDXLayout {...layoutProps} {...props} components={components} mdxType="MDXLayout">\n <h1>{`Heading 1`}</h1>\n <p>{`No Mermaid diagram :(`}</p>\n </MDXLayout>;\n}\n\n;\nMDXContent.isMDXComponent = true;', | ||
| ); | ||
| }); | ||
|
|
||
| it('basic', async () => { | ||
| const mdxCompiler = createTestCompiler(); | ||
| const result = await mdxCompiler.process(`# Heading 1\n | ||
| \`\`\`mermaid | ||
| graph TD; | ||
| A-->B; | ||
| A-->C; | ||
| B-->D; | ||
| C-->D; | ||
| \`\`\``); | ||
| expect(result.contents).toBe(` | ||
|
|
||
|
|
||
| const layoutProps = { | ||
| ${' '} | ||
| }; | ||
| const MDXLayout = "wrapper" | ||
| export default function MDXContent({ | ||
| components, | ||
| ...props | ||
| }) { | ||
| return <MDXLayout {...layoutProps} {...props} components={components} mdxType="MDXLayout"> | ||
| <h1>{\`Heading 1\`}</h1> | ||
| <mermaid value={\`graph TD; | ||
| A-->B; | ||
| A-->C; | ||
| B-->D; | ||
| C-->D;\`} /> | ||
| </MDXLayout>; | ||
| } | ||
|
|
||
| ; | ||
| MDXContent.isMDXComponent = true;`); | ||
| }); | ||
| }); |
48 changes: 48 additions & 0 deletions
48
packages/docusaurus-mdx-loader/src/remark/mermaid/index.ts
This file contains hidden or 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,48 @@ | ||
| /** | ||
| * Copyright (c) Facebook, Inc. and its affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| import visit from 'unist-util-visit'; | ||
| import type {Transformer} from 'unified'; | ||
| import type {Data, Literal, Node, Parent} from 'unist'; | ||
|
|
||
| type CodeMermaid = Literal<string> & { | ||
| type: 'code'; | ||
| lang: 'mermaid'; | ||
| }; | ||
|
|
||
| function processMermaidNode( | ||
| node: CodeMermaid, | ||
| index: number, | ||
| parent: Parent<Node<Data> | Literal, Data>, | ||
| ) { | ||
| parent.children.splice(index, 1, { | ||
| type: 'jsx', | ||
| value: `<mermaid value={\`${node.value}\`}/>`, | ||
| position: node.position, | ||
| }); | ||
| } | ||
|
|
||
| export default function plugin(): Transformer { | ||
| return async (root) => { | ||
| // Find all the mermaid diagram code blocks. i.e. ```mermaid | ||
| const instances: [CodeMermaid, number, Parent<Node<Data>, Data>][] = []; | ||
| visit( | ||
| root, | ||
| {type: 'code', lang: 'mermaid'}, | ||
| (node: CodeMermaid, index, parent) => { | ||
| if (parent) { | ||
| instances.push([node, index, parent]); | ||
| } | ||
| }, | ||
| ); | ||
|
|
||
| // Replace each Mermaid code block with the Mermaid component | ||
| instances.forEach(([node, index, parent]) => { | ||
| processMermaidNode(node, index, parent); | ||
| }); | ||
| }; | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,3 @@ | ||
| .tsbuildinfo* | ||
| tsconfig* | ||
| __tests__ |
This file contains hidden or 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,21 @@ | ||
| # Docusaurus Theme Mermaid | ||
|
|
||
| The mermaid components for Docusaurus. | ||
|
|
||
| ## Installation | ||
|
|
||
| Add `docusaurus/theme-mermaid` to your package: | ||
|
|
||
| ```bash | ||
| npm i @docusaurus/theme-mermaid | ||
| # or | ||
| yarn add @docusaurus/theme-mermaid | ||
| ``` | ||
|
|
||
| ## Swizzling components | ||
|
|
||
| ```bash | ||
| $ npm swizzle @docusaurus/theme-mermaid [component name] | ||
| ``` | ||
|
|
||
| All components used by this theme can be found [here](https://github.com/facebook/docusaurus/tree/main/packages/docusaurus-theme-mermaid/src/theme) |
This file contains hidden or 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,53 @@ | ||
| { | ||
| "name": "@docusaurus/theme-mermaid", | ||
| "version": "2.0.0-beta.20", | ||
| "description": "Mermaid components for Docusaurus.", | ||
| "main": "lib/index.js", | ||
| "exports": { | ||
| "./theme/MDXComponents/Mermaid": "./lib/theme/MDXComponents/Mermaid.js", | ||
| "./theme/useMermaid": "./lib/theme/useMermaid.js", | ||
| "./theme/Mermaid": "./lib/theme/Mermaid/index.js", | ||
Josh-Cena marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ".": "./lib/index.js" | ||
| }, | ||
| "types": "src/theme-mermaid.d.ts", | ||
| "publishConfig": { | ||
| "access": "public" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/facebook/docusaurus.git", | ||
| "directory": "packages/docusaurus-theme-mermaid" | ||
| }, | ||
| "license": "MIT", | ||
| "scripts": { | ||
| "build": "tsc --build && node ../../admin/scripts/copyUntypedFiles.js && prettier --config ../../.prettierrc --write \"lib/theme/**/*.js\"", | ||
| "watch": "run-p -c copy:watch build:watch", | ||
| "build:watch": "tsc --build --watch", | ||
| "copy:watch": "node ../../admin/scripts/copyUntypedFiles.js --watch" | ||
| }, | ||
| "dependencies": { | ||
| "@docusaurus/core": "2.0.0-beta.20", | ||
| "@docusaurus/theme-common": "2.0.0-beta.20", | ||
| "@docusaurus/theme-translations": "2.0.0-beta.20", | ||
| "@docusaurus/utils": "2.0.0-beta.20", | ||
| "@docusaurus/utils-common": "2.0.0-beta.20", | ||
| "@docusaurus/utils-validation": "2.0.0-beta.20", | ||
| "@mdx-js/react": "^1.6.22", | ||
| "mermaid": "^9.1.1", | ||
| "tslib": "^2.4.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@docusaurus/module-type-aliases": "2.0.0-beta.20", | ||
| "@docusaurus/types": "2.0.0-beta.20", | ||
| "@types/mdx-js__react": "^1.5.5", | ||
| "@types/mermaid": "^8.2.9", | ||
| "react-test-renderer": "^17.0.2" | ||
| }, | ||
| "peerDependencies": { | ||
| "react": "^16.8.4 || ^17.0.0", | ||
| "react-dom": "^16.8.4 || ^17.0.0" | ||
| }, | ||
| "engines": { | ||
| "node": ">=14" | ||
| } | ||
| } | ||
71 changes: 71 additions & 0 deletions
71
packages/docusaurus-theme-mermaid/src/__tests__/validateThemeConfig.test.ts
This file contains hidden or 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,71 @@ | ||
| /** | ||
| * Copyright (c) Facebook, Inc. and its affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| import {validateThemeConfig} from '../validateThemeConfig'; | ||
| import type {Joi} from '@docusaurus/utils-validation'; | ||
|
|
||
| function testValidateThemeConfig(themeConfig: {[key: string]: unknown}) { | ||
| function validate( | ||
| schema: Joi.ObjectSchema<{[key: string]: unknown}>, | ||
| cfg: {[key: string]: unknown}, | ||
| ) { | ||
| const {value, error} = schema.validate(cfg, { | ||
| convert: false, | ||
| }); | ||
| if (error) { | ||
| throw error; | ||
| } | ||
| return value; | ||
| } | ||
|
|
||
| return validateThemeConfig({validate, themeConfig}); | ||
| } | ||
|
|
||
| describe('validateThemeConfig', () => { | ||
| it('undefined config', () => { | ||
| const mermaid = undefined; | ||
| expect(testValidateThemeConfig({mermaid})).toEqual({}); | ||
| }); | ||
|
|
||
| it('nonexistent config', () => { | ||
| expect(testValidateThemeConfig({})).toEqual({}); | ||
| }); | ||
|
|
||
| it('empty config', () => { | ||
| const mermaid = {}; | ||
| expect(testValidateThemeConfig({mermaid})).toEqual({ | ||
| mermaid: {}, | ||
| }); | ||
| }); | ||
|
|
||
| it('theme', () => { | ||
| const mermaid = { | ||
| theme: { | ||
| light: 'light', | ||
| dark: 'dark', | ||
| }, | ||
| }; | ||
| expect(testValidateThemeConfig({mermaid})).toEqual({ | ||
| mermaid: { | ||
| ...mermaid, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('config', () => { | ||
| const mermaid = { | ||
| config: { | ||
| fontFamily: 'Ariel', | ||
| }, | ||
| }; | ||
| expect(testValidateThemeConfig({mermaid})).toEqual({ | ||
| mermaid: { | ||
| ...mermaid, | ||
| }, | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.