Skip to content

feat(v2): frontmatter-less: read first heading as title and use it in front-matter - #4485

Merged
slorber merged 9 commits into
meta:masterfrom
armano2:feat/frontmatter-title
Mar 23, 2021
Merged

slorber merged 9 commits into
meta:masterfrom
armano2:feat/frontmatter-title

Conversation

@armano2

@armano2 armano2 commented Mar 21, 2021 •

Copy link
Copy Markdown
Contributor

Motivation

frontmatter always win
if no frontmatter and an h1 heading is at the very top, we use it as title instead of the filename
if no frontmatter and an h1 heading is found in the middle of a doc, we ignore it
#4425 (comment)

if front-matter title is present 
  use front-matter title
else if try reading header
  use header as front-matter title and conditionally remove it from content
else 
  old behavior

if front-matter title and header is present
  use front-matter title and print warning

typescript-eslint/typescript-eslint#3147
fixes #4425

Have you read the Contributing Guidelines on pull requests?

yes

Test Plan

i'm unsure where to put unit tests for this as i can't find any other unit tests for utils

  • /api/CLI should correctly read heading and use it as title
  • any other doc should read title field correctly
  • blog post should correctly print headings when # or title field is used
  • /examples/markdownPageExample should print heading for markdown page

content can/should be modified by hand to see if data is printed correctly.

@armano2
armano2 requested review from lex111 and slorber as code owners March 21, 2021 14:51
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Mar 21, 2021
@netlify

netlify Bot commented Mar 21, 2021 •

Copy link
Copy Markdown

@netlify

netlify Bot commented Mar 21, 2021 •

Copy link
Copy Markdown

@github-actions

github-actions Bot commented Mar 21, 2021 •

Copy link
Copy Markdown

⚡️ Lighthouse report for the changes in this PR:

Category Score
🟠 Performance 87
🟢 Accessibility 96
🟢 Best practices 100
🟢 SEO 100
🟢 PWA 95

Lighthouse ran on https://deploy-preview-4485--docusaurus-2.netlify.app/

Comment thread packages/docusaurus-utils/src/index.ts Resolved
@idontknowjs

Copy link
Copy Markdown
Contributor

hey @armano2 , I'm a Docusaurus user. This feature sounds cool, will surely align to my usecase
By default when title is present in front-matter, it is also passed to sidebar_label and the name of the sidebar label gets changed to title.

In your case, will it happen too that the sidebar_label will also change as per the first heading along with the title?

@armano2

armano2 commented Mar 22, 2021 •

Copy link
Copy Markdown
Contributor Author

yes, that's a default behavior, if no sidebar label is present title is used instead, in /api/CLI you can actually see that happening

@armano2 armano2 changed the title feat(v2): read first heading as title and pass it to front-matter feat(v2): read first heading as title and pass it as front-matter Mar 22, 2021
@armano2

armano2 commented Mar 22, 2021

Copy link
Copy Markdown
Contributor Author

tests added to blog and docs

@armano2
armano2 force-pushed the feat/frontmatter-title branch from e3f6cd5 to be80201 Compare March 22, 2021 10:34
Comment thread website/docs/cli.md
Comment on lines 3 to +5
---

# CLI

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i can rollback this md file change as proper tests are now added

Suggested change
---
# CLI
title: CLI
---

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I think it's fine and we should dogfood this a bit too ;)

@armano2 armano2 changed the title feat(v2): read first heading as title and pass it as front-matter feat(v2): read first heading as title and use it as front-matter Mar 22, 2021
@armano2 armano2 changed the title feat(v2): read first heading as title and use it as front-matter feat(v2): read first heading as title and use it as front-matter title Mar 22, 2021
@armano2 armano2 changed the title feat(v2): read first heading as title and use it as front-matter title feat(v2): read first heading as title and use it as front-matter Mar 22, 2021
@armano2 armano2 changed the title feat(v2): read first heading as title and use it as front-matter feat(v2): read first heading as title and use it in front-matter Mar 22, 2021
@slorber

slorber commented Mar 22, 2021

Copy link
Copy Markdown
Collaborator

That looks nice, thanks for working on this! Will review this more deeply tomorrow.

Was wondering, do you think we should handle pages like docs and blog posts and automatically add the h1 tag to templates? It looks like we have a non-uniform setup that complicates a bit the code as pages should not remove the markdown heading.

I'd also like to have more tests for readFrontMatter and its edge cases, do you want to write some or can I edit your PR?

