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): add ability default lang for code blocks #1910

Merged
merged 13 commits into from
Nov 5, 2019
10 changes: 10 additions & 0 deletions CHANGELOG-2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
- Prioritize `@docusaurus/core` dependencies/ node_modules over user's node_modules. This fix a bug whereby if user has core-js@3 on its own node_modules but docusaurus depends on core-js@2, we previously encounter `Module not found: core-js/modules/xxxx` (because core-js@3 doesn't have that).
Another example is if user installed webpack@3 but docusaurus depends on webpack@4.
- Added code block line highlighting feature (thanks @lex111)! If you have previously swizzled the `CodeBlock` theme component, it is recommended to update your source code to have this feature.
- **BREAKING** `prismTheme` is renamed to `theme` as part new `prism` object in `themeConfig` field in your `docusaurus.config.js`. Eg:
```diff
themeConfig: {
- prismTheme: require('prism-react-renderer/themes/dracula'),
+ prism: {
+ theme: require('prism-react-renderer/themes/dracula'),
+ },
},
```
- Added new `prism` option `defaultLanguage` that is used if the language is not specified in code blocks

## 2.0.0-alpha.31

Expand Down
10 changes: 7 additions & 3 deletions packages/docusaurus-theme-classic/src/theme/CodeBlock/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const highlightLinesRangeRegex = /{([\d,-]+)}/;
export default ({children, className: languageClassName, metastring}) => {
const {
siteConfig: {
themeConfig: {prismTheme},
themeConfig: {prism = {}},
},
} = useDocusaurusContext();
const [showCopied, setShowCopied] = useState(false);
Expand Down Expand Up @@ -48,9 +48,13 @@ export default ({children, className: languageClassName, metastring}) => {
};
}, [button.current, target.current]);

const language =
let language =
languageClassName && languageClassName.replace(/language-/, '');

if (language === 'undefined' && prism.defaultLanguage) {
lex111 marked this conversation as resolved.
Show resolved Hide resolved
language = prism.defaultLanguage;
}

const handleCopyCode = () => {
window.getSelection().empty();
setShowCopied(true);
Expand All @@ -61,7 +65,7 @@ export default ({children, className: languageClassName, metastring}) => {
return (
<Highlight
{...defaultProps}
theme={prismTheme || defaultTheme}
theme={prism.theme || defaultTheme}
code={children.trim()}
language={language}>
{({className, style, tokens, getLineProps, getTokenProps}) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default ({
}) => {
const {
siteConfig: {
themeConfig: {prismTheme},
themeConfig: {prism = {}},
},
} = useDocusaurusContext();
const [showCopied, setShowCopied] = useState(false);
Expand Down Expand Up @@ -60,15 +60,19 @@ export default ({
<Playground
scope={{...React}}
code={children.trim()}
theme={prismTheme || defaultTheme}
theme={prism.theme || defaultTheme}
{...props}
/>
);
}

const language =
let language =
languageClassName && languageClassName.replace(/language-/, '');

if (!language && prism.defaultLanguage) {
language = prism.defaultLanguage;
}

const handleCopyCode = () => {
window.getSelection().empty();
setShowCopied(true);
Expand All @@ -79,7 +83,7 @@ export default ({
return (
<Highlight
{...defaultProps}
theme={prismTheme || defaultTheme}
theme={prism.theme || defaultTheme}
code={children.trim()}
language={language}>
{({className, style, tokens, getLineProps, getTokenProps}) => (
Expand Down
6 changes: 4 additions & 2 deletions website/docs/markdown-features.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,17 @@ Use the matching language meta string for your code block, and Docusaurus will p
console.log('Every repo must come with a mascot.');
```

By default, the Prism [syntax highlighting theme](https://github.com/FormidableLabs/prism-react-renderer#theming) we use is [Palenight](https://github.com/FormidableLabs/prism-react-renderer/blob/master/src/themes/palenight.js). You can change this to another theme by passing `prismTheme` as `themeConfig` in your docusaurus.config.js.
By default, the Prism [syntax highlighting theme](https://github.com/FormidableLabs/prism-react-renderer#theming) we use is [Palenight](https://github.com/FormidableLabs/prism-react-renderer/blob/master/src/themes/palenight.js). You can change this to another theme by passing `theme` field in `prism` as `themeConfig` in your docusaurus.config.js.

For example, if you prefer to use the `dracula` highlighting theme:

```js {4}
// docusaurus.config.js
module.exports = {
themeConfig: {
prismTheme: require('prism-react-renderer/themes/dracula'),
prism: {
theme: require('prism-react-renderer/themes/dracula'),
},
},
};
```
Expand Down