| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
export function remarkCdnVersion({version, token = '__VERSION__'} = {}) { |
| 18 |
if (!version) throw new Error('remarkCdnVersion: version is required'); |
| 19 |
const sub = (s) => typeof s === 'string' ? s.split(token).join(version) : s; |
| 20 |
return function (tree) { |
| 21 |
replace(tree, sub); |
| 22 |
}; |
| 23 |
} |
| 24 |
|
| 25 |
function replace(node, sub) { |
| 26 |
if (node.type === 'code' || node.type === 'inlineCode' || node.type === 'html') { |
| 27 |
node.value = sub(node.value); |
| 28 |
} |
| 29 |
if (node.type === 'mdxJsxFlowElement' || node.type === 'mdxJsxTextElement') { |
| 30 |
for (const attr of node.attributes || []) { |
| 31 |
if (attr.type === 'mdxJsxAttribute') attr.value = sub(attr.value); |
| 32 |
} |
| 33 |
} |
| 34 |
if (node.children) for (const child of node.children) replace(child, sub); |
| 35 |
} |