Comment on lines +276 to +288
const heading = /^# (.*)[\n\r]?/gi.exec(result.content);
if (heading) {
if (result.data.title) {
console.warn(
`Duplicate title detected in \`${source || 'this'}\` file`,
);
} else {
result.data.title = heading[1].trim();
if (removeTitleHeading) {
result.content = result.content.replace(heading[0], '');
if (result.excerpt) {
result.excerpt = result.excerpt.replace(heading[1], '');
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wonder if we could extract this easily in a separate function and test this in isolation?

@armano2 armano2 Mar 22, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could actually go few steps further in separate pr

index should jsut be an entry point with bunch of reexports and we could create multiple files with specific things.
this file is starting to become really long and unreadable

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

totally agree :)

was like that before and want to refactor that for a long time already

@armano2

armano2 commented Mar 22, 2021 •

Copy link
Copy Markdown
Contributor Author

Was wondering, do you think we should handle pages like docs and blog posts and automatically add the h1 tag to templates? It looks like we have a non-uniform setup that complicates a bit the code as pages should not remove the markdown heading.

this could be done but not in this PR, as we should not include to much changes at once

I'd also like to have more tests for readFrontMatter and its edge cases, do you want to write some or can I edit your PR?

i will try to add some tests, I'm not really familiar with convention in this project yet, as each project likes to do tests in different way
i added more test cases


circleci is failing after merge with master, same issue is preset on master

@slorber

slorber commented Mar 23, 2021

Copy link
Copy Markdown
Collaborator

Thanks

circleci is failing after merge with master, same issue is preset on master

yes we just removed CircleCI, will be fine now :)

@slorber slorber left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! that looks great

Comment on lines +276 to +288
const heading = /^# (.*)[\n\r]?/gi.exec(result.content);
if (heading) {
if (result.data.title) {
console.warn(
`Duplicate title detected in \`${source || 'this'}\` file`,
);
} else {
result.data.title = heading[1].trim();
if (removeTitleHeading) {
result.content = result.content.replace(heading[0], '');
if (result.excerpt) {
result.excerpt = result.excerpt.replace(heading[1], '');
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

totally agree :)

was like that before and want to refactor that for a long time already

@armano2

armano2 commented Mar 23, 2021

Copy link
Copy Markdown
Contributor Author

i just realized that this behavior is undocumented, maybe we should add it to docs?

@slorber

slorber commented Mar 23, 2021

Copy link
Copy Markdown
Collaborator

oh yes that could be nice thanks :)

We have a good place for that: https://docusaurus.io/docs/next/markdown-features/headings

@slorber

slorber commented Apr 9, 2021

Copy link
Copy Markdown
Collaborator

FYI I don't think it's a good idea to put the content title in the frontmatter object, it's a bit confusing because indeed it's not frontmatter.

Just wanted to let you know in case you depend on frontMatter.title being available despite not being set by real frontmatter.

Will revert this in #4590

@slorber slorber changed the title feat(v2): read first heading as title and use it in front-matter feat(v2): frontmatter-less: read first heading as title and use it in front-matter Apr 16, 2021
@slorber slorber added the pr: new feature This PR adds a new API or behavior. label Apr 16, 2021
@Sotty75

Sotty75 commented Apr 22, 2021 •

Copy link
Copy Markdown

I have reported a bug on how titles are handled after updating to latest stable version.
This worked as a breaking change for me, also now all level 2 headers placed at the top of the document are rendered as standard text, not properly formatted. #4665

I added an example of how this is broken, check the following link, intro.md

https://codesandbox.io/s/vigorous-wood-d2cz7?file=/docs/intro.md

@slorber

slorber commented Apr 22, 2021 •

Copy link
Copy Markdown
Collaborator

I believe it is a bug fix, not a breaking change, as we should not have allowed you to write a h1 title in frontmatter + markdown, and it is something to avoid

We didn't expect this would be annoying for anyone because it is a weird thing to do in the first place (or I'd like to know your motivations)

mrizwanashiq pushed a commit to mrizwanashiq/docusaurus that referenced this pull request Jun 25, 2026
…a#4485)

* feat(v2): read first heading as title and pass it to front-matter

* fix(v2): always trim content after extracting front-matter

* fix(v2): remove heading from rss and keep duplicate heading

* fix(v2): rollback some unnecessary comment changes

* test(v2): add unit tests to blog

* test(v2): add unit tests to docs

* test(v2): correct issue on windows

* test(v2): add additional test cases
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Signed Facebook CLA pr: new feature This PR adds a new API or behavior.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support docs without frontmatter

5 participants