From 84fa4be865158425fe8805011b2e946bc88121d6 Mon Sep 17 00:00:00 2001 From: Alexey Pyltsyn Date: Wed, 22 Apr 2020 15:28:14 +0300 Subject: [PATCH] fix(v2): ignore import declarations in excerpt (#2380) * fix(v2): ignore import declarations in excerpt * Update index.ts Co-authored-by: Yangshun Tay --- packages/docusaurus-utils/src/index.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/docusaurus-utils/src/index.ts b/packages/docusaurus-utils/src/index.ts index 80bddc33e2..695b4d6788 100644 --- a/packages/docusaurus-utils/src/index.ts +++ b/packages/docusaurus-utils/src/index.ts @@ -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): 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(); }, };