Skip to content

Commit 79afb61

Browse files
OzakIOneslorber
andcommitted
feat(mdx-loader): Remark plugin to report unused MDX / Markdown directives (#9394)
Co-authored-by: sebastienlorber <[email protected]>
1 parent a57f232 commit 79afb61

File tree

19 files changed

+506
-26
lines changed

19 files changed

+506
-26
lines changed

argos/playwright.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import type {PlaywrightTestConfig} from '@playwright/test';
1414
const config: PlaywrightTestConfig = {
1515
testDir: './tests',
1616

17+
timeout: 60000,
18+
1719
reporter: [['list'], ['@argos-ci/playwright/reporter']],
1820

1921
// Run website production built

argos/tests/screenshot.spec.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ function isBlacklisted(pathname: string) {
3636
}
3737
// Some paths explicitly blacklisted
3838
const BlacklistedPathnames: string[] = [
39-
'/feature-requests', // Flaky because of Canny widget
40-
'/community/canary', // Flaky because of dynamic canary version fetched from npm
39+
// Flaky because of Canny widget
40+
'/feature-requests',
41+
// Flaky because of dynamic canary version fetched from npm
42+
'/community/canary',
43+
// Long blog post with many image carousels, often timeouts
44+
'/blog/2022/08/01/announcing-docusaurus-2.0',
4145
];
4246

4347
return (

packages/docusaurus-mdx-loader/src/loader.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
parseFrontMatter,
1212
escapePath,
1313
getFileLoaderUtils,
14+
getWebpackLoaderCompilerName,
1415
} from '@docusaurus/utils';
1516
import stringifyObject from 'stringify-object';
1617
import preprocessor from './preprocessor';
@@ -134,10 +135,12 @@ export async function mdxLoader(
134135
this: LoaderContext<Options>,
135136
fileString: string,
136137
): Promise<void> {
138+
const compilerName = getWebpackLoaderCompilerName(this);
137139
const callback = this.async();
138140
const filePath = this.resourcePath;
139141
const reqOptions: Options = this.getOptions();
140142
const {query} = this;
143+
141144
ensureMarkdownConfig(reqOptions);
142145

143146
const {frontMatter} = parseFrontMatter(fileString);
@@ -165,6 +168,7 @@ export async function mdxLoader(
165168
content: preprocessedContent,
166169
filePath,
167170
frontMatter,
171+
compilerName,
168172
});
169173
} catch (errorUnknown) {
170174
const error = errorUnknown as Error;

packages/docusaurus-mdx-loader/src/processor.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ import details from './remark/details';
1515
import head from './remark/head';
1616
import mermaid from './remark/mermaid';
1717
import transformAdmonitions from './remark/admonitions';
18+
import unusedDirectivesWarning from './remark/unusedDirectives';
1819
import codeCompatPlugin from './remark/mdx1Compat/codeCompatPlugin';
1920
import {getFormat} from './format';
21+
import type {WebpackCompilerName} from '@docusaurus/utils';
2022
import type {MDXFrontMatter} from './frontMatter';
2123
import type {Options} from './loader';
2224
import type {AdmonitionOptions} from './remark/admonitions';
@@ -37,10 +39,12 @@ type SimpleProcessor = {
3739
content,
3840
filePath,
3941
frontMatter,
42+
compilerName,
4043
}: {
4144
content: string;
4245
filePath: string;
4346
frontMatter: {[key: string]: unknown};
47+
compilerName: WebpackCompilerName;
4448
}) => Promise<SimpleProcessorResult>;
4549
};
4650

@@ -123,6 +127,7 @@ async function createProcessorFactory() {
123127
gfm,
124128
options.markdownConfig.mdx1Compat.comments ? comment : null,
125129
...(options.remarkPlugins ?? []),
130+
unusedDirectivesWarning,
126131
].filter((plugin): plugin is MDXPlugin => Boolean(plugin));
127132

128133
// codeCompatPlugin needs to be applied last after user-provided plugins
@@ -167,12 +172,13 @@ async function createProcessorFactory() {
167172
});
168173

