Skip to content

feat: 添加 Mermaid 图表支持 - #45

Open
lyimoexiao wants to merge 1 commit into
L33Z22L11:mainfrom
lyimoexiao:feature/mermaid
Open

feat: 添加 Mermaid 图表支持#45
lyimoexiao wants to merge 1 commit into
L33Z22L11:mainfrom
lyimoexiao:feature/mermaid

Conversation

@lyimoexiao

Copy link
Copy Markdown
Contributor
  • packages/remark-mermaid: 解析 Markdown 中的 mermaid 代码块
  • app/components/content/Mermaid.vue: Mermaid Vue 组件用于渲染
  • content/previews/example.md: 添加 Mermaid 预览示例
  • nuxt.config.ts: 导入 remark 解析器

Copilot AI review requested due to automatic review settings February 2, 2026 20:01
@vercel

vercel Bot commented Feb 2, 2026

Copy link
Copy Markdown

@lyimoexiao is attempting to deploy a commit to the Zhilu Sites Team on Vercel.

A member of the Team first needs to authorize it.

@netlify

netlify Bot commented Feb 2, 2026

Copy link
Copy Markdown

Deploy Preview for zhilu-blog ready!

Name Link
🔨 Latest commit 8458844
🔍 Latest deploy log https://app.netlify.com/projects/zhilu-blog/deploys/698237228c700e00077374a5
😎 Deploy Preview https://deploy-preview-45--zhilu-blog.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds Mermaid diagram support to the application, enabling rendering of flowcharts and diagrams directly from Markdown code blocks.

Changes:

  • Created a new remark-mermaid package to parse Mermaid code blocks in Markdown
  • Added a Vue component (Mermaid.vue) to render Mermaid diagrams with theme support
  • Updated project configuration to include the new package in the build process

Reviewed changes

Copilot reviewed 8 out of 9 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
pnpm-workspace.yaml Added mermaid and remark-mermaid dependencies to the catalog
packages/remark-mermaid/tsconfig.json TypeScript configuration for the new remark-mermaid package
packages/remark-mermaid/src/index.ts Core plugin implementation that transforms Mermaid code blocks into custom nodes
packages/remark-mermaid/package.json Package configuration for the remark-mermaid workspace package
package.json Added dependencies and updated build script to include remark-mermaid
nuxt.config.ts Registered the remark-mermaid plugin in the Nuxt configuration
content/previews/example.md Added example Mermaid diagram demonstrating the feature
app/components/content/Mermaid.vue Vue component for rendering Mermaid diagrams with dark/light theme support

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/remark-mermaid/package.json Outdated
Comment on lines +17 to +21
"devDependencies": {
"@zinkawaii/tsconfig": "catalog:framework",
"tsdown": "catalog:framework",
"typescript": "catalog:framework"
}

Copilot AI Feb 2, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The remark-mermaid package lacks test dependencies and test scripts. Consider adding a test framework (like vitest) and test coverage for the plugin logic, particularly for theme detection and node transformation.

Copilot uses AI. Check for mistakes.
Comment thread packages/remark-mermaid/src/index.ts Outdated
if (!className)
return options.theme ?? 'default'

const isDark = className.includes('dark') || className.includes('night')

Copilot AI Feb 2, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The theme detection logic is fragile and relies on substring matching. This could produce false positives (e.g., 'darkness' or 'nightly' would match). Consider using a more precise matching strategy or documented class name conventions.

Suggested change
const isDark = className.includes('dark') || className.includes('night')
const isDark = /\b(dark|night)\b/.test(className)

Copilot uses AI. Check for mistakes.
Comment thread app/components/content/Mermaid.vue Outdated
theme,
})

const id = `mermaid-${Date.now()}-${Math.random().toString(36).slice(2)}`

Copilot AI Feb 2, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using Date.now() and Math.random() for ID generation could lead to collisions in rapid re-renders. Consider using a more reliable unique ID generator or a counter-based approach with a component-scoped prefix.

Suggested change
const id = `mermaid-${Date.now()}-${Math.random().toString(36).slice(2)}`
const id = `mermaid-${currentId}`

Copilot uses AI. Check for mistakes.
Comment thread app/components/content/Mermaid.vue Outdated
})

const id = `mermaid-${Date.now()}-${Math.random().toString(36).slice(2)}`
container.value.innerHTML = `<div class="mermaid" id="${id}">${props.code}</div>`

Copilot AI Feb 2, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Direct innerHTML assignment with user-provided code creates an XSS vulnerability. The props.code should be properly escaped or sanitized before being inserted into the DOM, or use textContent for text-only insertion.

Copilot uses AI. Check for mistakes.
Comment thread app/components/content/Mermaid.vue Outdated
import { useDark } from '@vueuse/core'
import { computed, nextTick, onMounted, ref, watch } from 'vue'

type MermaidTheme = 'default' | 'base' | 'dark' | 'forest' | 'neutral' | 'null'

Copilot AI Feb 2, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The MermaidTheme type includes 'null' as a string literal, but MermaidOptions in the remark plugin uses 'night' instead. These theme definitions should be consistent across both files. Consider extracting a shared type definition.

Suggested change
type MermaidTheme = 'default' | 'base' | 'dark' | 'forest' | 'neutral' | 'null'
type MermaidTheme = 'default' | 'base' | 'dark' | 'forest' | 'neutral' | 'night'

Copilot uses AI. Check for mistakes.
@lyimoexiao

lyimoexiao commented Feb 3, 2026

Copy link
Copy Markdown
Contributor Author

目前这个方案实现有个问题:Mermaid 是客户端渲染的,当单页组件过多时会导致页面首屏卡顿。
但是如果改用 mermaid-cli 在 build 阶段预先转换成 svg 嵌入页面的话,似乎又会导致亮暗色主题自适应会有问题。。。(毕竟主题已经写死了)
还有一个就是渲染时概率导致图表位置错位,需要调整下渲染逻辑(

@lyimoexiao

Copy link
Copy Markdown
Contributor Author

我目前只能写到这了,只有 SPA 开发经验所以如果有哪些不好的地方请大佬指正下()
辛苦了

@L33Z22L11 L33Z22L11 self-assigned this Feb 24, 2026
@netlify

netlify Bot commented Apr 15, 2026

Copy link
Copy Markdown

Deploy Preview for zhilu-blog ready!

Name Link
🔨 Latest commit 618dcca
🔍 Latest deploy log https://app.netlify.com/projects/zhilu-blog/deploys/69df849881c36a0008f793da
😎 Deploy Preview https://deploy-preview-45--zhilu-blog.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants