Skip to content

Commit b3a66e4

Browse files
authored
Fix bug in remark-version-alias (#210)
Fixes #209 The plugin fails to process multiple successive `import` statements as expected in MDX files because it only operates on `MdxjsEsm` nodes with a single element in the `data.estree.body` array. Multiple successive `import` statements add elements to `body`. This change replaces the `@version` alias in multiple successive imports by replacing each node, then adding the concatenated replaced values of all imports to the `MdxjsEsm` node's `value`.
1 parent 3055d94 commit b3a66e4

File tree

2 files changed

+54
-24
lines changed

2 files changed

+54
-24
lines changed

server/remark-version-alias.test.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,32 @@ title: My page
3939
description: My page
4040
---
4141
42-
import CodeExample from '@site/content/15.x/examples/access-plugin-minimal/config.go'
42+
import CodeExample from '@site/content/15.x/examples/access-plugin-minimal/config.go';
43+
44+
This is a paragraph.
45+
`,
46+
path: "docs/mypage.mdx",
47+
},
48+
{
49+
description: "three import statements in latest-version docs path",
50+
input: `---
51+
title: My page
52+
description: My page
53+
---
54+
55+
import CodeExample from "@version/examples/access-plugin-minimal/config.go";
56+
import MyImage from "@version/myimg.png";
57+
import Triangle from "@version/triangle.png";
58+
59+
This is a paragraph.`,
60+
expected: `---
61+
title: My page
62+
description: My page
63+
---
64+
65+
import CodeExample from '@site/content/15.x/examples/access-plugin-minimal/config.go';
66+
import MyImage from '@site/content/15.x/myimg.png';
67+
import Triangle from '@site/content/15.x/triangle.png';
4368
4469
This is a paragraph.
4570
`,
@@ -60,7 +85,7 @@ title: My page
6085
description: My page
6186
---
6287
63-
import CodeExample from '@site/content/16.x/examples/access-plugin-minimal/config.go'
88+
import CodeExample from '@site/content/16.x/examples/access-plugin-minimal/config.go';
6489
6590
This is a paragraph.
6691
`,
@@ -81,7 +106,7 @@ title: My page
81106
description: My page
82107
---
83108
84-
import CodeExample from '!!raw-loader!@site/content/16.x/examples/access-plugin-minimal/config.go'
109+
import CodeExample from '!!raw-loader!@site/content/16.x/examples/access-plugin-minimal/config.go';
85110
86111
This is a paragraph.
87112
`,

server/remark-version-alias.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,41 @@ export default function remarkVersionAlias(latestVersion: string): Transformer {
1717
// Only process import statements that import an identifier from a default
1818
// export.
1919
const esm = node as unknown as MdxjsEsm;
20-
if (
21-
!esm.data ||
22-
!esm.data.estree ||
23-
esm.data.estree.body.length !== 1 ||
24-
esm.data.estree.body[0]["type"] != "ImportDeclaration" ||
25-
esm.data.estree.body[0].specifiers.length !== 1 ||
26-
esm.data.estree.body[0].specifiers[0].type != "ImportDefaultSpecifier"
27-
) {
20+
if (!esm.data || !esm.data.estree) {
2821
return CONTINUE;
2922
}
3023

3124
let version: string = latestVersion;
25+
let newVal: Array<string> = [];
3226
const versionedPathParts = vfile.path.match(versionedDocsPattern);
3327
if (versionedPathParts) {
3428
version = versionedPathParts[1];
3529
}
3630

37-
const decl = esm.data.estree.body[0];
38-
39-
const newPath = (decl.source.value as string).replace(
40-
"@version",
41-
`@site/content/${version}`,
42-
);
43-
44-
esm.value = `import ${esm.data.estree.body[0].specifiers[0].local.name} from '${newPath}'`;
45-
decl.source = {
46-
type: "Literal",
47-
value: newPath,
48-
raw: `"${newPath}"`,
49-
};
31+
esm.data.estree.body.forEach((decl) => {
32+
if (
33+
decl["type"] != "ImportDeclaration" ||
34+
decl.specifiers.length !== 1 ||
35+
decl.specifiers[0].type != "ImportDefaultSpecifier"
36+
) {
37+
return;
38+
}
39+
const newPath = (decl.source.value as string).replace(
40+
"@version",
41+
`@site/content/${version}`,
42+
);
43+
decl.source = {
44+
type: "Literal",
45+
value: newPath,
46+
raw: `"${newPath}"`,
47+
};
48+
49+
newVal.push(
50+
`import ${decl.specifiers[0].local.name} from '${newPath}';`,
51+
);
52+
});
53+
54+
esm.value = newVal.join("\n");
5055

5156
return SKIP;
5257
});

0 commit comments

Comments
 (0)