169174
return {
170-
process: async ({content, filePath, frontMatter}) => {
175+
process: async ({content, filePath, frontMatter, compilerName}) => {
171176
const vfile = new VFile({
172177
value: content,
173178
path: filePath,
174179
data: {
175180
frontMatter,
181+
compilerName,
176182
},
177183
});
178184
return mdxProcessor.process(vfile).then((result) => ({

packages/docusaurus-mdx-loader/src/remark/contentTitle/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const plugin: Plugin = function plugin(
3535
const {toString} = await import('mdast-util-to-string');
3636
visit(root, 'heading', (headingNode: Heading, index, parent) => {
3737
if (headingNode.depth === 1) {
38+
vfile.data.compilerName;
3839
vfile.data.contentTitle = toString(headingNode);
3940
if (removeContentTitle) {
4041
parent!.children.splice(index, 1);

packages/docusaurus-mdx-loader/src/remark/mermaid/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*/
77

88
import visit from 'unist-util-visit';
9+
import {transformNode} from '../utils';
10+
911
// @ts-expect-error: TODO see https://github.com/microsoft/TypeScript/issues/49721
1012
import type {Transformer} from 'unified';
1113
import type {Code} from 'mdast';
@@ -16,10 +18,10 @@ import type {Code} from 'mdast';
1618
// by theme-mermaid itself
1719
export default function plugin(): Transformer {
1820
return (root) => {
19-
visit(root, 'code', (node: Code, index, parent) => {
21+
visit(root, 'code', (node: Code) => {
2022
if (node.lang === 'mermaid') {
2123
// TODO migrate to mdxJsxFlowElement? cf admonitions
22-
parent!.children.splice(index, 1, {
24+
transformNode(node, {
2325
type: 'mermaidCodeBlock',
2426
data: {
2527
hName: 'mermaid',

packages/docusaurus-mdx-loader/src/remark/transformImage/index.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import visit from 'unist-util-visit';
2020
import escapeHtml from 'escape-html';
2121
import sizeOf from 'image-size';
2222
import logger from '@docusaurus/logger';
23-
import {assetRequireAttributeValue} from '../utils';
23+
import {assetRequireAttributeValue, transformNode} from '../utils';
2424
// @ts-expect-error: TODO see https://github.com/microsoft/TypeScript/issues/49721
2525
import type {Transformer} from 'unified';
2626
// @ts-expect-error: TODO see https://github.com/microsoft/TypeScript/issues/49721
@@ -110,14 +110,12 @@ ${(err as Error).message}`;
110110
}
111111
}
112112

113-
Object.keys(jsxNode).forEach(
114-
(key) => delete jsxNode[key as keyof typeof jsxNode],
115-
);
116-
117-
jsxNode.type = 'mdxJsxTextElement';
118-
jsxNode.name = 'img';
119-
jsxNode.attributes = attributes;
120-
jsxNode.children = [];
113+
transformNode(jsxNode, {
114+
type: 'mdxJsxTextElement',
115+
name: 'img',
116+
attributes,
117+
children: [],
118+
});
121119
}
122120

123121
async function ensureImageFileExist(imagePath: string, sourceFilePath: string) {

packages/docusaurus-mdx-loader/src/remark/transformLinks/index.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
} from '@docusaurus/utils';
1818
import visit from 'unist-util-visit';
1919
import escapeHtml from 'escape-html';
20-
import {assetRequireAttributeValue} from '../utils';
20+
import {assetRequireAttributeValue, transformNode} from '../utils';
2121
// @ts-expect-error: TODO see https://github.com/microsoft/TypeScript/issues/49721
2222
import type {Transformer} from 'unified';
2323
// @ts-expect-error: TODO see https://github.com/microsoft/TypeScript/issues/49721
@@ -90,14 +90,12 @@ async function toAssetRequireNode(
9090

9191
const {children} = node;
9292

93-
Object.keys(jsxNode).forEach(
94-
(key) => delete jsxNode[key as keyof typeof jsxNode],
95-
);
96-
97-
jsxNode.type = 'mdxJsxTextElement';
98-
jsxNode.name = 'a';
99-
jsxNode.attributes = attributes;
100-
jsxNode.children = children;
93+
transformNode(jsxNode, {
94+
type: 'mdxJsxTextElement',
95+
name: 'a',
96+
attributes,
97+
children,
98+
});
10199
}
102100

103101
async function ensureAssetFileExist(assetPath: string, sourceFilePath: string) {

packages/docusaurus-mdx-loader/src/remark/unusedDirectives/__tests__/__fixtures__/containerDirectives.md

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/docusaurus-mdx-loader/src/remark/unusedDirectives/__tests__/__fixtures__/leafDirectives.md

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)