generated from un-ts/lib-boilerplate
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
159 lines (145 loc) · 4.6 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import type { Content, Element } from 'hast'
import { isElement } from 'hast-util-is-element'
import type { Paragraph, Strong, Text } from 'mdast'
import type { BlockContent, ContainerDirective } from 'mdast-util-directive'
import rehypeParse from 'rehype-parse'
import rehypeRemark, { all, defaultHandlers } from 'rehype-remark'
import remarkDirective from 'remark-directive'
import remarkGfm from 'remark-gfm'
import remarkStringify from 'remark-stringify'
import { unified } from 'unified'
import type { MinimalDuplex } from 'unified-stream'
import { stream } from 'unified-stream'
const getClassList = (node: Element) =>
node.properties?.className as string[] | undefined
const processor = unified()
.use(rehypeParse, { fragment: true })
.use(remarkDirective)
.use(remarkGfm)
.use(rehypeRemark, {
handlers: {
br(h, node: Content) {
return h(node, 'html', '<br>')
},
div(h, node: Element) {
const classList = getClassList(node)
if (
// ignore toc
classList?.includes('toc-macro') ||
// ignore code block header
classList?.includes('codeHeader')
) {
return
}
const nodes = all(h, node)
if (classList?.includes('confluence-information-macro')) {
let type: string | undefined
for (const className of classList) {
const matched = /confluence-information-macro-(.*)/.exec(className)
if (matched) {
type = matched[1]
break
}
}
if (type) {
const alert: ContainerDirective = {
type: 'containerDirective',
name: 'alert',
attributes: {
type,
},
children: nodes as BlockContent[],
}
return alert
}
}
if (classList?.includes('panel') && !classList.includes('code')) {
const panelTitle = nodes.find(
(node): node is Strong => node.type === 'strong',
)
const panel: ContainerDirective = {
type: 'containerDirective',
name: 'panel',
attributes: panelTitle && {
title: panelTitle.children.find(
(node): node is Text => node.type === 'text',
)!.value,
},
children: nodes.filter(
(node): node is Paragraph => node.type === 'paragraph',
),
}
return panel
}
return nodes
},
pre(h, node: Element, parent) {
const params = node.properties?.dataSyntaxhighlighterParams ?? ''
const matched = /brush:\s*([^;]+);/.exec(params as string)
const lang = matched?.[1]
const classList = getClassList(node)
const properties = {
className: lang && [...(classList ?? []), 'language-' + lang],
}
let expandable = false
if (isElement(parent)) {
const parentClassList = getClassList(parent as Element)
if (parentClassList?.includes('hide-toolbar')) {
expandable = true
}
}
const codeNode = defaultHandlers.pre(h, {
...node,
children: node.children.map(child =>
child.type === 'text'
? {
type: 'element',
tagName: 'code',
children: [child],
properties,
}
: {
...child,
properties: {
...('properties' in child && child.properties),
...properties,
},
},
),
})
return codeNode && expandable
? {
...codeNode,
meta: 'expandable',
}
: codeNode
},
strong(h, node: Element) {
const strongNode = defaultHandlers.strong(h, node) as Strong
for (const child of strongNode.children) {
if (child.type === 'text') {
child.value = child.value.trim()
}
}
return strongNode
},
},
})
.use(remarkStringify, {
fences: true,
})
.freeze()
export function cf2md(
input: string,
encoding?: BufferEncoding | undefined,
): Promise<string>
export function cf2md(input: NodeJS.ReadableStream): MinimalDuplex
export function cf2md(
input: NodeJS.ReadableStream | string,
encoding?: BufferEncoding | undefined,
) {
if (typeof input === 'string') {
return processor.process(input).then(vfile => vfile.toString(encoding))
}
return input.pipe(stream(processor()))
}