mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-12 00:27:21 +02:00
fix: ignore code block lines when create excerpt (#5495)
This commit is contained in:
parent
ebf81b6ef6
commit
578470a24c
2 changed files with 22 additions and 0 deletions
|
@ -129,6 +129,19 @@ describe('createExcerpt', () => {
|
|||
`),
|
||||
).toEqual('Markdown title');
|
||||
});
|
||||
|
||||
test('should create excerpt for content with various code blocks', () => {
|
||||
expect(
|
||||
createExcerpt(dedent`
|
||||
\`\`\`jsx
|
||||
import React from 'react';
|
||||
import Layout from '@theme/Layout';
|
||||
\`\`\`
|
||||
|
||||
Lorem \`ipsum\` dolor sit amet, consectetur \`adipiscing elit\`.
|
||||
`),
|
||||
).toEqual('Lorem ipsum dolor sit amet, consectetur adipiscing elit.');
|
||||
});
|
||||
});
|
||||
|
||||
describe('parseMarkdownContentTitle', () => {
|
||||
|
|
|
@ -18,6 +18,7 @@ export function createExcerpt(fileString: string): string | undefined {
|
|||
// Remove Markdown alternate title
|
||||
.replace(/^[^\n]*\n[=]+/g, '')
|
||||
.split('\n');
|
||||
let inCode = false;
|
||||
|
||||
/* eslint-disable no-continue */
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
|
@ -32,6 +33,14 @@ export function createExcerpt(fileString: string): string | undefined {
|
|||
continue;
|
||||
}
|
||||
|
||||
// Skip code block line.
|
||||
if (fileLine.trim().startsWith('```')) {
|
||||
inCode = !inCode;
|
||||
continue;
|
||||
} else if (inCode) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const cleanedLine = fileLine
|
||||
// Remove HTML tags.
|
||||
.replace(/<[^>]*>/g, '')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue