Skip to content

Commit

Permalink
fix(utils): handle CRLF when parsing MDX imports (#8606)
Browse files Browse the repository at this point in the history
  • Loading branch information
scastiel authored and slorber committed Feb 2, 2023
1 parent 4761c8c commit 2bdd27a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
20 changes: 20 additions & 0 deletions packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts
Expand Up @@ -130,6 +130,26 @@ describe('createExcerpt', () => {
);
});

it('creates excerpt for content with imports/exports declarations, with CRLF line endings', () => {
expect(
createExcerpt(
dedent`
import Component from '@site/src/components/Component';
export function ItemCol(props) {
return <Item {...props} className={'col col--6 margin-bottom--lg'}/>
}
Lorem **ipsum** dolor sit \`amet\`[^1], consectetur _adipiscing_ elit. [**Vestibulum**](https://wiktionary.org/wiki/vestibulum) ex urna[^note], ~~molestie~~ et sagittis ut, varius ac justo :wink:.
Nunc porttitor libero nec vulputate venenatis. Nam nec rhoncus mauris. Morbi tempus est et nibh maximus, tempus venenatis arcu lobortis.
`.replace(/\n/g, '\r\n'),
),
).toBe(
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ex urna, molestie et sagittis ut, varius ac justo.',
);
});

it('creates excerpt for heading specified with anchor-id syntax', () => {
expect(
createExcerpt(dedent`
Expand Down
8 changes: 5 additions & 3 deletions packages/docusaurus-utils/src/markdownUtils.ts
Expand Up @@ -55,16 +55,18 @@ export function createExcerpt(fileString: string): string | undefined {
const fileLines = fileString
.trimStart()
// Remove Markdown alternate title
.replace(/^[^\n]*\n[=]+/g, '')
.split('\n');
.replace(/^[^\r\n]*\r?\n[=]+/g, '')
.split(/\r?\n/);
let inCode = false;
let inImport = false;
let lastCodeFence = '';

for (const fileLine of fileLines) {
if (fileLine === '' && inImport) {
// An empty line marks the end of imports
if (!fileLine.trim() && inImport) {
inImport = false;
}

// Skip empty line.
if (!fileLine.trim()) {
continue;
Expand Down

0 comments on commit 2bdd27a

Please sign in to comment.