Skip to content

Commit beb6b59

Browse files
authored
fix(rehypeHeaderAnchor): trim anchor for better format with React Component (#2318)
1 parent 8fb0751 commit beb6b59

File tree

5 files changed

+78
-3
lines changed

5 files changed

+78
-3
lines changed

packages/core/src/node/mdx/rehypePlugins/__snapshots__/headerAnchor.test.ts.snap

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,49 @@ MDXContent.__RSPRESS_PAGE_META = {};
203203
MDXContent.__RSPRESS_PAGE_META["inline-code.mdx"] = {"toc":[],"title":"Hello World \`inline code\`","headingTitle":"Hello World \`inline code\`","frontmatter":{}};
204204
"
205205
`;
206+
207+
exports[`rehypeHeadAnchor > should support mdx component with trim 1`] = `
208+
"const frontmatter = {};
209+
import {jsx as _jsx, jsxs as _jsxs} from "react/jsx-runtime";
210+
import {useMDXComponents as _provideComponents} from "@mdx-js/react";
211+
function _createMdxContent(props) {
212+
const _components = {
213+
a: "a",
214+
h1: "h1",
215+
..._provideComponents(),
216+
...props.components
217+
}, {Badge} = _components;
218+
if (!Badge) _missingMdxReference("Badge", true);
219+
return _jsxs(_components.h1, {
220+
id: "hello-world",
221+
children: [_jsx(_components.a, {
222+
className: "header-anchor",
223+
"aria-hidden": "true",
224+
href: "#hello-world",
225+
children: "#"
226+
}), "Hello World ", _jsx(Badge, {
227+
text: "WARNING"
228+
})]
229+
});
230+
}
231+
export default function MDXContent(props = {}) {
232+
const {wrapper: MDXLayout} = {
233+
..._provideComponents(),
234+
...props.components
235+
};
236+
return MDXLayout ? _jsx(MDXLayout, {
237+
...props,
238+
children: _jsx(_createMdxContent, {
239+
...props
240+
})
241+
}) : _createMdxContent(props);
242+
}
243+
function _missingMdxReference(id, component) {
244+
throw new Error("Expected " + (component ? "component" : "object") + " \`" + id + "\` to be defined: you likely forgot to import, pass, or provide it.");
245+
}
246+
247+
MDXContent.__RSPRESS_PAGE_META = {};
248+
249+
MDXContent.__RSPRESS_PAGE_META["inline-code.mdx"] = {"toc":[],"title":"Hello World ","headingTitle":"Hello World ","frontmatter":{}};
250+
"
251+
`;

packages/core/src/node/mdx/rehypePlugins/headerAnchor.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,35 @@ describe('rehypeHeadAnchor', () => {
6868
const resultWithoutBackslash = await compile({
6969
source: `
7070
# Hello World \`inline code\` {#custom-id}
71+
`,
72+
checkDeadLinks: false,
73+
docDirectory: '/usr/rspress-project/docs',
74+
filepath: '/usr/rspress-project/docs/inline-code.mdx',
75+
config: null,
76+
pluginDriver: null,
77+
routeService: null,
78+
});
79+
expect(resultWithoutBackslash).toEqual(result);
80+
expect(result).toMatchSnapshot();
81+
});
82+
83+
it('should support mdx component with trim', async () => {
84+
const result = await compile({
85+
source: `
86+
# Hello World <Badge text={"WARNING"} />
87+
`,
88+
checkDeadLinks: false,
89+
docDirectory: '/usr/rspress-project/docs',
90+
filepath: '/usr/rspress-project/docs/inline-code.mdx',
91+
config: null,
92+
pluginDriver: null,
93+
routeService: null,
94+
});
95+
96+
// This is actually a wrong usage, but we need to be compatible.
97+
const resultWithoutBackslash = await compile({
98+
source: `
99+
# Hello World <Badge text={"WARNING"} />
71100
`,
72101
checkDeadLinks: false,
73102
docDirectory: '/usr/rspress-project/docs',

packages/core/src/node/mdx/rehypePlugins/headerAnchor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const rehypeHeaderAnchor: Plugin<[], Root> = () => {
2121
if (!node.properties?.id) {
2222
const [text, customId] = collectHeaderText(node);
2323
node.properties ??= {};
24-
node.properties.id = customId || slugger.slug(text);
24+
node.properties.id = customId || slugger.slug(text.trim());
2525
}
2626
// apply to headings
2727
node.children.unshift(create(node));

packages/core/src/node/mdx/remarkPlugins/toc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export const parseToc = (tree: Root) => {
6464
if (node.depth === 1) {
6565
if (!title) title = text;
6666
} else {
67-
const id = customId ? customId : slugger.slug(text);
67+
const id = customId ? customId : slugger.slug(text.trim());
6868
const { depth } = node;
6969
toc.push({ id, text, depth });
7070
}

packages/plugin-api-docgen/static/global-components/API.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const rehypeHeaderAnchor: Plugin<[], Root> = () => {
3636
if (!node.properties?.id) {
3737
const text = collectHeaderText(node);
3838
node.properties ??= {};
39-
node.properties.id = slugger.slug(text);
39+
node.properties.id = slugger.slug(text.trim());
4040
}
4141
// apply to headings
4242
node.children.unshift(create(node));

0 commit comments

Comments
 (0)