代码块
文档中的代码块超级厉害 💪。
Code title
You can add a title to the code block by adding a title
key after the language (leave a space between them).
```jsx title="/src/components/HelloCodeTitle.js"
function HelloCodeTitle(props) {
return <h1>Hello, {props.name}</h1>;
}
```
function HelloCodeTitle(props) {
return <h1>Hello, {props.name}</h1>;
}
Syntax highlighting
代码块是使用 3 个反引号包裹的文本块。 You may check out this reference for the specifications of MDX.
```js
console.log('每个仓库都应该有个吉祥物。');
```
Use the matching language meta string for your code block, and Docusaurus will pick up syntax highlighting automatically, powered by Prism React Renderer.
console.log('每个仓库都应该有个吉祥物。');
Theming
By default, the Prism syntax highlighting theme we use is Palenight. 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:
import {themes as prismThemes} from 'prism-react-renderer';
export default {
themeConfig: {
prism: {
theme: prismThemes.dracula,
},
},
};
因为 每个 Prism 主题都只是一个 JS 对象,所以如果你对默认值不满意,也可以写一个自己的主题。 Docusaurus enhances the github
and vsDark
themes to provide richer highlight, and you can check our implementations for the light and dark code block themes.
Supported Languages
By default, Docusaurus comes with a subset of commonly used languages.
一些流行语言,包括 Java、C#、PHP 在内,默认未启用。
To add syntax highlighting for any of the other Prism-supported languages, define it in an array of additional languages.
每个附加语言都必须是有效的 Prism 组件名称。 For example, Prism would map the language cs
to csharp
, but only prism-csharp.js
exists as a component, so you need to use additionalLanguages: ['csharp']
. You can look into node_modules/prismjs/components
to find all components (languages) available.
举个例子,如果你想要支持 PowerShell 语言的高亮:
export default {
// ...
themeConfig: {
prism: {
additionalLanguages: ['powershell'],
},
// ...
},
};
After adding additionalLanguages
, restart Docusaurus.
If you want to add highlighting for languages not yet supported by Prism, you can swizzle prism-include-languages
:
- npm
- Yarn
- pnpm
npm run swizzle @docusaurus/theme-classic prism-include-languages
yarn swizzle @docusaurus/theme-classic prism-include-languages
pnpm run 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
:
const prismIncludeLanguages = (Prism) => {
// ...
additionalLanguages.forEach((lang) => {
require(`prismjs/components/prism-${lang}`);
});
require('/path/to/your/prism-language-definition');
// ...
};
You can refer to Prism's official language definitions when you are writing your own language definitions.
When adding a custom language definition, you do not need to add the language to the additionalLanguages
config array, since Docusaurus only looks up the additionalLanguages
strings in languages that Prism provides. Adding the language import in prism-include-languages.js
is sufficient.
Line highlighting
Highlighting with comments
You can use comments with highlight-next-line
, highlight-start
, and highlight-end
to select which lines are highlighted.
```js
function HighlightSomeText(highlight) {
if (highlight) {
// highlight-next-line
return '这行被高亮了!';
}
return '这里不会';
}
function HighlightMoreText(highlight) {
// highlight-start
if (highlight) {
return '这块 被高亮了!';
}
// highlight-end
return '这里不会';
}
```
function HighlightSomeText(highlight) {
if (highlight) {
return '这行被高亮了!';
}
return '这里不会';
}
function HighlightMoreText(highlight) {
if (highlight) {
return '这段被高亮了!';
}
return '这里不会';
}
受支持的注释语法:
样式 | 语法 |
---|---|
C 样式 | /* ... */ and // ... |
JSX 样式 | {/* ... */} |
Bash 样式 | # ... |
HTML 样式 | <!-- ... --> |
We will do our best to infer which set of comment styles to use based on the language, and default to allowing all comment styles. 如果有任何注释风格暂时没有被支持,我们乐意去添加对它们的支持。 欢迎发起拉取请求。 需要注意的是,不同注释风格不会有语义上的差别,只会有内容上的。
You can set your own background color for highlighted code line in your src/css/custom.css
which will better fit to your selected syntax highlighting theme. 下方的颜色适用于默认的语法高亮主题(Palenight)。如果你使用其他的主题,也需要做出相应的颜色调整。
:root {
--docusaurus-highlighted-code-line-bg: rgb(72, 77, 91);
}
/* If you have a different syntax highlighting theme for dark mode. */
[data-theme='dark'] {
/* Color which works with dark mode syntax highlighting theme */
--docusaurus-highlighted-code-line-bg: rgb(100, 100, 100);
}
If you also need to style the highlighted code line in some other way, you can target on theme-code-block-highlighted-line
CSS class.
Highlighting with metadata string
你也可以在元数据字符串中指定高亮行的范围(需要在语言种类后留一个空格)。 要高亮多行内容,请使用英文半角逗号来分隔行号,或使用范围语法来选择多行代码块以高亮。 This feature uses the parse-number-range
library and you can find more syntax on their project details.
```jsx {1,4-6,11}
import React from 'react';
function MyComponent(props) {
if (props.isBar) {
return <div>Bar</div>;
}
return <div>Foo</div>;
}
export default MyComponent;
```
import React from 'react';
function MyComponent(props) {
if (props.isBar) {
return <div>Bar</div>;
}
return <div>Foo</div>;
}
export default MyComponent;
推荐你使用注释来实现语法高亮。 如果你把通过内嵌的注 释来高亮代码,你就不需要在代码块内容很长时手动去数代码行数了。 即使你添加/删除了某几行,你也不需要去调整行号的偏移范围。
- ```jsx {3}
+ ```jsx {4}
function HighlightSomeText(highlight) {
if (highlight) {
+ console.log('找到了高亮文本');
return '这个文本高亮了!';
}
return '没啥被高亮了';
}
```
下面我们将介绍可扩展的魔法注释是如何实现自定义指令及和功能的。 只有在不使用元数据字符串的数字范围时,魔法注释才会被解析。
Custom magic comments
// highlight-next-line
and // highlight-start
etc. are called "magic comments", because they will be parsed and removed, and their purposes are to add metadata to the next line, or the section that the pair of start- and end-comments enclose.
你可以通过主题配置来设定你的自定义魔法注释。 For example, you can register another magic comment that adds a code-block-error-line
class name:
- docusaurus.config.js
- src/css/custom.css
- myDoc.md
export default {
themeConfig: {
prism: {
magicComments: [
// Remember to extend the default highlight class name as well!
{
className: 'theme-code-block-highlighted-line',
line: 'highlight-next-line',
block: {start: 'highlight-start', end: 'highlight-end'},
},
{
className: 'code-block-error-line',
line: 'This will error',
},
],
},
},
};