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(eslint-plugin): new prefer-docusaurus-heading rule #8384

Merged
merged 8 commits into from Jan 19, 2023

Conversation

Devansu-Yadav
Copy link
Contributor

@Devansu-Yadav Devansu-Yadav commented Nov 28, 2022

Pre-flight checklist

  • I have read the Contributing Guidelines on pull requests.
  • If this is a code change: I have written unit tests and/or added dogfooding pages to fully verify the new behavior.
  • If this is a new API or substantial change: the PR has an accompanying issue (closes #0000) and the maintainers have approved on my working plan.

Motivation

Described in #6472. Basically, this plugin enforces the use of @theme/Heading component instead of <h2> tags.

Test Plan

Added Jest unit tests for this plugin.

Test links

Deploy preview: https://deploy-preview-8384--docusaurus-2.netlify.app/

Related issues/PRs

#6472

Comments

@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Nov 28, 2022
@netlify
Copy link

netlify bot commented Nov 28, 2022

[V2]

Built without sensitive environment variables

Name Link
🔨 Latest commit 47c9b7a
🔍 Latest deploy log https://app.netlify.com/sites/docusaurus-2/deploys/63c9230407d46200089ad22a
😎 Deploy Preview https://deploy-preview-8384--docusaurus-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@github-actions
Copy link

github-actions bot commented Nov 28, 2022

⚡️ Lighthouse report for the deploy preview of this PR

URL Performance Accessibility Best Practices SEO PWA Report
/ 🟠 67 🟢 97 🟢 92 🟢 100 🟢 90 Report
/docs/installation 🟠 67 🟢 100 🟢 92 🟢 100 🟢 90 Report

@slorber
Copy link
Collaborator

slorber commented Dec 7, 2022

@Josh-Cena no strong opinion but what's the motivation to force users to use Heading instead of h2?

That looks like a valid thing to do to use a custom h2 (or any other level) in some cases.

Is there a reason to handle h2 in a different way than any other heading level?


Not against this rule but we probably need to think a bit deeper about what we are trying to prevent here, and also what should be the default experience.

Note @theme/Heading is a theme component, and it may technically not exist on a user site (although it's unlikely to happen in practice)

@Josh-Cena
Copy link
Collaborator

Josh-Cena commented Dec 7, 2022

@slorber @theme/Heading has anchor links, and can help us build link graphs that are section-title-aware. It also leads to a more uniform UX in general. See for example: typescript-eslint/typescript-eslint#5951

I do think this should not only apply to h2 though, but to all hn headings.

@Devansu-Yadav
Copy link
Contributor Author

Devansu-Yadav commented Dec 8, 2022

@slorber @theme/Heading has anchor links, and can help us build link graphs that are section-title-aware. It also leads to a more uniform UX in general. See for example: typescript-eslint/typescript-eslint#5951

I do think this should not only apply to h2 though, but to all hn headings.

@Josh-Cena @slorber I agree that we should have this rule apply to all possible headings and in fact, I have already added an option includeAllHeadings that checks against all hn headings. Do you reckon this should be the default behaviour of this rule?

PS: Just a heads up, I'll be able to continue my work on this PR after the 19th Dec, as I'd be unavailable due to my ongoing University exams 😅

@slorber
Copy link
Collaborator

slorber commented Dec 8, 2022

We should probably not use and special edge case for h2, and just apply this to all headings by default.

Eventually pass a list of headings to check as an option, but I really wonder who would use it 🤔

Do we want to make a special case for h1 @Josh-Cena ? In this case there's no anchor/id.

@Devansu-Yadav
Copy link
Contributor Author

Devansu-Yadav commented Dec 20, 2022

We should probably not use and special edge case for h2, and just apply this to all headings by default.

Eventually pass a list of headings to check as an option, but I really wonder who would use it 🤔

Do we want to make a special case for h1 @Josh-Cena ? In this case there's no anchor/id.

@Josh-Cena @slorber Any updates on how I should proceed with this? What I mean is that, do we need to have any special cases for h1 or h2 in this rule?

@Josh-Cena Josh-Cena changed the title Add eslint plugin prefer-docusaurus-heading feat(eslint-plugin): new prefer-docusaurus-heading rule Dec 21, 2022
@Josh-Cena Josh-Cena added the pr: new feature This PR adds a new API or behavior. label Dec 21, 2022
@slorber
Copy link
Collaborator

slorber commented Dec 22, 2022

I think we can move on and apply the rule for all hX elements 👍

We'll add more options later if needed, once people come with a good use-case to explain why an option is needed

@Devansu-Yadav
Copy link
Contributor Author

Devansu-Yadav commented Dec 31, 2022

I think we can move on and apply the rule for all hX elements 👍

We'll add more options later if needed, once people come with a good use-case to explain why an option is needed

@slorber I have implemented the eslint rule without any options for now. For fixing all the eslint errors introduced due to this new rule, I have made use of Heading defined in @theme/Heading module. Is this correct or should I be directly using the Heading component?

I know the above question might be a bit naive, but from what I understand after digging through source code, all of the @theme and @docusaurus modules or aliases are linked with their corresponding React component implementations and bundled using Webpack to load them in our React apps.

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.

Almost LGTM thanks 👍

Some changes requested in comments

@Josh-Cena let me know if you want to review this

@@ -53,6 +53,7 @@ For more fine-grained control, you can also enable the plugin manually and confi
| [`@docusaurus/no-untranslated-text`](./no-untranslated-text.mdx) | Enforce text labels in JSX to be wrapped by translate calls | |
| [`@docusaurus/string-literal-i18n-messages`](./string-literal-i18n-messages.mdx) | Enforce translate APIs to be called on plain text labels | ✅ |
| [`@docusaurus/no-html-links`](./no-html-links.mdx) | Ensures @docusaurus/Link is used instead of `<a>` tags | ✅ |
| [`@docusaurus/prefer-docusaurus-heading`](./prefer-docusaurus-heading.mdx) | Ensures @theme/Heading is used instead of `<hn>` tags for headings | ✅ |
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe prefer-theme-heading would be a more accurate rule name? (also maybe more confusing name?)

Any opinion @Josh-Cena ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I agree prefer-theme-heading sounds more accurate but doesn't docusaurus provide different themes containing the Heading component?

Copy link
Collaborator

Choose a reason for hiding this comment

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

docusaurus core doesn't provide any heading for now, only the theme does.

But maybe we could have a core heading component later if we want to create a graph of pages + their respective headings or whatever. We can keep this name for now, it doesn't matter much 😅

website/src/pages/versions.tsx Show resolved Hide resolved
.eslintrc.js Show resolved Hide resolved
@Devansu-Yadav Devansu-Yadav requested review from slorber and removed request for lex111 and Josh-Cena January 8, 2023 12:02
@Devansu-Yadav
Copy link
Contributor Author

Almost LGTM thanks 👍

Some changes requested in comments

@Josh-Cena let me know if you want to review this

@Josh-Cena @slorber Any updates from your end on the changes that I have made recently in this PR?

Signed-off-by: Devansu <devansuyadav@gmail.com>
Signed-off-by: Devansu <devansuyadav@gmail.com>
Signed-off-by: Devansu <devansuyadav@gmail.com>
Signed-off-by: Devansu <devansuyadav@gmail.com>
Signed-off-by: Devansu <devansuyadav@gmail.com>
Signed-off-by: Devansu <devansuyadav@gmail.com>
Signed-off-by: Devansu <devansuyadav@gmail.com>
Signed-off-by: Devansu <devansuyadav@gmail.com>
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, LGTM 👍

@@ -53,6 +53,7 @@ For more fine-grained control, you can also enable the plugin manually and confi
| [`@docusaurus/no-untranslated-text`](./no-untranslated-text.mdx) | Enforce text labels in JSX to be wrapped by translate calls | |
| [`@docusaurus/string-literal-i18n-messages`](./string-literal-i18n-messages.mdx) | Enforce translate APIs to be called on plain text labels | ✅ |
| [`@docusaurus/no-html-links`](./no-html-links.mdx) | Ensures @docusaurus/Link is used instead of `<a>` tags | ✅ |
| [`@docusaurus/prefer-docusaurus-heading`](./prefer-docusaurus-heading.mdx) | Ensures @theme/Heading is used instead of `<hn>` tags for headings | ✅ |
Copy link
Collaborator

Choose a reason for hiding this comment

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

docusaurus core doesn't provide any heading for now, only the theme does.

But maybe we could have a core heading component later if we want to create a graph of pages + their respective headings or whatever. We can keep this name for now, it doesn't matter much 😅

@slorber slorber added the to backport This PR is planned to be backported to a stable version of Docusaurus label Jan 19, 2023
@slorber slorber merged commit 90e7e32 into facebook:main Jan 19, 2023
@slorber slorber removed the to backport This PR is planned to be backported to a stable version of Docusaurus label Jan 26, 2023
This was referenced Oct 19, 2023
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.

None yet

4 participants