Markdown Features
Documentation is one of your product's interfaces with your users. A well-written and well-organized set of docs helps your users understand your product quickly. Our aligned goal here is to help your users find and understand the information they need, as quickly as possible.
Docusaurus 2 uses modern tooling to help you compose your interactive documentations with ease. You may embed React components, or build live coding blocks where your users may play with the code on the spot. Start sharing your eureka moments with the code your audience cannot walk away from. It is perhaps the most effective way of attracting potential users.
In this section, we'd like to introduce you to the tools we've picked that we believe will help you build powerful documentation. Let us walk you through with an example.
important
All the following content assumes you are using @docusaurus/preset-classic
.
Markdown is a syntax that enables you to write formatted content in a readable syntax. The standard Markdown syntax is supported and we use MDX as the parsing engine, which can do much more than just parsing Markdown. More on that later.
Create a markdown file, greeting.md
, and place it under the docs
directory.
In the top of the file, specify id
the title
in the front matter, so that Docusaurus will pick them up correctly when generating your site.
This will render in the browser as follows:
Hello from Docusaurus
Are you ready to create the documentation site for your open source project?
Headers
will show up on the table of contents on the upper right
So that your users will know what this page is all about without scrolling down or even without reading too much.
Only h2 and h3 will be in the toc
The headers are well-spaced so that the hierarchy is clear.
- lists will help you
- present the key points
- that you want your users to remember
- and you may nest them
- multiple times
- and you may nest them
Markdown Headers
Documents use the following markdown header fields that are enclosed by a line ---
on either side:
id
: A unique document id. If this field is not present, the document'sid
will default to its file name (without the extension).title
: The title of your document. If this field is not present, the document'stitle
will default to itsid
.hide_title
: Whether to hide the title at the top of the doc. By default it isfalse
.hide_table_of_contents
: Whether to hide the table of contents to the right. By default it isfalse
.sidebar_label
: The text shown in the document sidebar and in the next/previous button for this document. If this field is not present, the document'ssidebar_label
will default to itstitle
.custom_edit_url
: The URL for editing this document. If this field is not present, the document's edit URL will fall back toeditUrl
from options fields passed todocusaurus-plugin-content-docs
.keywords
: Keywords meta tag for the document page, for search engines.description
: The description of your document, which will become the<meta name="description" content="..."/>
and<meta property="og:description" content="..."/>
in<head>
, used by search engines. If this field is not present, it will default to the first line of the contents.image
: Cover or thumbnail image that will be used when displaying the link to your post.
Example:
Embedding React components
Docusaurus has built-in support for MDX, which allows you to write JSX within your Markdown files and render them as React components.
Note 1: While both .md
and .mdx
files are parsed using MDX, some of the syntax are treated slightly differently. For the most accurate parsing and better editor support, we recommend using the .mdx
extension for files containing MDX syntax. Let's rename the previous file to greeting.mdx
.
Note 2: Since all doc files are parsed using MDX, any HTML is treated as JSX. Therefore, if you need to inline-style a component, follow JSX flavor and provide style objects. This behavior is different from Docusaurus 1. See also Migrating from v1 to v2.
Try this block here:
Notice how it renders both the markup from your React component and the Markdown syntax:
I can write Markdown alongside my JSX!
You can also import your own components defined in other files or third-party components installed via npm! Check out the MDX docs to see what other fancy stuff you can do with MDX.
Referencing other documents
If you want to reference another document file, you should use the name of the document you want to reference. Docusaurus will convert the file path to be the final website path (and remove the .md
).
For example, if you are in doc2.md
and you want to reference doc1.md
and folder/doc3.md
:
One benefit of this approach is that the links to external files will still work if you are viewing the file on GitHub.
Syntax highlighting
Code blocks are text blocks wrapped around by strings of 3 backticks. You may check out this reference for specifications of MDX.
Use the matching language meta string for your code block, and Docusaurus will pick up syntax highlighting automatically, powered by Prism React Renderer.
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:
By default, Docusaurus comes with this subset of commonly used languages.
To add syntax highlighting for any of the other Prism supported languages, define it in an array of additional languages.
For example, if you want to add highlighting for the powershell
language:
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).
To accomplish this, Docusaurus adds the docusaurus-highlight-code-line
class to the highlighted lines. You will need to define your own styling for this CSS, possibly in your src/css/custom.css
with a custom background color which is dependent on your selected syntax highlighting theme. The color given below works for the default highlighting theme (Palenight), so if you are using another theme, you will have to tweak the color accordingly.
To highlight multiple lines, separate the line numbers by commas or use the range syntax to select a chunk of lines. This feature uses the parse-number-range
library and you can find more syntax on their project details.
Interactive code editor
(Powered by React Live)
You can create an interactive coding editor with the @docusaurus/theme-live-codeblock
plugin.
First, add the plugin to your package.
- npm
- Yarn
You will also need to add the plugin to your docusaurus.config.js
.
To use the plugin, create a code block with live
attached to the language meta string.
The code block will be rendered as an interactive editor. Changes to the code will reflect on the result panel live.
It is 7:21:05 AM.
Multi-language support code blocks
With MDX, you can easily create interactive components within your documentation, for example, to display code in multiple programming languages and switching between them using a tabs component.
Instead of implementing a dedicated component for multi-language support code blocks, we've implemented a generic Tabs component in the classic theme so that you can use it for other non-code scenarios as well.
The following example is how you can have multi-language code tabs in your docs. Note that the empty lines above and below each language block is intentional. This is a current limitation of MDX, you have to leave empty lines around Markdown syntax for the MDX parser to know that it's Markdown syntax and not JSX.
And you will get the following:
- JavaScript
- Python
- Java
You may want to implement your own <MultiLanguageCode />
abstraction if you find the above approach too verbose. We might just implement one in future for convenience.
Callouts/admonitions
In addition to the basic Markdown syntax, we use remark-admonitions alongside MDX to add support for admonitions. Admonitions are wrapped by a set of 3 colons.
Example:
Title
The content and title can include markdown.