fix: ignore code block lines when create excerpt (#5495)

This commit is contained in:
Alexey Pyltsyn 2021-09-22 11:15:46 +03:00 committed by GitHub
parent ebf81b6ef6
commit 578470a24c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 0 deletions

View file

@ -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', () => {

View file

@ -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, '')