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:
Alexey Pyltsyn 2020-04-22 15:28:14 +03:00 committed by GitHub
parent 956d701cf5
commit 84fa4be865
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -182,6 +182,9 @@ export function getSubFolder(file: string, refDir: string): string | null {
return match && match[1]; return match && match[1];
} }
// Regex for an import statement.
const importRegexString = '^(.*import){1}(.+){0,1}\\s[\'"](.+)[\'"];';
export function parse( export function parse(
fileString: string, fileString: string,
): { ): {
@ -193,7 +196,18 @@ export function parse(
} { } {
const options: {} = { const options: {} = {
excerpt: (file: matter.GrayMatterFile<string>): void => { 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();
}, },
}; };