-
-
Notifications
You must be signed in to change notification settings - Fork 847
/
index.js
322 lines (295 loc) · 9.93 KB
/
index.js
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Register `Raw` in tree:
/// <reference types="mdast-util-to-hast" />
/**
* @typedef {import('hast').Element} Element
* @typedef {import('hast').ElementContent} ElementContent
* @typedef {import('hast').Nodes} Nodes
* @typedef {import('hast').Parents} Parents
* @typedef {import('hast').Root} Root
* @typedef {import('hast-util-to-jsx-runtime').Components} JsxRuntimeComponents
* @typedef {import('remark-rehype').Options} RemarkRehypeOptions
* @typedef {import('unist-util-visit').BuildVisitor<Root>} Visitor
* @typedef {import('unified').PluggableList} PluggableList
*/
/**
* @callback AllowElement
* Filter elements.
* @param {Readonly<Element>} element
* Element to check.
* @param {number} index
* Index of `element` in `parent`.
* @param {Readonly<Parents> | undefined} parent
* Parent of `element`.
* @returns {boolean | null | undefined}
* Whether to allow `element` (default: `false`).
*
* @typedef {Partial<JsxRuntimeComponents>} Components
* Map tag names to components.
*
* @typedef Deprecation
* Deprecation.
* @property {string} from
* Old field.
* @property {string} id
* ID in readme.
* @property {keyof Options} [to]
* New field.
*
* @typedef Options
* Configuration.
* @property {AllowElement | null | undefined} [allowElement]
* Filter elements (optional);
* `allowedElements` / `disallowedElements` is used first.
* @property {ReadonlyArray<string> | null | undefined} [allowedElements]
* Tag names to allow (default: all tag names);
* cannot combine w/ `disallowedElements`.
* @property {string | null | undefined} [children]
* Markdown.
* @property {string | null | undefined} [className]
* Wrap in a `div` with this class name.
* @property {Components | null | undefined} [components]
* Map tag names to components.
* @property {ReadonlyArray<string> | null | undefined} [disallowedElements]
* Tag names to disallow (default: `[]`);
* cannot combine w/ `allowedElements`.
* @property {PluggableList | null | undefined} [rehypePlugins]
* List of rehype plugins to use.
* @property {PluggableList | null | undefined} [remarkPlugins]
* List of remark plugins to use.
* @property {Readonly<RemarkRehypeOptions> | null | undefined} [remarkRehypeOptions]
* Options to pass through to `remark-rehype`.
* @property {boolean | null | undefined} [skipHtml=false]
* Ignore HTML in markdown completely (default: `false`).
* @property {boolean | null | undefined} [unwrapDisallowed=false]
* Extract (unwrap) what’s in disallowed elements (default: `false`);
* normally when say `strong` is not allowed, it and it’s children are dropped,
* with `unwrapDisallowed` the element itself is replaced by its children.
* @property {UrlTransform | null | undefined} [urlTransform]
* Change URLs (default: `defaultUrlTransform`)
*
* @callback UrlTransform
* Transform all URLs.
* @param {string} url
* URL.
* @param {string} key
* Property name (example: `'href'`).
* @param {Readonly<Element>} node
* Node.
* @returns {string | null | undefined}
* Transformed URL (optional).
*/
import {unreachable} from 'devlop'
import {toJsxRuntime} from 'hast-util-to-jsx-runtime'
import {urlAttributes} from 'html-url-attributes'
import {Fragment, jsx, jsxs} from 'react/jsx-runtime'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import {unified} from 'unified'
import {visit} from 'unist-util-visit'
import {VFile} from 'vfile'
const changelog =
'https://github.com/remarkjs/react-markdown/blob/main/changelog.md'
/** @type {PluggableList} */
const emptyPlugins = []
/** @type {Readonly<RemarkRehypeOptions>} */
const emptyRemarkRehypeOptions = {allowDangerousHtml: true}
const safeProtocol = /^(https?|ircs?|mailto|xmpp)$/i
// Mutable because we `delete` any time it’s used and a message is sent.
/** @type {ReadonlyArray<Readonly<Deprecation>>} */
const deprecations = [
{from: 'astPlugins', id: 'remove-buggy-html-in-markdown-parser'},
{from: 'allowDangerousHtml', id: 'remove-buggy-html-in-markdown-parser'},
{
from: 'allowNode',
id: 'replace-allownode-allowedtypes-and-disallowedtypes',
to: 'allowElement'
},
{
from: 'allowedTypes',
id: 'replace-allownode-allowedtypes-and-disallowedtypes',
to: 'allowedElements'
},
{
from: 'disallowedTypes',
id: 'replace-allownode-allowedtypes-and-disallowedtypes',
to: 'disallowedElements'
},
{from: 'escapeHtml', id: 'remove-buggy-html-in-markdown-parser'},
{from: 'includeElementIndex', id: '#remove-includeelementindex'},
{
from: 'includeNodeIndex',
id: 'change-includenodeindex-to-includeelementindex'
},
{from: 'linkTarget', id: 'remove-linktarget'},
{from: 'plugins', id: 'change-plugins-to-remarkplugins', to: 'remarkPlugins'},
{from: 'rawSourcePos', id: '#remove-rawsourcepos'},
{from: 'renderers', id: 'change-renderers-to-components', to: 'components'},
{from: 'source', id: 'change-source-to-children', to: 'children'},
{from: 'sourcePos', id: '#remove-sourcepos'},
{from: 'transformImageUri', id: '#add-urltransform', to: 'urlTransform'},
{from: 'transformLinkUri', id: '#add-urltransform', to: 'urlTransform'}
]
/**
* Component to render markdown.
*
* @param {Readonly<Options>} options
* Props.
* @returns {JSX.Element}
* React element.
*/
export function Markdown(options) {
const allowedElements = options.allowedElements
const allowElement = options.allowElement
const children = options.children || ''
const className = options.className
const components = options.components
const disallowedElements = options.disallowedElements
const rehypePlugins = options.rehypePlugins || emptyPlugins
const remarkPlugins = options.remarkPlugins || emptyPlugins
const remarkRehypeOptions = options.remarkRehypeOptions
? {...options.remarkRehypeOptions, ...emptyRemarkRehypeOptions}
: emptyRemarkRehypeOptions
const skipHtml = options.skipHtml
const unwrapDisallowed = options.unwrapDisallowed
const urlTransform = options.urlTransform || defaultUrlTransform
const processor = unified()
.use(remarkParse)
.use(remarkPlugins)
.use(remarkRehype, remarkRehypeOptions)
.use(rehypePlugins)
const file = new VFile()
if (typeof children === 'string') {
file.value = children
} else {
unreachable(
'Unexpected value `' +
children +
'` for `children` prop, expected `string`'
)
}
if (allowedElements && disallowedElements) {
unreachable(
'Unexpected combined `allowedElements` and `disallowedElements`, expected one or the other'
)
}
for (const deprecation of deprecations) {
if (Object.hasOwn(options, deprecation.from)) {
unreachable(
'Unexpected `' +
deprecation.from +
'` prop, ' +
(deprecation.to
? 'use `' + deprecation.to + '` instead'
: 'remove it') +
' (see <' +
changelog +
'#' +
deprecation.id +
'> for more info)'
)
}
}
const mdastTree = processor.parse(file)
/** @type {Nodes} */
let hastTree = processor.runSync(mdastTree, file)
// Wrap in `div` if there’s a class name.
if (className) {
hastTree = {
type: 'element',
tagName: 'div',
properties: {className},
// Assume no doctypes.
children: /** @type {Array<ElementContent>} */ (
hastTree.type === 'root' ? hastTree.children : [hastTree]
)
}
}
visit(hastTree, transform)
return toJsxRuntime(hastTree, {
Fragment,
components,
ignoreInvalidStyle: true,
// @ts-expect-error: to do: types.
jsx,
// @ts-expect-error: to do: types.
jsxs,
passKeys: true,
passNode: true
})
/** @type {Visitor} */
function transform(node, index, parent) {
if (node.type === 'raw' && parent && typeof index === 'number') {
if (skipHtml) {
parent.children.splice(index, 1)
} else {
parent.children[index] = {type: 'text', value: node.value}
}
return index
}
if (node.type === 'element') {
/** @type {string} */
let key
for (key in urlAttributes) {
if (
Object.hasOwn(urlAttributes, key) &&
Object.hasOwn(node.properties, key)
) {
const value = node.properties[key]
const test = urlAttributes[key]
if (test === null || test.includes(node.tagName)) {
node.properties[key] = urlTransform(String(value || ''), key, node)
}
}
}
}
if (node.type === 'element') {
let remove = allowedElements
? !allowedElements.includes(node.tagName)
: disallowedElements
? disallowedElements.includes(node.tagName)
: false
if (!remove && allowElement && typeof index === 'number') {
remove = !allowElement(node, index, parent)
}
if (remove && parent && typeof index === 'number') {
if (unwrapDisallowed && node.children) {
parent.children.splice(index, 1, ...node.children)
} else {
parent.children.splice(index, 1)
}
return index
}
}
}
}
/**
* Make a URL safe.
*
* @satisfies {UrlTransform}
* @param {string} value
* URL.
* @returns {string}
* Safe URL.
*/
export function defaultUrlTransform(value) {
// Same as:
// <https://github.com/micromark/micromark/blob/929275e/packages/micromark-util-sanitize-uri/dev/index.js#L34>
// But without the `encode` part.
const colon = value.indexOf(':')
const questionMark = value.indexOf('?')
const numberSign = value.indexOf('#')
const slash = value.indexOf('/')
if (
// If there is no protocol, it’s relative.
colon < 0 ||
// If the first colon is after a `?`, `#`, or `/`, it’s not a protocol.
(slash > -1 && colon > slash) ||
(questionMark > -1 && colon > questionMark) ||
(numberSign > -1 && colon > numberSign) ||
// It is a protocol, it should be allowed.
safeProtocol.test(value.slice(0, colon))
) {
return value
}
return ''
}