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(v2): Allow swizzling prism-include-languages in theme-classic #2841

Merged
merged 3 commits into from
May 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 2 additions & 17 deletions packages/docusaurus-theme-classic/src/prism-include-languages.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,6 @@
*/

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

(() => {
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;
}
})();
prismIncludeLanguages(Prism);
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 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 ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
import siteConfig from '@generated/docusaurus.config';

const prismIncludeLanguages = (Prism) => {
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;
}
};

export default prismIncludeLanguages;
24 changes: 24 additions & 0 deletions website/docs/markdown-features.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,30 @@ module.exports = {
};
```

If you want to add highlighting for languages not yet supported by Prism, you can swizzle `prism-include-languages`:

```bash npm2yarn
yarn swizzle @docusaurus/theme-classic prism-include-languages
```

It will produce `prism-include-languages.js` in your `src/theme` folder. You can add highlighting support for custom languages by editing `prism-include-languages.js`:

```js {8} title="src/theme/prism-include-languages.js"
const prismIncludeLanguages = (Prism) => {
// ...
lex111 marked this conversation as resolved.
Show resolved Hide resolved

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

require('/path/to/your/prism-language-definition');

// ...
}
```

You can refer to [Prism's official language definitions](https://github.com/PrismJS/prism/tree/master/components) when you are writing your own language definitions.

### Line highlighting

You can bring emphasis to certain lines of code by specifying line ranges after the language meta string (leave a space after the language).
Expand Down