fix(utils): handle CRLF when parsing MDX imports (#8606)

This commit is contained in:
Sébastien Castiel 2023-02-02 03:48:28 -05:00 committed by GitHub
parent ece720b1a7
commit 2f02beebe2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 3 deletions

View file

@ -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`

View file

@ -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;