-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
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): Allow swizzling prism-include-languages in theme-classic #2841
feat(v2): Allow swizzling prism-include-languages in theme-classic #2841
Conversation
Deploy preview for docusaurus-2 ready! Built with commit 20ccbb9 |
It’s definitely certain that we won’t be able to completely refuse from swizzling, this is actually not a bad mechanism for change built-in components. We will also leave Therefore, I think we need to update the docs for this new use case - when people want to add custom language. However in most cases this is not needed by anyone, since very few people develop their own programming languages, since this feature is intended only for them, right? |
That's true. I've added the docs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some improvements.
Motivation
Right now it's hard to include custom languages syntax highlighting support. It's almost impossible to do it in a simple config due to context replacement issue. Right now there are two alternatives that both have some issues:
CodeBlock
component. Issue: A localCodeBlock
might eventually diverge from the CodeBlock intheme-classic
.Prism
object used bytheme-classic
will be correctly patched is completely at the mercy of package manager's hoisting behavior:Suppose you do this in your plugin
If you declare
prism-react-renderer
as a dependency of your plugin, then you might end up patching aprism-react-renderer
that is not used bytheme-classic
, since you might get a node_modules layout like:If you declare
prism-react-renderer
as a peer dependency or doesn't declare as a dependency, then you are still relying on package manager's hoisting behavior that produces this node_modules layout:However, it's completely legal for a package manager to produce this:
Then your plugin cannot find
prism-react-renderer
any more.The root of the issue is that we need a way that is guaranteed to use
prism-react-renderer
from@docusaurus/theme-classic
, while still being able to be customized easily in userland. This leaves us with only one alternative that doesn't require significant refactoring: Swizzling.This diff moves the main implementation of
prism-include-languages
intosrc/theme
folder so that it can be swizzled. The API of@theme/prism-include-languages
is likely to be very stable so swizzling it wouldn't produce a huge risk of divergence.Questions for maintainers
additionalLanguages
config?Have you read the Contributing Guidelines on pull requests?
Yes
Test Plan
prism-include-languages
byyarn workspace docusaurus-2-website swizzle @docusaurus/theme-classic prism-include-languages
docusaurus.config.js
to include swift as an additional language:prism: { theme: require('prism-react-renderer/themes/github'), darkTheme: require('prism-react-renderer/themes/dracula'), + additionalLanguages: ['swift'] },
yarn start
, and see the code is correctly syntax highlighted.5. Edit the swizzled `prism-include-languages` to make it an empty function.
Related PRs
(If this PR adds or changes functionality, please take some time to update the docs at https://github.com/facebook/docusaurus, and link to your PR here.)