Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mdx-loader): Remark plugin to report unused MDX / Markdown directives #9394

Merged
merged 27 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
1c2b05b
wip: unused directives plugin
OzakIOne Oct 11, 2023
2f6cddb
wip: parse tree from root
OzakIOne Oct 12, 2023
0478dd8
wip: unit test mock
OzakIOne Oct 12, 2023
f934f84
wip: better error log
OzakIOne Oct 12, 2023
ea124d3
Merge branch 'main' into ozaki/mdx-unused-directives
slorber Oct 12, 2023
cefc3e4
prevent duplicate logging of unused directives in prod build
slorber Oct 12, 2023
f87a0b4
fix TS vfile datamap issue
slorber Oct 12, 2023
3ea1c74
wip: add text & leaf tests
OzakIOne Oct 12, 2023
6a0337a
fix: include lines in path
OzakIOne Oct 13, 2023
e4e7255
remark unused directive tests should handle compilerName option
slorber Oct 13, 2023
1b59e60
wip: improve log messages
OzakIOne Oct 13, 2023
b21537e
Merge branch 'main' into ozaki/mdx-unused-directives
slorber Oct 23, 2023
59cb00f
update unused directives snapshots
slorber Oct 23, 2023
61b41ae
refactor unused directives
slorber Oct 23, 2023
cb54192
refactor unused directives
slorber Oct 23, 2023
10d4057
refactor unused directives
slorber Oct 23, 2023
b777c29
remove annoying eslint rule
slorber Oct 23, 2023
a142e60
change format of warning
slorber Oct 23, 2023
23f6cb9
refactor
slorber Oct 23, 2023
55a0abe
Implement unused simple text directive re-serialization
slorber Oct 24, 2023
5fb34ae
use transformNode everywhere
slorber Oct 24, 2023
764e9ab
eslint
slorber Oct 24, 2023
bfa8a90
eslint
slorber Oct 24, 2023
99f83b2
fix bug + add client/server output mismatch tests
slorber Oct 24, 2023
a85f049
increase playwright timeout
slorber Oct 24, 2023
d7cf972
Merge branch 'main' into ozaki/mdx-unused-directives
slorber Oct 24, 2023
65406ac
ignore playwright path
slorber Oct 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implement unused simple text directive re-serialization
  • Loading branch information
slorber committed Oct 24, 2023
commit 55a0abee65074b1a91fcfec4c71be85cddd6f5b4
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ exports[`directives remark plugin - client compiler default behavior for leaf di
exports[`directives remark plugin - client compiler default behavior for text directives: console 1`] = `
[
[
"[WARNING] Docusaurus found 4 unused Markdown directives in file "packages/docusaurus-mdx-loader/src/remark/unusedDirectives/__tests__/__fixtures__/textDirectives.md"
- :textDirective2 (7:7)
"[WARNING] Docusaurus found 2 unused Markdown directives in file "packages/docusaurus-mdx-loader/src/remark/unusedDirectives/__tests__/__fixtures__/textDirectives.md"
- :textDirective3 (9:7)
- :textDirective4 (11:7)
- :textDirective5 (13:7)
Your content might render in an unexpected way. Visit https://github.com/facebook/docusaurus/pull/9394 to find out why and how to fix it.",
],
]
Expand All @@ -52,10 +50,10 @@ exports[`directives remark plugin - client compiler default behavior for text di
"<p>Simple: textDirective1</p>
<pre><code class="language-sh">Simple: textDirectiveCode
</code></pre>
<p>Simple<div></div></p>
<p>Simple:textDirective2</p>
<p>Simple<div>label</div></p>
<p>Simple<div></div></p>
<p>Simple<div></div></p>
<p>Simple:textDirective5</p>
<pre><code class="language-sh">Simple:textDirectiveCode
</code></pre>"
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import process from 'process';
import visit from 'unist-util-visit';
import logger from '@docusaurus/logger';
import {posixPath} from '@docusaurus/utils';
import {transformNode} from '../utils';

// @ts-expect-error: TODO see https://github.com/microsoft/TypeScript/issues/49721
import type {Transformer, Processor, Parent} from 'unified';
import type {
Expand Down Expand Up @@ -94,11 +96,24 @@ function isSimpleTextDirective(
const hasAttributes = Object.keys(directive.attributes ?? {}).length > 0;
// Children in MDAST = Directive label
const hasChildren = directive.children.length > 0;
return hasAttributes || hasChildren;
return !hasAttributes && !hasChildren;
}
return false;
}

function transformSimpleTextDirectiveToString(textDirective: Directive) {
transformNode(textDirective, {
type: 'text',
value: `:${textDirective.name}`, // We ignore label/props on purpose here
});
}

function isUnusedDirective(directive: Directive) {
// If directive data is set (notably hName/hProperties set by admonitions)
// this usually means the directive has been handled by another plugin
return !directive.data;
}

const plugin: Plugin = function plugin(this: Processor): Transformer {
return (tree, file) => {
// We only enable these warnings for the client compiler
Expand All @@ -113,10 +128,9 @@ const plugin: Plugin = function plugin(this: Processor): Transformer {
visit<Parent>(tree, directiveTypes, (directive: Directive) => {
// If directive data is set (notably hName/hProperties set by admonitions)
// this usually means the directive has been handled by another plugin
if (!directive.data) {
if (isUnusedDirective(directive)) {
if (isSimpleTextDirective(directive)) {
// TODO !
unusedDirectives.push(directive);
transformSimpleTextDirectiveToString(directive);
} else {
unusedDirectives.push(directive);
}
Expand Down
23 changes: 22 additions & 1 deletion packages/docusaurus-mdx-loader/src/remark/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import escapeHtml from 'escape-html';
import type {Parent} from 'unist';
import type {Parent, Node} from 'unist';
import type {PhrasingContent, Heading} from 'mdast';
import type {
MdxJsxAttribute,
Expand All @@ -15,6 +15,27 @@ import type {
// @ts-expect-error: TODO see https://github.com/microsoft/TypeScript/issues/49721
} from 'mdast-util-mdx';

/**
* Util to transform one node type to another node type
* The input node is mutated in place
* @param node the node to mutate
* @param newNode what the original node should become become
*/
export function transformNode<NewNode extends Node>(
node: Node,
newNode: NewNode,
): NewNode {
Object.keys(node).forEach((key) => {
// @ts-expect-error: unsafe but ok
delete node[key];
});
Object.keys(newNode).forEach((key) => {
// @ts-expect-error: unsafe but ok
node[key] = newNode[key];
});
return node as NewNode;
}

export function stringifyContent(
node: Parent,
toString: (param: unknown) => string, // TODO weird but works
Expand Down