mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-10 15:47:23 +02:00
fix(v2): ignore import declarations in excerpt (#2380)
* fix(v2): ignore import declarations in excerpt * Update index.ts Co-authored-by: Yangshun Tay <tay.yang.shun@gmail.com>
This commit is contained in:
parent
956d701cf5
commit
84fa4be865
1 changed files with 15 additions and 1 deletions
|
@ -182,6 +182,9 @@ export function getSubFolder(file: string, refDir: string): string | null {
|
|||
return match && match[1];
|
||||
}
|
||||
|
||||
// Regex for an import statement.
|
||||
const importRegexString = '^(.*import){1}(.+){0,1}\\s[\'"](.+)[\'"];';
|
||||
|
||||
export function parse(
|
||||
fileString: string,
|
||||
): {
|
||||
|
@ -193,7 +196,18 @@ export function parse(
|
|||
} {
|
||||
const options: {} = {
|
||||
excerpt: (file: matter.GrayMatterFile<string>): void => {
|
||||
file.excerpt = file.content.trim().split('\n', 1).shift();
|
||||
let fileContent = file.content.trimLeft();
|
||||
|
||||
// Hacky way of stripping out import statements from the excerpt
|
||||
// TODO: Find a better way to do so, possibly by compiling the Markdown content,
|
||||
// stripping out HTML tags and obtaining the first line.
|
||||
if (RegExp(importRegexString).test(fileContent)) {
|
||||
fileContent = fileContent
|
||||
.replace(RegExp(importRegexString, 'gm'), '')
|
||||
.trimLeft();
|
||||
}
|
||||
|
||||
file.excerpt = fileContent.split('\n', 1).shift();
|
||||
},
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue