fix(utils): strip MDX export blocks before heading detection in parseMarkdownContentTitle - #12332
fix(utils): strip MDX export blocks before heading detection in parseMarkdownContentTitle#12332okxint wants to merge 1 commit into
Conversation
…MarkdownContentTitle
|
Hi @okxint! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
✅ [V2]Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
There was a problem hiding this comment.
(Note, I'm not a maintainer, just the author of the original issue. I'm weighting in with my review but when another maintainer chimes in, their feedback should get precedence.)
The fix itself looks acceptable to me.
In the JSDoc it explicitly stated before that the function "[ignores] import/export declarations", which wasn't true before. This PR is what makes the documentation accurate!
I'd expect to see at least one new test cases added too in packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts:
it('parses markdown h1 title placed after export declarations and remove it', () => {
const markdown = dedent`
export function Uwu() {
return <>uwu</>;
}
# Markdown Title
<Uwu/> Ipsum
`;
expect(
parseMarkdownContentTitle(markdown, {removeContentTitle: true}),
).toEqual({
content: markdown.replace('# Markdown Title\n', ''),
contentTitle: 'Markdown Title',
});
});While reviewing locally, I had added this test case as well, but I see it would fail as the title parsing won't actually render any JSX that may appear in headings. The title is set to <Uwu/> Title literally. As the doc says, the function "won't be best effort", so in this PR at least, I don't think any changes to fix this would be worthwhile. Just making note of it and leaving the test case for future reference. I personally don't have a use case to put JSX in headings anyway, so happy to pretend I didn't see that.
it('parses markdown h1 title placed after multiple export declarations used in h1 and and remove it', () => {
const markdown = dedent`
export function Uwu() {
return <>uwu</>;
};
# <Uwu/> Title
Lorem Ipsum
`;
expect(
parseMarkdownContentTitle(markdown, {removeContentTitle: true}),
).toEqual({
content: markdown.replace('# <Uwu/> Title\n', ''),
contentTitle: 'uwu Title',
});
});
parseMarkdownContentTitlestrips leading MDXimportblocks via a regex before looking for# h1, but does not stripexportdeclarations (e.g.export function Author() {...}orexport const meta = ...). When an MDX file has anexportbefore the heading, the stripped content still starts withexport ..., the title regex finds no match at position 0, and Docusaurus falls back to the site name for<title>and the sidebar.Fix: add
exportas an alternative alongsideimportin the existing regex. The change is minimal —import\sbecomes(?:import|export)\s— and the existing import-stripping behaviour is preserved exactly.Fixes #12331