-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce
@theguild/remark-mermaid
(#1092)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
1429dc6
commit 048632e
Showing
22 changed files
with
274 additions
and
201 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@theguild/components": patch | ||
--- | ||
dependencies updates: | ||
- Removed dependency [`[email protected]` ↗︎](https://www.npmjs.com/package/mermaid/v/10.1.0) (from `dependencies`) | ||
- Removed dependency [`[email protected]` ↗︎](https://www.npmjs.com/package/unist-util-visit/v/4.1.2) (from `dependencies`) |
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,6 @@ | ||
--- | ||
'@theguild/remark-mermaid': patch | ||
'@theguild/components': patch | ||
--- | ||
|
||
release `@theguild/remark-mermaid` |
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 |
---|---|---|
|
@@ -11,3 +11,4 @@ packages/og-image/vender/*.wasm | |
.eslintcache | ||
.wrangler/ | ||
.DS_Store | ||
tsup.config.*.mjs |
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 was deleted.
Oops, something went wrong.
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,47 @@ | ||
# `@theguild/remark-mermaid` | ||
|
||
> Created by Dimitri POSTOLOV https://twitter.com/B2o5T. | ||
> | ||
> Maintained by The Guild https://twitter.com/TheGuildDev. | ||
Remark plugin for replacing ```mermaid code blocks with react `<Mermaid />` component. | ||
|
||
- Support MDX2 | ||
|
||
- Support both dark and light themes. Listening for the `dark` class on the `<html />` element and | ||
updating mermaid's theme. | ||
|
||
## Install | ||
|
||
```sh | ||
npm i @theguild/remark-mermaid | ||
``` | ||
|
||
## How to Use | ||
|
||
Follow code will be converted to a mermaid diagram. | ||
|
||
````mdx | ||
// my-page.mdx | ||
|
||
```mermaid | ||
graph TD; | ||
subgraph AA [Consumers] | ||
A[Mobile app]; | ||
B[Web app]; | ||
C[Node.js client]; | ||
end | ||
subgraph BB [Services] | ||
E[REST API]; | ||
F[GraphQL API]; | ||
G[SOAP API]; | ||
end | ||
Z[GraphQL API]; | ||
A --> Z; | ||
B --> Z; | ||
C --> Z; | ||
Z --> E; | ||
Z --> F; | ||
Z --> G; | ||
``` | ||
```` |
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,49 @@ | ||
{ | ||
"name": "@theguild/remark-mermaid", | ||
"version": "0.0.0", | ||
"type": "module", | ||
"description": "Remark plugin for replacing ```mermaid code blocks with react `<Mermaid />` component", | ||
"repository": { | ||
"url": "https://github.com/the-guild-org/docs", | ||
"directory": "packages/remark-mermaid" | ||
}, | ||
"author": "Dimitri POSTOLOV <[email protected]> (https://github.com/B2o5T)", | ||
"license": "MIT", | ||
"main": "./dist/index.js", | ||
"exports": { | ||
"./package.json": "./package.json", | ||
".": "./dist/index.js", | ||
"./*": "./dist/*.js" | ||
}, | ||
"typesVersions": { | ||
"*": { | ||
"mermaid": [ | ||
"./dist/mermaid.d.ts" | ||
] | ||
} | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "tsup", | ||
"dev": "tsup --watch" | ||
}, | ||
"peerDependencies": { | ||
"react": "^18.2.0" | ||
}, | ||
"dependencies": { | ||
"mermaid": "10.2.1", | ||
"unist-util-visit": "4.1.2" | ||
}, | ||
"devDependencies": { | ||
"@types/mdast": "3.0.11", | ||
"@types/mermaid": "9.2.0", | ||
"react": "^18.2.0", | ||
"unified": "10.1.2" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"sideEffects": false | ||
} |
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,42 @@ | ||
import { ReactElement, useEffect, useId, useState } from 'react'; | ||
import mermaid, { MermaidConfig } from 'mermaid'; | ||
|
||
export function Mermaid({ chart }: { chart: string }): ReactElement { | ||
const id = useId(); | ||
const [svg, setSvg] = useState(''); | ||
|
||
useEffect(() => { | ||
const htmlElement = document.documentElement; | ||
const mutationObserver = new MutationObserver(renderChart); | ||
mutationObserver.observe(htmlElement, { attributes: true }); | ||
|
||
// Switching themes taken from https://github.com/mermaid-js/mermaid/blob/1b40f552b20df4ab99a986dd58c9d254b3bfd7bc/packages/mermaid/src/docs/.vitepress/theme/Mermaid.vue#L53 | ||
async function renderChart() { | ||
const hasDarkClass = htmlElement.classList.contains('dark'); | ||
const mermaidConfig: MermaidConfig = { | ||
startOnLoad: false, | ||
securityLevel: 'loose', | ||
fontFamily: 'inherit', | ||
themeCSS: 'margin: 1.5rem auto 0;', | ||
theme: hasDarkClass ? 'dark' : 'default', | ||
}; | ||
|
||
try { | ||
mermaid.initialize(mermaidConfig); | ||
const { svg } = await mermaid.render( | ||
// strip invalid characters for `id` attribute | ||
id.replaceAll(':', ''), | ||
chart, | ||
); | ||
setSvg(svg); | ||
} catch (error) { | ||
// eslint-disable-next-line no-console -- show error | ||
console.error('Error while rendering mermaid', error); | ||
} | ||
} | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps -- only on mount | ||
}, []); | ||
|
||
return <div dangerouslySetInnerHTML={{ __html: svg }} />; | ||
} |
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,15 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es2021", | ||
"module": "ESNext", | ||
"declaration": true, | ||
"noEmit": true, | ||
"esModuleInterop": true, | ||
"strict": true, | ||
"skipLibCheck": true, | ||
"allowJs": true, | ||
"jsx": "react-jsx", | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true | ||
} | ||
} |
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,18 @@ | ||
import { defineConfig, Options } from 'tsup'; | ||
import tsconfig from './tsconfig.json'; | ||
|
||
export default defineConfig([ | ||
{ | ||
format: 'esm', | ||
entry: ['src/**/*.{ts,tsx}'], | ||
target: tsconfig.compilerOptions.target as Options['target'], | ||
bundle: false, | ||
}, | ||
{ | ||
name: 'dts', | ||
entry: ['src/**/*.{ts,tsx}'], | ||
dts: { | ||
only: true, | ||
}, | ||
}, | ||
]); |
Oops, something went wrong.
048632e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
the-guild-components-storybook – ./
the-guild-components-storybook.vercel.app
the-guild-components-storybook-theguild.vercel.app
the-guild-components-storybook-git-main-theguild.vercel.app