-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
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): add support specify new languages for Prism #2250
Conversation
Deploy preview for docusaurus-2 ready! Built with commit 6f99c05 |
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.
Since this is just about importing more modules, can we add those lines to getClientModules()
of packages/docusaurus-theme-classic/src/index.js
? You have access to themeConfig
there. That way we don't have to add any global variables at all.
I think checking whether the language exists on Prism.language
is optional and shouldn't be a blocker to use this approach. The checking should be possible in packages/docusaurus-theme-classic/src/index.js
as well.
This will not work, we need to change the global Prism object. We already have a “ready-made” object from prism-react-renderer dep and we need to “modify” it. If we want a better solution, then we need to fork prism-react-renderer, I guess...
This check is necessary so as not to included (require) languages that have already been loaded (required). If the language is not supported by Prism, an exception will be thrown if you had this in mind. |
How are we modifying it in the code? Or is requiring the language module modifying it within? |
No, I mean that we need an existing Prism object, that is, we can not do support any language without this global variable from prism-react-renderer. |
Can you add another client module that does the exposing of the |
But how? Do you want to import Prism globally before loading React? Is this possible in the current implementation? UPD: Even if this is done, will it really work? https://github.com/FormidableLabs/prism-react-renderer/blob/master/src/defaultProps.js#L10 |
@yangshun please could you investigate if there is another good solution here so I can close this PR? |
I'll take a stab at this over the weekend (or the next :S) |
That would be great, although I have no idea how to do it differently, unless to fork prism-react-renderer lib and somehow modify the Prism object 🤷♂️ |
Here's is a small plugin that have worked for me: /** @returns {import('@docusaurus/types').Plugin<any>} */
module.exports = ({
siteConfig: { themeConfig: { prism: { additionalLanguages = [] } = {} } = {} },
}) => ({
getClientModules: () => [
require.resolve('./prism-expose'),
...additionalLanguages.map(lang => require.resolve(`prismjs/components/prism-${lang}`)),
require.resolve('./prism-cleanup'),
],
}); // prism-expose.js
import Prism from 'prism-react-renderer/prism';
window.Prism = Prism; // prism-cleanup.js
window.Prism = undefined; |
@ark120202 thank you 👍 , this approach apparently meant @yangshun, but I misunderstood him, and was looking for a solution in a completely different direction. I can rework my PR in accordance with this solution. |
1eaa34b
to
0d5250a
Compare
@yangshun re-check it out please. |
window.Prism = Prism; | ||
|
||
additionalLanguages.forEach(lang => { | ||
require(`prismjs/components/prism-${lang}`); // eslint-disable-line |
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.
I might be wrong, but if there's no some context replacement I'm missing, all files matching this query would be included in a bundle
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.
Yeah agreed, I think so too! One alternative is to generate a file in one of the plugin steps that generates the list of require()
statements containing just the languages needed and have this file import it.
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.
LGTM, my main concern is the dynamic requires.
packages/docusaurus-theme-classic/src/include-additional-languages.js
Outdated
Show resolved
Hide resolved
packages/docusaurus-theme-classic/src/include-additional-languages.js
Outdated
Show resolved
Hide resolved
packages/docusaurus-theme-classic/src/include-additional-languages.js
Outdated
Show resolved
Hide resolved
@yangshun I fixed all bugs, please review. |
@@ -17,7 +17,8 @@ | |||
"prism-react-renderer": "^1.0.2", | |||
"react-router-dom": "^5.1.2", | |||
"react-toggle": "^4.1.1", | |||
"remark-admonitions": "^1.1.0" | |||
"remark-admonitions": "^1.1.0", | |||
"webpack": "^4.41.2" |
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.
This should be part of devDependencies
right?
|
||
return { | ||
plugins: [ | ||
new ContextReplacementPlugin( |
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 comments on what it does and why it is necessary would be nice.
25a2860
to
6f99c05
Compare
@lex111 wow, I think you have just solved one of the most important feature requests/blockers. Amazing job ❤️ |
Motivation
Resolve #1879 and close #2356 as our users have an interest in this feature, I think we need to add this.
Related issues on Canny:
Cons of the current implementation:
Pollution of global space, I do not know how to avoid this, given the use of Prism React Renderer :(Have you read the Contributing Guidelines on pull requests?
Yes
Test Plan
themeConfig.prism.additionalLanguages
array with any languages missing in Prism React Renderer (rust eg)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.)