-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathremark-logos.js
More file actions
68 lines (56 loc) · 1.8 KB
/
Copy pathremark-logos.js
File metadata and controls
68 lines (56 loc) · 1.8 KB
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
/**
* Remark plugin that converts :logos-<name>: shortcodes into inline
* <img> elements pointing to /img/logos/<name>.svg.
*
* Works with Docusaurus 3 / MDX v3 by emitting mdxJsxTextElement AST
* nodes that the MDX compiler handles natively.
*/
function remarkLogos() {
const logoPattern = /:logos-([^:]+):/;
function transformChildren(node) {
if (!node.children) return;
for (let i = 0; i < node.children.length; i++) {
const child = node.children[i];
if (child.type === 'text' && logoPattern.test(child.value)) {
const newNodes = splitTextNode(child);
node.children.splice(i, 1, ...newNodes);
i += newNodes.length - 1;
} else {
transformChildren(child);
}
}
}
function splitTextNode(node) {
const regex = /:logos-([^:]+):/g;
const parts = [];
let lastIndex = 0;
let match;
while ((match = regex.exec(node.value)) !== null) {
// Text before the match
if (match.index > lastIndex) {
parts.push({ type: 'text', value: node.value.slice(lastIndex, match.index) });
}
// Inline <img> as an MDX JSX element
parts.push({
type: 'mdxJsxTextElement',
name: 'img',
attributes: [
{ type: 'mdxJsxAttribute', name: 'src', value: `/img/logos/${match[1]}.svg` },
{ type: 'mdxJsxAttribute', name: 'alt', value: match[1] },
{ type: 'mdxJsxAttribute', name: 'className', value: 'inline-logo' },
],
children: [],
});
lastIndex = regex.lastIndex;
}
// Remaining text after the last match
if (lastIndex < node.value.length) {
parts.push({ type: 'text', value: node.value.slice(lastIndex) });
}
return parts;
}
return (tree) => {
transformChildren(tree);
};
}
module.exports = remarkLogos;