Skip to content

Commit 34dbccf

Browse files
authored
feat: resolve MDX content mapper remarkPlugins (#2060)
1 parent 584e53f commit 34dbccf

5 files changed

Lines changed: 23 additions & 3 deletions

File tree

‎packages/knip/fixtures/plugins/typescript/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
},
1111
"devDependencies": {
1212
"ember-content-mapper": "*",
13+
"remark-gfm": "*",
1314
"@tsconfig/node16": "*",
1415
"@tsconfig/node20": "*",
1516
"@tsconfig/node22": "*",

‎packages/knip/fixtures/plugins/typescript/tsconfig.content-mapper.json‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
"contentMappers": [
33
{
44
"package": "@mdx-js/content-mapper",
5-
"extensions": [".mdx"]
5+
"extensions": [".mdx"],
6+
"options": {
7+
"remarkPlugins": ["remark-gfm", ["remark-math", {}], "remark-mdx-frontmatter"]
8+
}
69
},
710
{
811
"package": "ember-content-mapper",

‎packages/knip/src/plugins/typescript/index.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ const config = ['tsconfig.json', 'package.json'];
1919

2020
const packageJsonPath = 'typescript.contentMapper';
2121

22+
const mdxContentMappers = ['mdx-content-mapper', '@mdx-js/content-mapper'];
23+
const mdxTransformerPlugins = ['remark-mdx-frontmatter'];
24+
2225
const resolveContentMapper = ({ exec }: ContentMapperManifest, options: PluginOptions) => {
2326
if (!Array.isArray(exec) || exec.some(arg => typeof arg !== 'string')) return [];
2427
return options
@@ -42,7 +45,17 @@ const resolveTsConfig = (localConfig: TsConfigJson, options: PluginOptions) => {
4245
?.filter(reference => reference.path.endsWith('.json'))
4346
.map(reference => toConfig('typescript', reference.path, { containingFilePath: options.configFilePath })) ?? [];
4447

45-
const contentMappers = localConfig.contentMappers?.map(contentMapper => toDependency(contentMapper.package)) ?? [];
48+
const contentMappers = [];
49+
for (const { package: name, options: mapperOptions } of localConfig.contentMappers ?? []) {
50+
contentMappers.push(toDependency(name));
51+
if (!mdxContentMappers.includes(name)) continue;
52+
const remarkPlugins = mapperOptions?.remarkPlugins;
53+
if (!Array.isArray(remarkPlugins)) continue;
54+
for (const plugin of remarkPlugins) {
55+
const id = typeof plugin === 'string' ? plugin : plugin[0];
56+
if (typeof id === 'string' && !mdxTransformerPlugins.includes(id)) contentMappers.push(toDependency(id));
57+
}
58+
}
4659

4760
if (!(compilerOptions && localConfig)) return compact([...contentMappers, ...extend, ...references]);
4861

‎packages/knip/src/types/tsconfig-json.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface TsConfigJson {
88
};
99
contentMappers?: {
1010
package: string;
11+
options?: { remarkPlugins?: Array<string | [string, ...unknown[]]> };
1112
}[];
1213
references?: Array<{ path: string }>;
1314
}

‎packages/knip/test/plugins/typescript.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ test('Find dependencies with the TypeScript plugin', async () => {
1515
assert(issues.unresolved['tsconfig.json']['ts-graphql-plugin']);
1616
assert(issues.unresolved['tsconfig.json']['tslib']); // resolved up to dep of knip itself
1717
assert(issues.unlisted['tsconfig.content-mapper.json']['@mdx-js/content-mapper']);
18+
assert(issues.unlisted['tsconfig.content-mapper.json']['remark-math']);
19+
assert(!issues.unlisted['tsconfig.content-mapper.json']['remark-mdx-frontmatter']);
1820
assert(issues.unlisted['tsconfig.jsx-import-source-preact.json']['preact']);
1921
assert(issues.unresolved['tsconfig.jsx-import-source-preact.json']['preact']);
2022
assert(issues.unresolved['tsconfig.jsx-import-source-react.json']['vitest/globals']);
@@ -23,7 +25,7 @@ test('Find dependencies with the TypeScript plugin', async () => {
2325
assert.deepEqual(counters, {
2426
...baseCounters,
2527
binaries: 1,
26-
unlisted: 3,
28+
unlisted: 4,
2729
unresolved: 5,
2830
processed: 0,
2931
total: 0,

0 commit comments

Comments
 (0)