mirror of
https://github.com/facebook/docusaurus.git
synced 2025-04-29 10:17:55 +02:00
fix(utils): handle CRLF when parsing MDX imports (#8606)
This commit is contained in:
parent
ece720b1a7
commit
2f02beebe2
2 changed files with 25 additions and 3 deletions
|
@ -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`
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Reference in a new issue