-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rehype-mdx-import-media.ts
295 lines (254 loc) · 8.19 KB
/
rehype-mdx-import-media.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
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
import {
type Expression,
type Identifier,
type ImportDeclaration,
type TemplateElement
} from 'estree'
import { type Root } from 'hast'
import { propertiesToMdxJsxAttributes } from 'hast-util-properties-to-mdx-jsx-attributes'
import parseSrcset from 'parse-srcset'
import { type Plugin } from 'unified'
import { visit } from 'unist-util-visit'
export interface RehypeMdxImportMediaOptions {
/**
* HTML element attributes that should be processed. The key is the HTML element tag name. The
* value is a list of attribute names to process. The default value is {@link defaultAttributes}
*/
attributes?: Record<string, Iterable<string>>
/**
* The casing to use for attribute names.
*
* This should match the `elementAttributeNameCase` value passed to MDX.
*
* @default 'react'
* @see https://mdxjs.com/packages/mdx/#processoroptions
*/
elementAttributeNameCase?: 'html' | 'react'
/**
* Where to keep URL hash.
*
* - `both`: Keep the URL hash on both the import source and the JSX prop.
* - `import`: Only keep the URL hash on the import source.
* - `jsx`: Only keep the URL hash on the JSX prop.
* - `none`: Remove the URL hash.
*
* @default 'import'
*/
preserveHash?: 'both' | 'import' | 'jsx' | 'none'
/**
* Where to keep query parameters.
*
* - `both`: Keep the query parameters on both the import source and the JSX prop.
* - `import`: Only keep the query parameters on the import source.
* - `jsx`: Only keep the query parameters on the JSX prop.
* - `none`: Remove the query parameters.
*
* @default 'import'
*/
preserveQuery?: 'both' | 'import' | 'jsx' | 'none'
/**
* By default imports are resolved relative to the input file. This matches default markdown
* behaviour. If this is set to false, this behaviour is removed and URLs are no longer processed.
* This allows to import images from `node_modules`. If this is disabled, local images can still
* be imported by prepending the path with `./`.
*
* @default true
*/
resolve?: boolean
}
const urlPattern = /^(https?:)?\//
const relativePathPattern = /\.\.?\//
export const defaultAttributes: Record<string, Iterable<string>> = {
audio: 'src',
embed: 'src',
img: ['src', 'srcset'],
object: 'data',
source: ['src', 'srcset'],
track: 'src',
video: ['poster', 'src']
}
/**
* A rehype MDX plugin for converting media sources into imports.
*/
const rehypeMdxImportMedia: Plugin<[RehypeMdxImportMediaOptions?], Root> = ({
attributes = defaultAttributes,
elementAttributeNameCase,
preserveHash = 'import',
preserveQuery = 'import',
resolve = true
} = {}) => {
const elementMap = new Map(
Object.entries(attributes).map(([tagName, attributeNames]) => {
const set = new Set<string>()
if (typeof attributeNames === 'string') {
set.add(attributeNames.toLowerCase())
} else {
for (const name of attributeNames) {
set.add(name.toLowerCase())
}
}
return [tagName.toLowerCase(), set]
})
)
return (ast) => {
const imports: ImportDeclaration[] = []
const imported = new Map<string, string>()
visit(ast, 'element', (node, index, parent) => {
const attributeNames = elementMap.get(node.tagName)
if (!attributeNames) {
return
}
let shouldReplace = false
// Don’t even bother continuing if there are no properties to replace.
for (const name in node.properties) {
if (attributeNames.has(name.toLowerCase())) {
shouldReplace = true
break
}
}
if (!shouldReplace) {
return
}
shouldReplace = false
/**
* Generate an identifier node for an import path.
*
* If the path should not be replaced, nothing is returned. If an identifier was already
* calculated for this path, it is returned instead.
*
* @param importSource
* The path to get an identifier for.
* @returns
* The matching identifier, or none.
*/
function getIdentifier(importSource: string): [] | [Identifier, string] {
let value = importSource
if (urlPattern.test(value)) {
return []
}
if (!relativePathPattern.test(value) && resolve) {
value = `./${value}`
}
const hashIndex = value.indexOf('#')
const hash = hashIndex === -1 ? '' : value.slice(hashIndex)
const remainder = hashIndex === -1 ? value : value.slice(0, hashIndex)
const queryIndex = remainder.indexOf('?')
const query = queryIndex === -1 ? '' : remainder.slice(queryIndex)
value = queryIndex === -1 ? remainder : remainder.slice(0, queryIndex)
let propChunk = ''
if (preserveQuery === 'import') {
value += query
} else if (preserveQuery === 'jsx') {
propChunk += query
} else if (preserveQuery === 'both') {
value += query
propChunk += query
}
if (preserveHash === 'import') {
value += hash
} else if (preserveHash === 'jsx') {
propChunk += hash
} else if (preserveHash === 'both') {
value += hash
propChunk += hash
}
let name = imported.get(value)
if (!name) {
name = `_rehypeMdxImportMedia${imported.size}`
imports.push({
type: 'ImportDeclaration',
source: { type: 'Literal', value },
specifiers: [{ type: 'ImportDefaultSpecifier', local: { type: 'Identifier', name } }]
})
imported.set(value, name)
}
shouldReplace = true
return [{ type: 'Identifier', name }, propChunk]
}
const replacements = propertiesToMdxJsxAttributes(node.properties, {
elementAttributeNameCase,
transform(name, value) {
if (!value) {
return value
}
const lower = name.toLowerCase()
if (!attributeNames.has(lower)) {
return value
}
if (lower !== 'srcset') {
const [identifier, extra] = getIdentifier(value)
if (!identifier) {
return value
}
if (extra) {
return {
type: 'TemplateLiteral',
expressions: [identifier],
quasis: [
{ type: 'TemplateElement', tail: false, value: { raw: '' } },
{ type: 'TemplateElement', tail: true, value: { raw: extra } }
]
}
}
return identifier
}
const srcset = parseSrcset(value)
const expressions: Expression[] = []
const quasis: TemplateElement[] = []
let raw = ''
for (const [srcIndex, src] of srcset.entries()) {
const [identifier, extra] = getIdentifier(src.url)
if (identifier) {
quasis.push({ type: 'TemplateElement', tail: false, value: { raw } })
expressions.push(identifier)
raw = extra!
} else {
raw += src.url
}
if (src.d) {
raw += ` ${src.d}x`
}
if (src.w) {
raw += ` ${src.w}w`
}
if (src.h) {
raw += ` ${src.h}h`
}
if (srcIndex < srcset.length - 1) {
raw += ','
}
}
if (!expressions.length) {
return value
}
quasis.push({ type: 'TemplateElement', tail: true, value: { raw } })
return { type: 'TemplateLiteral', expressions, quasis }
}
})
if (shouldReplace) {
parent!.children[index!] = {
type: 'mdxJsxTextElement',
name: node.tagName,
attributes: replacements,
children: node.children,
data: node.data,
position: node.position
}
}
})
if (imports.length) {
ast.children.unshift({
type: 'mdxjsEsm',
value: '',
data: {
estree: {
type: 'Program',
sourceType: 'module',
body: imports
}
}
})
}
}
}
export default rehypeMdxImportMedia