Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 9 commits into from
Mar 23, 2021

Conversation

armano2
Copy link
Contributor

@armano2 armano2 commented Mar 21, 2021

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.

@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Mar 21, 2021
@netlify
Copy link

netlify bot commented Mar 21, 2021

@netlify
Copy link

netlify bot commented Mar 21, 2021

Deploy preview for docusaurus-2 ready!

Built without sensitive environment variables with commit c4c002b

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

@github-actions
Copy link

github-actions bot commented Mar 21, 2021

⚡️ 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/

@idontknowjs
Copy link
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
Copy link
Contributor Author

armano2 commented Mar 22, 2021

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
Copy link
Contributor Author

armano2 commented Mar 22, 2021

tests added to blog and docs

Comment on lines 3 to +5
---

# CLI
Copy link
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
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
Copy link
Collaborator

slorber commented Mar 22, 2021

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
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?

Copy link
Contributor Author

@armano2 armano2 Mar 22, 2021

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
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
Copy link
Contributor Author

armano2 commented Mar 22, 2021

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
Copy link
Collaborator

slorber commented Mar 23, 2021

Thanks

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

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

Copy link
Collaborator

@slorber slorber left a comment

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
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
Copy link
Contributor Author

armano2 commented Mar 23, 2021

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

@slorber
Copy link
Collaborator

slorber commented Mar 23, 2021

oh yes that could be nice thanks :)

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

@slorber
Copy link
Collaborator

slorber commented Apr 9, 2021

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
Copy link

Sotty75 commented Apr 22, 2021

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
Copy link
Collaborator

slorber commented Apr 22, 2021

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)

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