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

fix(theme): make Prism code block language / additionalLanguages case insensitive #9183

Merged
merged 11 commits into from
Aug 10, 2023

Conversation

heysujal
Copy link
Contributor

@heysujal heysujal commented Jul 28, 2023

Pre-flight checklist

Motivation

fix #9012

If someone uses a language that is not supported by Docusaurus by default then you have to add that language in additionalLanguages[] in docusaurus.config.js. If a language added is not in lowercase, then highlighting for that language will not work. For example
Adding 'PHP' or 'pHp' instead of 'php'

Test Plan

  1. Good : additionalLanguages: ['php'],

image

  1. Bad : additionalLanguages: ['PHP'],

image

  1. Bad : additionalLanguages: ['pHp'],

image

Test links

https://deploy-preview-9183--docusaurus-2.netlify.app/tests/pages/code-block-tests/#code-block-prism-language-tests

Related issues/PRs

#9012

heysujal and others added 3 commits July 28, 2023 13:15
Signed-off-by: Sujal Gupta <sujalgupta6100@gmail.com>
Signed-off-by: Sujal Gupta <sujalgupta6100@gmail.com>
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Jul 28, 2023
@netlify
Copy link

netlify bot commented Jul 28, 2023

[V2]

Name Link
🔨 Latest commit 5420c57
🔍 Latest deploy log https://app.netlify.com/sites/docusaurus-2/deploys/64d501719ba5de000713a5db
😎 Deploy Preview https://deploy-preview-9183--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 configuration.

@github-actions
Copy link

github-actions bot commented Jul 28, 2023

⚡️ Lighthouse report for the deploy preview of this PR

URL Performance Accessibility Best Practices SEO PWA Report
/ 🟠 57 🟢 97 🟢 92 🟢 100 🟠 89 Report
/docs/installation 🟠 71 🟢 100 🟢 92 🟢 100 🟠 89 Report

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

You can keep phP or something like that in the additional languages of this PR so that we can make PHP work in the preview and be able to validate the PR without running local modifications. We are tracking screenshots and do visual regression tests of our pages now so that's a good way for us to ensure that this feature keeps working over time if we actually use it on our site.

Currently there's no highlighting there:
https://deploy-preview-9183--docusaurus-2.netlify.app/tests/pages/code-block-tests/#code-block-withwithout-the-good-prism-language-case-in-additionallanguages-tests

website/_dogfooding/_pages tests/code-block-tests.mdx Outdated Show resolved Hide resolved
@@ -27,6 +27,21 @@ See:
- https://github.com/facebook/docusaurus/pull/3749
- https://github.com/facebook/docusaurus/pull/6177

## Code block with/without the good prism language case in additionalLanguages[] tests

```php
Copy link
Collaborator

Choose a reason for hiding this comment

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

that would be nice to add 2 other cases like PHP and PhP there to ensure we also support case insensitive language metastring

Copy link
Contributor Author

@heysujal heysujal Jul 28, 2023

Choose a reason for hiding this comment

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

Well, i already tried doing that before making a pr.
But only php code block works and other like you mentioned doesn't highlight code.
I thought our only goal was to allow the user to put a language in additionalLanguages array in any case but he should enter the language in only lower case when specifying the language of code block. Something like this
image

But, do we also want to add highlight support for code blocks like these?
image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, I have added them and I also saw that the VSCode Markdown preview supports highlighting all these blocks. So, maybe we should do it too.

@slorber slorber added the pr: bug fix This PR fixes a bug in a past release. label Jul 28, 2023
@slorber slorber changed the title fix: Enhanced Language Highlighting: Case-Insensitive Support fix: make Prism code block language / additionalLanguages case insensitive Jul 28, 2023
@Josh-Cena
Copy link
Collaborator

I think this should be done within theme config validation, where we map additionalLanguages to lowercase in the first place.

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, almost good but only 1 of the 3 examples there is highlighted, while I expect the 3 examples to be ;)

https://deploy-preview-9183--docusaurus-2.netlify.app/tests/pages/code-block-tests/#code-block-prism-language-tests

Uploading CleanShot 2023-07-28 at 19.34.01@2x.png…

@@ -176,7 +176,7 @@ export default function themeClassic(

configureWebpack() {
const prismLanguages = additionalLanguages
.map((lang) => `prism-${lang}`)
.map((lang) => `prism-${lang.toLowerCase()}`)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This could be better handled in the config validation, you can use Joi to normalize/lowercase the provided user input values so that everything else in the system see lowercase values (including prism-include-languages.ts, it shouldn't be needed to lowercase there too anymore)

?>
```

```PHP
Copy link
Collaborator

Choose a reason for hiding this comment

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

test added, but unfortunately highlighting doesn't work yet

We probably need to lowercase the meta string language in the code block theme component too

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, almost good but only 1 of the 3 examples there is highlighted, while I expect the 3 examples to be ;)

https://deploy-preview-9183--docusaurus-2.netlify.app/tests/pages/code-block-tests/#code-block-prism-language-tests

CleanShot 2023-07-28 at 19 34 01@2x

@heysujal
Copy link
Contributor Author

yes we should support the other two as well.

@heysujal
Copy link
Contributor Author

heysujal commented Aug 8, 2023

@slorber after two weeks I tried working on this PR again. now, I noticed that my code changes are not being picked up as I tried to redo the changes of PR that I have made in the local setup. For example I am using a bad case in additionalLanguages array for php like 'pHP' and I removed all the toLowerCase() that I added in this PR locally. I expected the code to break. but it still renders without any problem and gives the same output as this.

image

After reverting the changes I think it should have broken but it didn't. The changes not being picked up is the reason why I cannot work further on the meta string thing you mentioned (even though I guessed where to make changes)

I run yarn workspace website start whenever I make changes to recompile and build it again.

@slorber
Copy link
Collaborator

slorber commented Aug 10, 2023

Hey, not sure why but maybe:

  • you had a local caching issue (run yarn clear to be sure)
  • you are on a case-insensitive FS (macos, windows) and can't emulate the problem correctly

Anyway, I was able to make it work and the deploy preview is fine, building on Linux, so thanks, that's fine 👍

https://deploy-preview-9183--docusaurus-2.netlify.app/tests/pages/code-block-tests/#code-block-prism-language-tests

CleanShot 2023-08-10 at 17 30 02@2x

@slorber slorber changed the title fix: make Prism code block language / additionalLanguages case insensitive fix(theme): make Prism code block language / additionalLanguages case insensitive Aug 10, 2023
@slorber slorber merged commit 1e40943 into facebook:main Aug 10, 2023
28 of 29 checks passed
@heysujal
Copy link
Contributor Author

heysujal commented Aug 12, 2023

@slorber It feels a bit odd to me 😅😅😅, I mean you did all the work yet it looks like I have made the changes by creating a PR.

@slorber
Copy link
Collaborator

slorber commented Aug 12, 2023

I just polished a bit what you started so that we can merge 😄 I do that often and it's faster to move on than back and forth PR discussions

@heysujal
Copy link
Contributor Author

heysujal commented Aug 12, 2023

I just polished a bit what you started so that we can merge 😄 I do that often and it's faster to move on than back and forth PR discussions

Yes that makes a lot of sense

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: bug fix This PR fixes a bug in a past release.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow case-insensitivity for code block language
4 participants