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

feat: add mermaid surport #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ dist/

# dependencies
node_modules/

pnpm-lock.yaml
# logs
npm-debug.log*
yarn-debug.log*
Expand Down
10 changes: 9 additions & 1 deletion astro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { site } from './src/config.json'
import remarkMath from 'remark-math'
import rehypeKatex from 'rehype-katex'
import swup from '@swup/astro'
import { mermaid } from './src/plugins/remarkMermaidPlugin'

// https://astro.build/config
export default defineConfig({
Expand All @@ -35,7 +36,14 @@ export default defineConfig({
markdown: {
syntaxHighlight: false,
smartypants: false,
remarkPlugins: [remarkMath, remarkDirective, remarkEmbed, remarkSpoiler, remarkReadingTime],
remarkPlugins: [
remarkMath,
remarkDirective,
remarkEmbed,
remarkSpoiler,
remarkReadingTime,
mermaid,
],
rehypePlugins: [
rehypeHeadingIds,
rehypeKatex,
Expand Down
15 changes: 15 additions & 0 deletions src/content/posts/markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,21 @@ we can use 3 backticks ``` in new line and write snippet and close with 3 backti
const var text = "hello world"
```

```mermaid
sequenceDiagram
participant Client as 客户端
participant Server as 服务器

Client->>Server: FIN=1, ACK=1, seq=u, ack=v
Note right of Client: 客户端发送FIN报文,表示没有数据要发送了
Server-->>Client: ACK=1, ack=u+1, seq=v
Note left of Server: 服务器确认FIN报文
Server->>Client: FIN=1, ACK=1, seq=w, ack=u+1
Note left of Server: 服务器发送FIN报文,表示没有数据要发送了
Client-->>Server: ACK=1, ack=w+1, seq=u+1
Note right of Client: 客户端确认FIN报文
```

## KaTeX 公式

使用 `$` 符号包裹公式生成行内公式,例如:$E = mc^2$。
Expand Down
5 changes: 5 additions & 0 deletions src/layouts/Layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ const { title, description, image } = Astro.props
<AccentColorInjector />
<PrintVersion />
</head>

<body>
<script type="module">
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs';
mermaid.initialize({ startOnLoad: true });
</script>
<slot />

<BackToTopFAB client:only="react" />
Expand Down
14 changes: 14 additions & 0 deletions src/plugins/remarkMermaidPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { visit } from 'unist-util-visit'

export function mermaid() {
return function (tree, { data }) {
console.log(tree)
console.log(data)
visit(tree, 'code', (node) => {
if (node.lang === 'mermaid') {
node.type = 'html'
node.value = `<pre class="mermaid">${node.value}</pre>`
}
})
}
}