fix(v2): fix title logic (meta vs heading) + ignore fixed anchor id syntax (#4688)

* parseMarkdownContentTitle should ignore {#my-anchor-id} syntax

* use frontMatter.title in priority for page meta title

* parseMarkdownString should ignore fixed anchor ids syntax

* docs: make the distinction between headingTitle + metaTitle more clear + add useful todo

* docs: make the distinction between headingTitle + metaTitle more clear + add useful todo

* writeHeadingIds should ignore top-level md title like "# Title"
=> we are not supposed to create anchor links for h1 headers

* update tests

* fix doc tests
This commit is contained in:
Sébastien Lorber 2021-04-27 15:44:46 +02:00 committed by GitHub
parent bca796545b
commit 8ebbc17c7b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 124 additions and 77 deletions

View file

@ -86,7 +86,7 @@ export function parseMarkdownContentTitle(
const content = contentUntrimmed.trim();
const regularTitleMatch = /^(?:import\s.*(from.*)?;?|\n)*?(?<pattern>#\s*(?<title>[^#\n]*)+[ \t]*#?\n*?)/g.exec(
const regularTitleMatch = /^(?:import\s.*(from.*)?;?|\n)*?(?<pattern>#\s*(?<title>[^#\n{]*)+[ \t]*(?<suffix>({#*[\w-]+})|#)?\n*?)/g.exec(
content,
);
const alternateTitleMatch = /^(?:import\s.*(from.*)?;?|\n)*?(?<pattern>\s*(?<title>[^\n]*)\s*\n[=]+)/g.exec(
@ -120,12 +120,10 @@ type ParsedMarkdown = {
export function parseMarkdownString(
markdownFileContent: string,
options?: {
source?: string;
keepContentTitle?: boolean;
},
): ParsedMarkdown {
try {
const sourceOption = options?.source;
const keepContentTitle = options?.keepContentTitle ?? false;
const {frontMatter, content: contentWithoutFrontMatter} = parseFrontMatter(
@ -141,20 +139,6 @@ export function parseMarkdownString(
const excerpt = createExcerpt(content);
// TODO not sure this is a good place for this warning
if (
frontMatter.title &&
contentTitle &&
!keepContentTitle &&
!(process.env.DOCUSAURUS_NO_DUPLICATE_TITLE_WARNING === 'false')
) {
console.warn(
chalk.yellow(`Duplicate title found in ${sourceOption ?? 'this'} file.
Use either a frontmatter title or a markdown title, not both.
If this is annoying you, use env DOCUSAURUS_NO_DUPLICATE_TITLE_WARNING=false`),
);
}
return {
frontMatter,
content,
@ -175,7 +159,7 @@ export async function parseMarkdownFile(
): Promise<ParsedMarkdown> {
const markdownString = await fs.readFile(source, 'utf-8');
try {
return parseMarkdownString(markdownString, {source});
return parseMarkdownString(markdownString);
} catch (e) {
throw new Error(
`Error while parsing markdown file ${source}