diff --git a/packages/docusaurus-utils/src/index.ts b/packages/docusaurus-utils/src/index.ts index 80bddc33e224..695b4d67886c 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(); }, };