Skip to content

Commit

Permalink
fix(docusaurus-utils): Handle CRLF EOL when parsing MDX with imports
Browse files Browse the repository at this point in the history
  • Loading branch information
scastiel committed Jan 31, 2023
1 parent d184e84 commit 3394bba
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
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
4 changes: 3 additions & 1 deletion packages/docusaurus-utils/src/markdownUtils.ts
Expand Up @@ -62,9 +62,11 @@ export function createExcerpt(fileString: string): string | undefined {
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 3394bba

Please sign in to comment.