mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-01 11:18:24 +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', () => {
|
it('creates excerpt for heading specified with anchor-id syntax', () => {
|
||||||
expect(
|
expect(
|
||||||
createExcerpt(dedent`
|
createExcerpt(dedent`
|
||||||
|
|
|
@ -55,16 +55,18 @@ export function createExcerpt(fileString: string): string | undefined {
|
||||||
const fileLines = fileString
|
const fileLines = fileString
|
||||||
.trimStart()
|
.trimStart()
|
||||||
// Remove Markdown alternate title
|
// Remove Markdown alternate title
|
||||||
.replace(/^[^\n]*\n[=]+/g, '')
|
.replace(/^[^\r\n]*\r?\n[=]+/g, '')
|
||||||
.split('\n');
|
.split(/\r?\n/);
|
||||||
let inCode = false;
|
let inCode = false;
|
||||||
let inImport = false;
|
let inImport = false;
|
||||||
let lastCodeFence = '';
|
let lastCodeFence = '';
|
||||||
|
|
||||||
for (const fileLine of fileLines) {
|
for (const fileLine of fileLines) {
|
||||||
if (fileLine === '' && inImport) {
|
// An empty line marks the end of imports
|
||||||
|
if (!fileLine.trim() && inImport) {
|
||||||
inImport = false;
|
inImport = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skip empty line.
|
// Skip empty line.
|
||||||
if (!fileLine.trim()) {
|
if (!fileLine.trim()) {
|
||||||
continue;
|
continue;
|
||||||
|
|
Loading…
Add table
Reference in a new issue