-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support mermaid code blocks in Markdown (#7490)
Co-authored-by: Joshua Chen <[email protected]> Co-authored-by: sebastienlorber <[email protected]> # Conflicts: # project-words.txt # website/package.json # yarn.lock
- Loading branch information
Showing
39 changed files
with
4,812 additions
and
2,939 deletions.
There are no files selected for viewing
This file contains 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 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
51 changes: 51 additions & 0 deletions
51
packages/docusaurus-mdx-loader/src/remark/mermaid/__tests__/__snapshots__/index.test.ts.snap
This file contains 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,51 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`mermaid remark plugin does nothing if there's no mermaid code block 1`] = ` | ||
" | ||
const layoutProps = { | ||
}; | ||
const MDXLayout = "wrapper" | ||
export default function MDXContent({ | ||
components, | ||
...props | ||
}) { | ||
return <MDXLayout {...layoutProps} {...props} components={components} mdxType="MDXLayout"> | ||
<h1>{\`Heading 1\`}</h1> | ||
<p>{\`No Mermaid diagram :(\`}</p> | ||
<pre><code parentName="pre" {...{ | ||
"className": "language-js" | ||
}}>{\`this is not mermaid | ||
\`}</code></pre> | ||
</MDXLayout>; | ||
} | ||
; | ||
MDXContent.isMDXComponent = true;" | ||
`; | ||
exports[`mermaid remark plugin works for basic mermaid code blocks 1`] = ` | ||
" | ||
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;/n A-->B;/n A-->C;/n B-->D;/n C-->D;" | ||
}}></mermaid> | ||
</MDXLayout>; | ||
} | ||
; | ||
MDXContent.isMDXComponent = true;" | ||
`; |
46 changes: 46 additions & 0 deletions
46
packages/docusaurus-mdx-loader/src/remark/mermaid/__tests__/index.test.ts
This file contains 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,46 @@ | ||
/** | ||
* 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("does nothing if there's no mermaid code block", async () => { | ||
const mdxCompiler = createTestCompiler(); | ||
const result = await mdxCompiler.process( | ||
`# Heading 1 | ||
No Mermaid diagram :( | ||
\`\`\`js | ||
this is not mermaid | ||
\`\`\` | ||
`, | ||
); | ||
expect(result.contents).toMatchSnapshot(); | ||
}); | ||
|
||
it('works for basic mermaid code blocks', async () => { | ||
const mdxCompiler = createTestCompiler(); | ||
const result = await mdxCompiler.process(`# Heading 1 | ||
\`\`\`mermaid | ||
graph TD; | ||
A-->B; | ||
A-->C; | ||
B-->D; | ||
C-->D; | ||
\`\`\``); | ||
expect(result.contents).toMatchSnapshot(); | ||
}); | ||
}); |
32 changes: 32 additions & 0 deletions
32
packages/docusaurus-mdx-loader/src/remark/mermaid/index.ts
This file contains 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,32 @@ | ||
/** | ||
* 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 {Code} from 'mdast'; | ||
|
||
// TODO: this plugin shouldn't be in the core MDX loader | ||
// After we allow plugins to provide Remark/Rehype plugins (see | ||
// https://github.com/facebook/docusaurus/issues/6370), this should be provided | ||
// by theme-mermaid itself | ||
export default function plugin(): Transformer { | ||
return (root) => { | ||
visit(root, 'code', (node: Code, index, parent) => { | ||
if (node.lang === 'mermaid') { | ||
parent!.children.splice(index, 1, { | ||
type: 'mermaidCodeBlock', | ||
data: { | ||
hName: 'mermaid', | ||
hProperties: { | ||
value: node.value, | ||
}, | ||
}, | ||
}); | ||
} | ||
}); | ||
}; | ||
} |
This file contains 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 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 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 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 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 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,16 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
// This component is meant to be implemented by a Mermaid renderer theme | ||
// It is notable created to be overridden by docusaurus-theme-mermaid | ||
|
||
// By default, the classic theme does not provide any Mermaid implementation | ||
// Yet we declare it there so that we can register it in MDX | ||
// TODO later the mermaid theme should be able to register its MDX component | ||
// see https://github.com/facebook/docusaurus/pull/7490#issuecomment-1279117288 | ||
|
||
export {default} from '@docusaurus/Noop'; |
This file contains 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 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 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,57 @@ | ||
{ | ||
"name": "@docusaurus/theme-mermaid", | ||
"version": "2.1.0", | ||
"description": "Mermaid components for Docusaurus.", | ||
"main": "lib/index.js", | ||
"types": "src/theme-mermaid.d.ts", | ||
"sideEffects": false, | ||
"exports": { | ||
"./lib/*": "./lib/*", | ||
"./src/*": "./src/*", | ||
"./client": { | ||
"type": "./lib/client/index.d.ts", | ||
"default": "./lib/client/index.js" | ||
}, | ||
".": { | ||
"types": "./src/theme-mermaid.d.ts", | ||
"default": "./lib/index.js" | ||
} | ||
}, | ||
"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.1.0", | ||
"@docusaurus/module-type-aliases": "2.1.0", | ||
"@docusaurus/theme-common": "2.1.0", | ||
"@docusaurus/types": "^2.1.0", | ||
"@docusaurus/utils-validation": "2.1.0", | ||
"@mdx-js/react": "^1.6.22", | ||
"mermaid": "^9.1.1", | ||
"tslib": "^2.4.0" | ||
}, | ||
"devDependencies": { | ||
"@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": ">=16.14" | ||
} | ||
} |
Oops, something went wrong.