Skip to content
This repository has been archived by the owner on Mar 6, 2020. It is now read-only.

Commit

Permalink
add support specify new languages for Prism
Browse files Browse the repository at this point in the history
  • Loading branch information
liushooter committed Mar 4, 2020
1 parent a2434f5 commit 21bbff5
Show file tree
Hide file tree
Showing 5 changed files with 3,395 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

The classic theme for Docusaurus.

## 语法高亮

https://github.com/facebook/docusaurus/pull/2250

## Installation

Add `docusaurus/theme-classic` to your package:
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"infima": "0.2.0-alpha.3",
"parse-numeric-range": "^0.0.2",
"prism-react-renderer": "^1.0.2",
"react-toggle": "^4.1.1"
"react-toggle": "^4.1.1",
"webpack": "^4.42.0"
},
"peerDependencies": {
"@docusaurus/core": "^2.0.0",
Expand Down
26 changes: 24 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

const path = require('path');
const ContextReplacementPlugin = require('webpack/lib/ContextReplacementPlugin');

// Need to be inlined to prevent dark mode FOUC
// Make sure that the 'storageKey' is the same as the one in `/theme/hooks/useTheme.js`
Expand Down Expand Up @@ -45,7 +46,10 @@ module.exports = function(context, options) {
const {
siteConfig: {themeConfig},
} = context;
const {disableDarkMode = false} = themeConfig || {};

const {disableDarkMode = false, prism: {additionalLanguages = []} = {}} =
themeConfig || {};

const {customCss} = options || {};
return {
name: 'docusaurus-theme-classic',
Expand All @@ -55,7 +59,25 @@ module.exports = function(context, options) {
},

getClientModules() {
return ['infima/dist/css/default/default.css', customCss];
return ['infima/dist/css/default/default.css',
customCss,
path.resolve(__dirname, './prism-include-languages'),
];
},

configureWebpack() {
const prismLanguages = additionalLanguages
.map(lang => `prism-${lang}`)
.join('|');

return {
plugins: [
new ContextReplacementPlugin(
/prismjs[\\/]components$/,
new RegExp(`^./(${prismLanguages})$`),
),
],
};
},

injectHtmlTags() {
Expand Down
26 changes: 26 additions & 0 deletions src/prism-include-languages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import Prism from 'prism-react-renderer/prism';
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
import siteConfig from '@generated/docusaurus.config';

(() => {
if (ExecutionEnvironment.canUseDOM) {
const {
themeConfig: {prism: {additionalLanguages = []} = {}},
} = siteConfig;

window.Prism = Prism;

additionalLanguages.forEach(lang => {
require(`prismjs/components/prism-${lang}`); // eslint-disable-line
});

delete window.Prism;
}
})();
Loading

0 comments on commit 21bbff5

Please sign in to comment.