Skip to content

Commit f0953b8

Browse files
committed
refactor: give plugins their own files
1 parent d39df03 commit f0953b8

File tree

9 files changed

+312
-254
lines changed

9 files changed

+312
-254
lines changed

src/utils/markdown/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
export { renderMarkdown } from './processor'
2-
export { rehypeParseCommentComponents } from './plugins'

src/utils/markdown/plugins.ts

Lines changed: 0 additions & 248 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { visit } from 'unist-util-visit'
2+
import { toString } from 'hast-util-to-string'
3+
4+
import { isHeading } from './helpers'
5+
6+
export type MarkdownHeading = {
7+
id: string
8+
text: string
9+
level: number
10+
}
11+
12+
export function rehypeCollectHeadings(
13+
tree,
14+
file,
15+
initialHeadings?: MarkdownHeading[],
16+
) {
17+
const headings = initialHeadings ?? []
18+
19+
return function collectHeadings(tree, file: any) {
20+
visit(tree, 'element', (node) => {
21+
if (!isHeading(node)) {
22+
return
23+
}
24+
25+
const id =
26+
typeof node.properties?.id === 'string' ? node.properties.id : ''
27+
if (!id) {
28+
return
29+
}
30+
31+
headings.push({
32+
id,
33+
level: Number(node.tagName.substring(1)),
34+
text: toString(node).trim(),
35+
})
36+
})
37+
38+
if (file) {
39+
file.data.headings = headings
40+
}
41+
}
42+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { isElement } from 'hast-util-is-element'
2+
import type { Element } from 'hast-util-is-element/lib'
3+
4+
export const normalizeComponentName = (name: string) => name.toLowerCase()
5+
6+
export const slugify = (value: string, fallback: string) => {
7+
if (!value) {
8+
return fallback
9+
}
10+
11+
return (
12+
value
13+
.trim()
14+
.toLowerCase()
15+
.replace(/[^a-z0-9\s-]/g, '')
16+
.replace(/\s+/g, '-')
17+
.replace(/-+/g, '-')
18+
.replace(/^-|-$/g, '')
19+
.slice(0, 64) || fallback
20+
)
21+
}
22+
23+
export const isHeading = (node: unknown): node is Element =>
24+
isElement(node) && /^h[1-6]$/.test(node.tagName)
25+
26+
export const headingLevel = (node: Element) => Number(node.tagName.substring(1))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export { rehypeParseCommentComponents } from './parseCommentComponents'
2+
export { rehypeTransformCommentComponents } from './transformCommentComponents'
3+
export { transformTabsComponent } from './transformTabsComponent'
4+
export { type MarkdownHeading, rehypeCollectHeadings } from './collectHeadings'

0 commit comments

Comments
 (0)