-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
180 additions
and
4 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 |
---|---|---|
|
@@ -9,4 +9,14 @@ literals www.example.com, https://example.com, and [email protected]. | |
|
||
```js | ||
console.log(2) | ||
``` | ||
``` | ||
|
||
# h1 | ||
|
||
## h2 \`code\` | ||
|
||
### h3 [link](https://islandjs.dev) | ||
|
||
#### h4 你好啊 | ||
|
||
##### h5 |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,65 @@ | ||
import Slugger from 'github-slugger' | ||
import { visit } from 'unist-util-visit' | ||
import { Root } from 'mdast' | ||
import { parse } from 'acorn' | ||
import type { Plugin } from 'unified' | ||
import type { MdxjsEsm, Program } from 'mdast-util-mdxjs-esm' | ||
|
||
const slugger = new Slugger() | ||
|
||
interface TocItem { | ||
id: string | ||
text: string | ||
depth: number | ||
} | ||
interface ChildNode { | ||
type: 'link' | 'text' | 'inlineCode' | ||
value: string | ||
children?: ChildNode[] | ||
} | ||
export const remarkPluginToc: Plugin<[], Root> = () => { | ||
return (tree) => { | ||
// 初始化 toc 数组 | ||
const toc: TocItem[] = [] | ||
// 遍历 tree | ||
visit(tree, 'heading', (node) => { | ||
// debugger | ||
if (!node.depth || !node.children) return | ||
// 收集 h2~h4 的标题 | ||
if (node.depth > 1 && node.depth < 5) { | ||
const text = (node.children as ChildNode[]) | ||
.map((n) => { | ||
// case: link内容解析 | ||
switch (n.type) { | ||
case 'link': | ||
return n.children?.map((c) => c.value).join('') || '' | ||
default: | ||
return n.value | ||
} | ||
}) | ||
.join('') | ||
// id 通过 slugger 生成规范化的标题id | ||
const id = slugger.slug(text) | ||
const item: TocItem = { | ||
id, | ||
text, | ||
depth: node.depth, | ||
} | ||
toc.push(item) | ||
} | ||
}) | ||
// 插入 toc | ||
const insertCode = `export const toc = ${JSON.stringify(toc, null, 2)}` | ||
tree.children.unshift({ | ||
type: 'mdxjsEsm', | ||
value: insertCode, | ||
// 附加ast信息 | ||
data: { | ||
estree: parse(insertCode, { | ||
ecmaVersion: 2020, | ||
sourceType: 'module', | ||
}) as Program, | ||
}, | ||
} as MdxjsEsm) | ||
} | ||
} |