hexo-renderer-markdown-it
This renderer plugin uses Markdown-it as a render engine on Hexo. Adds support for Markdown and CommonMark.
Main Features
- Support for Markdown, GFM and CommonMark
- Extensive configuration
- Faster than the default renderer |
hexo-renderer-marked
- Safe ID for headings
- Anchors for headings with ID
- Footnotes
<sub>
H2O<sup>
x2<ins>
Inserted
Installation
Warning: make sure you're inside the main hexo directory before starting this guide.
A default Hexo installation will include a markdown renderer plugin which uses marked
, so you will have to remove it if you want to use hexo-renderer-markdown-it
.
$ npm un hexo-renderer-marked --save
If you have already removed the default renderer, and others you might of added, you can now safely install hexo-renderer-markdown-it
$ npm i hexo-renderer-markdown-it --save
Options
markdown:
preset: 'default'
render:
html: true
xhtmlOut: false
langPrefix: 'language-'
breaks: true
linkify: true
typographer: true
quotes: '“”‘’'
enable_rules:
disable_rules:
plugins:
anchors:
level: 2
collisionSuffix: ''
permalink: false
permalinkClass: 'header-anchor'
permalinkSide: 'left'
permalinkSymbol: '¶'
case: 0
separator: '-'
images:
lazyload: false
prepend_root: false
post_asset: false
inline: false
See below for more details.
Advanced Configuration
markdown:
preset: 'default'
- "commonmark" - configures parser to strict CommonMark mode.
- "default" - similar to GFM, used when no preset name given. Enables all available rules, but still without html, typographer & autolinker.
- "zero" - all rules disabled. Useful to quickly setup your config via
.enable()
. For example, when you need only bold
and italic
markup and nothing else.
Note that the default render and anchor options override some options in the preset. If you prefer to have the preset only:
markdown:
preset: 'default'
render:
anchors:
Render options
html
The html
setting defines whether or not HTML content inside the document should be escaped or passed to the final result.
html: true
html: false
xhtmlOut
The xhtmlOut
setting defines whether the parser will export fully XHTML compatible tags. This only needs to be true
for complete CommonMark support.
xhtmlOut: true
xhtmlOut: false
breaks
Makes line breaks in the source file will be parsed into <br>
tags. So every time you press the Enter
key you will create a line break, which is not the default Markdown, CommonMark, or GFM behaviour.
breaks: true
breaks: false
langPrefix
Add a prefix to the class name of code blocks (when a language is specified).
langPrefix: 'language-'
This option only applies when both Hexo's built-in highlighters are disabled.
Example:
langPrefix: 'lang-'
Source:
``` js
example
```
HTML:
<pre>
<code class="lang-js">example</code>
</pre>
linkify
The parser has the ability to inline links pasted directly into the text. If you write a piece of text that looks like a link it will be rendered as <a src="http://example.com">http://example.com</a>
.
linkify: true
linkify: false
typographer
This is enables the substitution for common typography elements like ©, curly quotes, dashes, etc.
typographer: true
typographer: false
quotes
Defines the double and single quotes used for substituting dumb quotes if typographer is set to true
.
quotes: '“”‘’'
quotes: '«»“”'
Example configuration
markdown:
render:
html: true
xhtmlOut: false
breaks: false
linkify: true
typographer: true
quotes: '“”‘’'
Manage rules
Certain rules are enabled or disabled depending on the preset. For example, "zero" preset disables all rules, to enable specific rules:
markdown:
preset: 'zero'
enable_rules: 'link'
enable_rules:
- 'link'
- 'image'
"default" preset enables all rules, to disable specific rules:
markdown:
preset: 'default'
disable_rules: 'link'
disable_rules:
- 'link'
- 'image'
Available rules
Since the rules are subject to change, it's better to check the Markdown-it's source code for up-to-date rules. Look for the _rules
variable in the following files:
Automatic Headline ID's
Enables you to automatically create ID's for the headings so you can link back to them. A valid html document cannot have two elements with duplicate id value, for example if title
id is already used, subsequent title
values will be updated to title-2
, title-3
and so on.
Default options:
markdown:
anchors:
level: 2
collisionSuffix: ''
permalink: false
permalinkClass: header-anchor
permalinkSide: 'left'
permalinkSymbol: ¶
case: 0
separator: '-'
Plugins
Included plugins:
- markdown-it-abbr
- markdown-it-attrs
- markdown-it-cjk-breaks
- markdown-it-container
- markdown-it-deflist
- markdown-it-emoji
- markdown-it-footnote
- markdown-it-ins
- markdown-it-mark
- markdown-it-sub
- markdown-it-sup
Plugins are not enabled by default, to enable:
markdown:
plugins:
- markdown-it-abbr
- markdown-it-table-of-contents
Plugin option
markdown:
plugins:
- name: 'markdown-it-emoji'
options:
shortcuts:
laughing: ':D'
- name: 'other-plugin'
options: ...
Extensibility
This plugin overrides some default behaviors of how markdown-it plugin renders the markdown into html, to integrate with the Hexo ecosystem. It is possible to override this plugin too, without resorting to forking the whole thing.
For example, to enable unsafe links (which is disabled by default):
hexo.extend.filter.register('markdown-it:renderer', function(md) {
const { config } = this;
md.validateLink = function() { return true; };
});
const { slugize } = require('hexo-util');
const opts = hexo.config.markdown.anchors;
const mdSlugize = (str) => {
return slugize(str, { transform: opts.case, ...opts });
};
hexo.extend.filter.register('markdown-it:renderer', function(md) {
md.use(require('markdown-it-table-of-contents'), {
includeLevel: [2,3,4],
slugify: mdSlugize
});
});
Save the file in "scripts/" folder and run Hexo as usual.
Refer to markdown-it API documentation.
Frequently Asked Questions
Missing Styles of GFM Task Lists
In general, adding the following styles to the theme can solve the problem.
li.task-list-item {
list-style-type: none;
}
li.task-list-item .task-list-item-checkbox {
margin: 0 0.2em 0.25em -1.6em;
}
How can I add math support?
First, install KaTeX plugin for markdown-it.
npm install katex @renbaoshuo/markdown-it-katex
Then add @renbaoshuo/markdown-it-katex
to plugins list.
plugins:
- '@renbaoshuo/markdown-it-katex'
If you need to allow spaces before and after delimiters (e.g. $ 1 + 1 = 2 $
), set the skipDelimitersCheck
option to true
:
plugins:
- name: '@renbaoshuo/markdown-it-katex'
options:
skipDelimitersCheck: true
Don't forget to include the KaTeX stylesheet in your html:
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/katex/dist/katex.min.css"
/>
How can I merge table cells with the same content?
Install the markdown-it-merge-cells
plugin.
npm install markdown-it-merge-cells
Then add the plugin to plugins list.
plugins:
- markdown-it-merge-cells
Requests and bug reports
If you have any feature requests or bugs to report, you're welcome to file an issue.