Docusaurus
Admonitions
Note:
Note
:::note Note:
Note
:::
Tip:
Tip
:::tip Tip:
Tip
:::
Info:
Info
:::info Info:
Info
:::
Caution:
Caution
:::caution Caution:
Caution
:::
Warning!
Danger
:::danger Warning!
Danger
:::
Codeblock
with title
index.jsfunction foo() {
console.log("Hello World!");
}```jsx title="index.js"
function foo() {
console.log("Hello World!");
}
```Line Highlighting
- with comment
function foo() {
if (hour < 18) {
console.log("Good day");
} else {
console.log("Good evening");
}
}```javascript
// highlight-next-line
function foo() {
if (hour < 18) {
console.log("Good day");
// highlight-start
} else {
console.log("Good evening");
// highlight-end
}
}
```
- with comment
Custom error comments
Declaration
- docusaurus.config.js
- src/css/custom.css
module.exports = {
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: 'highlight-next-line-error',
},
],
},
},
};.code-block-error-line {
background-color: #ff000020;
display: block;
margin: 0 calc(-1 * var(--ifm-pre-padding));
padding: 0 var(--ifm-pre-padding);
border-left: 3px solid #ff000080;
}Usage
const name = null;
console.log(name.toUpperCase());
// Uncaught TypeError: Cannot read properties of null (reading 'toUpperCase')```javascript
const name = null;
// highlight-next-line-error
console.log(name.toUpperCase());
// Uncaught TypeError: Cannot read properties of null (reading 'toUpperCase')
```
with line range
function foo() {
if (hour < 18) {
console.log("Good day");
} else {
console.log("Good evening");
}
}```javascript {1,4-6}
function foo() {
if (hour < 18) {
console.log("Good day");
} else {
console.log("Good evening");
}
}
```Line numbering
function foo() {
console.log("Hello World!");
}```javascript showLineNumbers
function foo() {
console.log("Hello World!");
}
```
Installation
- npm
- yarn
- pnpm
npm install --save @docusaurus/theme-mermaid
yarn add @docusaurus/theme-mermaid
pnpm add @docusaurus/theme-mermaid
Config
docusaurus.config.jsmodule.exports = {
markdown: {
mermaid: true,
},
themes: ['@docusaurus/theme-mermaid'],
};Usage
Class Diagram:
```mermaid
classDiagram
direction RL
class Student {
-idCard : IdCard
}
class IdCard{
-id : int
-name : string
}
class Bike{
-id : int
-name : string
}
Student "1" --o "1" IdCard : carries
Student "1" --o "1" Bike : rides
```
Interactive Code Editor
add Plugin package
- npm
- yarn
- pnpm
npm install --save @docusaurus/theme-live-codeblock
yarn add @docusaurus/theme-live-codeblock
pnpm add @docusaurus/theme-live-codeblock
add plugin to config
docusaurus.config.jsmodule.exports = {
// ...
themes: ['@docusaurus/theme-live-codeblock'],
// ...
};Usage
```jsx live
function toCelsius(f) {
f = 77;
celsius = (5/9) * (f-32)
return celsius;
}
```
React Live Imports
ButtonExample
Import:
- npm
- yarn
- pnpm
npm run swizzle @docusaurus/theme-live-codeblock ReactLiveScope -- --eject
yarn swizzle @docusaurus/theme-live-codeblock ReactLiveScope --eject
pnpm run swizzle @docusaurus/theme-live-codeblock ReactLiveScope -- --eject
src/theme/ReactLiveScope/index.jsimport React from 'react';
const ButtonExample = (props) => (
<button
{...props}
style={{
backgroundColor: 'white',
color: 'black',
border: 'solid red',
borderRadius: 20,
padding: 10,
cursor: 'pointer',
...props.style,
}}
/>
);
// Add react-live imports you need here
const ReactLiveScope = {
React,
...React,
ButtonExample,
};
export default ReactLiveScope;
Caution:
Restart Dev-Server!
usage
```jsx live
function MyPlayground(props) {
return (
<div>
<ButtonExample onClick={() => alert('hey!')}>Click me</ButtonExample>
</div>
);
}
```
Tabs
Example
- html
- css
- js
htmlcssjsUsage
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
<Tabs>
<TabItem value="html" label="html" default>
html
</TabItem>
<TabItem value="css" label="css">
css
</TabItem>
<TabItem value="js" label="js">
js
</TabItem>
</Tabs>
Heading
set heading range in toc for single document
---
toc_min_heading_level: 2
toc_max_heading_level: 6
---set heading range in toc as default for all documents
docusaurus.config.jsmodule.exports = {
themeConfig: {
tableOfContents: {
minHeadingLevel: 2,
maxHeadingLevel: 6,
},
},
};
MDX Plugins
Math Equations
Katex
Documentation
Install
- npm
- yarn
- pnpm
npm install --save remark-math@3 rehype-katex@4
yarn add remark-math@3 rehype-katex@4
pnpm add remark-math@3 rehype-katex@4
config
docusaurus.config.jsconst math = require('remark-math');
const katex = require('rehype-katex');
module.exports = {
title: 'Docusaurus',
tagline: 'Build optimized websites quickly, focus on your content',
presets: [
[
'@docusaurus/preset-classic',
{
docs: {
path: 'docs',
remarkPlugins: [math],
rehypePlugins: [katex],
},
},
],
],
stylesheets: [
{
href: 'https://cdn.jsdelivr.net/npm/katex@0.13.24/dist/katex.min.css',
type: 'text/css',
integrity:
'sha384-odtC+0UGzzFL/6PNoE8rX/SPcQDXBJ+uRepguP4QkPCm2LBxH3FA3y+fKSiJ+AmM',
crossorigin: 'anonymous',
},
],
};
Version must be:
package.json
"rehype-katex": "^4.0.0",
"remark-math": "^3.0.1",
Config with Docusaurus
plugin-content-docs
:docusaurus.config.js/** @type {import('@docusaurus/plugin-content-docs').Options} */
const defaultSettings = {
remarkPlugins: [math],
rehypePlugins: [katex],
};
Usage Inline
Fundamental Theorem of Calculus
Let be Riemann integrable. Let be . Then is continuous, and at all such that is continuous at , is differentiable at with .**Fundamental Theorem of Calculus**
Let $f:[a,b] \to \R$ be Riemann integrable. Let $F:[a,b]\to\R$ be $F(x)=
\int_{a}^{x}f(t)dt$.
Then $$F$$ is continuous, and at all $x$ such that $f$ is continuous at $x$,
$F$ is differentiable at $x$ with $F'(x)=f(x)$.
Usage as Block
$$
I = \int_0^{2\pi} \sin(x)\,dx
$$