Skip to content

Commit

Permalink
fix: add heading id to titles for enabling toc navigation (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
guqing authored Feb 28, 2024
1 parent 4b8e697 commit 2d833dd
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
2 changes: 2 additions & 0 deletions console/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
"ink-mde": "^0.29.1",
"rehype-highlight": "^7.0.0",
"rehype-raw": "^7.0.0",
"rehype-slug": "^6.0.0",
"rehype-stringify": "^10.0.0",
"remark-breaks": "^4.0.0",
"remark-gfm": "^4.0.0",
"remark-heading-id": "^1.0.1",
"remark-images": "^4.0.0",
"remark-parse": "^11.0.0",
"remark-rehype": "^11.0.0",
Expand Down
57 changes: 56 additions & 1 deletion console/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions console/src/lib/remark/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ import remarkBreaks from "remark-breaks";
import remarkImages from "remark-images";
import rehypeHighlight from "rehype-highlight";
import remarkGfm from "remark-gfm";
import slug from 'rehype-slug';
import remarkHeadingId from 'remark-heading-id';

export const renderToHtml = async (sourceCode: string): Promise<string> => {
var file = await unified()
.use(remarkParse)
.use(remarkParse) // 解析 Markdown
.use(remarkGfm)
.use(remarkBreaks)
.use(remarkImages)
.use(remarkRehype, { allowDangerousHtml: true })
.use(remarkHeadingId)
.use(remarkRehype, { allowDangerousHtml: true }) // 将 Markdown AST 转换为 HTML AST
.use(rehypeRaw)
.use(rehypeStringify)
.use(slug) // 为所有标题元素添加 ID
.use(rehypeStringify) // 将 HTML AST 转换为字符串形式的 HTML
.use(rehypeHighlight)
.process(sourceCode);
return String(file);
Expand Down

0 comments on commit 2d833dd

Please sign in to comment.