• Stars
    star
    333
  • Rank 122,425 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Markdown-it is a Markdown parser, done right. A faster and CommonMark compliant alternative for Hexo.

hexo-renderer-markdown-it

Build Status npm version Coverage Status

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  # https://markdown-it.github.io/markdown-it/#MarkdownIt.renderInline

See below for more details.

Advanced Configuration

Preset options

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 # Doesn't escape HTML content
    ## OR
html: false # Escapes HTML content so the tags will appear as text.

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 # Parser produces fully XHTML compliant code.
               # Ex: A line breaks will be <br />
    ## OR
xhtmlOut: false # Parser will not produce XHTML compliant code.
                # Ex: A line break will be <br>

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 # Parser produces `<br>` tags every time there is a line break in the source document.
    ## OR
breaks: false # Parser will ignore line breaks in the source document.
              #Default double line break creates paragraph is still supported

langPrefix

Add a prefix to the class name of code blocks (when a language is specified).

langPrefix: 'language-' # default

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 # Returns text links as proper links inlined with the paragraph.
    ## OR
linkify: false # Returns text links as text.

typographer

This is enables the substitution for common typography elements like Β©, curly quotes, dashes, etc.

typographer: true # Substitution of common typographical elements will take place.
    ## OR
typographer: false # No substitution, so dumb quotes will remain dumb quotes, etc.

quotes

Defines the double and single quotes used for substituting dumb quotes if typographer is set to true.

quotes: 'β€œβ€β€˜β€™' # "double" will be turned into β€œsingle”
               # 'single' will be turned into β€˜single’
    ## OR
quotes: 'Β«Β»β€œβ€' # "double" will be turned into Β«singleΒ»
               # 'single' will be turned into β€œsingle”

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'

  # Single rule
  enable_rules: 'link'

  # Multiple rules
  enable_rules:
    - 'link'
    - 'image'

"default" preset enables all rules, to disable specific rules:

markdown:
  preset: 'default'

  # Single rule
  disable_rules: 'link'

  # Multiple rules
  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:
    # Minimum level for ID creation. (Ex. h2 to h6)
    level: 2
    # A suffix that is prepended to the number given if the ID is repeated.
    collisionSuffix: ''
    # If `true`, creates an anchor tag with a permalink besides the heading.
    permalink: false
    # Class used for the permalink anchor tag.
    permalinkClass: header-anchor
    # Set to 'right' to add permalink after heading
    permalinkSide: 'left'
    # The symbol used to make the permalink
    permalinkSymbol: ΒΆ
    # Transform anchor to (1) lower case; (2) upper case
    case: 0
    # Replace space with a character
    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
    # installed external plugins also can be enabled
    - 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; // Optional, parse user config from _config.yml
  md.validateLink = function() { return true; };
});

// Specify custom function in plugin option
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'
  # Other plugins...

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
  # Other plugins...

Requests and bug reports

If you have any feature requests or bugs to report, you're welcome to file an issue.

More Repositories

1

hexo

A fast, simple & powerful blog framework, powered by Node.js.
TypeScript
38,563
star
2

site

The website for Hexo. https://hexo.io/
Stylus
653
star
3

awesome-hexo

A curated list of awesome things related to Hexo
614
star
4

hexo-deployer-git

Git deployer plugin for Hexo.
JavaScript
556
star
5

hexo-generator-feed

Feed generator for Hexo.
JavaScript
544
star
6

hexo-theme-light

A simple theme for Hexo
Stylus
316
star
7

hexo-math

A hexo plugin that uses MathJax to render math equations.
JavaScript
307
star
8

hexo-theme-landscape

A brand new default theme for Hexo.
Stylus
306
star
9

hexo-generator-sitemap

Sitemap generator for Hexo.
JavaScript
281
star
10

warehouse

JSON database
TypeScript
200
star
11

hexo-renderer-marked

Markdown renderer for Hexo
JavaScript
171
star
12

hexo-starter

Hexo Starter site (use `npx hexo init myBlog`)
170
star
13

hexo-cli

Command line interface for Hexo
TypeScript
170
star
14

hexo-browsersync

BrowserSync plugin for Hexo.
JavaScript
139
star
15

hexo-renderer-pandoc

A pandoc-markdown-flavor renderer for hexo.
JavaScript
110
star
16

hexo-theme-unit-test

This is a dummy Hexo site for theme unit test. You should test your theme before release.
94
star
17

hexo-theme-phase

Feel the flow of time with Phase, the most beautiful theme for Hexo.
Stylus
92
star
18

hexo-util

Utilities for Hexo.
TypeScript
85
star
19

hexo-server

Server module for Hexo.
JavaScript
78
star
20

hexojs.github.io

Replaced by hexojs/site
HTML
59
star
21

hexo-generator-index

Index generator plugin for Hexo.
JavaScript
49
star
22

hexo-generator-alias

Generate alias pages for redirecting to posts, pages or URL
JavaScript
48
star
23

hexo-renderer-ejs

EJS renderer for Hexo
JavaScript
48
star
24

hexo-migrator-wordpress

WordPress migrator for Hexo.
JavaScript
47
star
25

hexo-deployer-rsync

Rsync deployer plugin for Hexo.
JavaScript
45
star
26

hexo-fs

File system module for Hexo.
TypeScript
41
star
27

hexo-generator-archive

Archive generator plugin for Hexo.
JavaScript
36
star
28

hexo-filter-nofollow

Add nofollow attribute to all external links automatically.
JavaScript
36
star
29

hexo-generator-tag

Tag generator plugin for Hexo.
JavaScript
32
star
30

hexo-asset-pipeline

A hexo plugin to minify/optimize HTML, CSS, JS and images. Supports revisioning of assets.
JavaScript
29
star
31

hexo-livereload

Livereload plugin for Hexo
JavaScript
28
star
32

hexo-renderer-jade

Jade renderer for Hexo. Replaced by https://github.com/hexojs/hexo-renderer-pug
JavaScript
28
star
33

hexo-deployer-heroku

Heroku deployer plugin for Hexo.
JavaScript
27
star
34

hexo-generator-category

Category generator plugin for Hexo.
JavaScript
27
star
35

hexo-html-minifier

Minify HTML files with HTMLMinifier.
JavaScript
26
star
36

hexo-filter-auto-spacing

Add spaces between CJK characters and western characters.
JavaScript
24
star
37

hexo-deployer-ftpsync

FTP deployer for Hexo
JavaScript
23
star
38

hexo-pagination

Pagination utilities for Hexo generator plugins.
JavaScript
22
star
39

hexo-front-matter

Front-matter parser
TypeScript
21
star
40

hexo-renderer-stylus

Stylus renderer for Hexo
JavaScript
18
star
41

hexo-filter-responsive-images

Generate mutliple version of images for responsive Hexo blogs
JavaScript
18
star
42

hexo-autoprefixer

Autoprefixer plugin for Hexo.
JavaScript
17
star
43

hexo-filter-lqip

A Hexo plugins which helps to introduce low quality image placeholders to the theme
JavaScript
16
star
44

hexo-clean-css

Minify CSS files with clean-css.
JavaScript
16
star
45

hexo-fontawesome

A utility function which helps to inline fontawesome SVG files.
JavaScript
15
star
46

hexo-renderer-less

Less renderer for Hexo.
JavaScript
14
star
47

hexo-uglify

Minify JavaScript files with UglifyJS.
JavaScript
14
star
48

hexo-i18n

i18n module for Hexo.
TypeScript
13
star
49

hexo-migrator-rss

RSS migrator for Hexo.
JavaScript
12
star
50

hexo-renderer-inferno

Inferno.js JSX renderer for Hexo
JavaScript
11
star
51

hexo-renderer-pug

Pug renderer for Hexo.
JavaScript
10
star
52

hexo-inject

Dynamic script & style (and more) injection for Hexo
JavaScript
10
star
53

hexo-renderer-jsx

JavaScript
9
star
54

hexo-notify

Notification plugin for Hexo.
JavaScript
8
star
55

hexo-webui

Hexo Web UI
Stylus
8
star
56

hexo-renderer-swig

Swig renderer for Hexo
JavaScript
8
star
57

hexo-log

TypeScript
7
star
58

hexo-deployer-openshift

OpenShift deployer plugin for Hexo.
JavaScript
7
star
59

hexo-renderer-haml

Haml renderer for Hexo.
JavaScript
5
star
60

eslint-config-hexo

ESLint config for Hexo projects
JavaScript
5
star
61

hexo-tag-embed

TypeScript
4
star
62

hexo-tag-imgurl

A simple image tag plugin for Hexo.
JavaScript
4
star
63

create-hexo

Hexo site initializer
TypeScript
4
star
64

hexo-renderer-coffeescript

CoffeeScript renderer for Hexo.
JavaScript
4
star
65

hexo-slides

Presentation with Hexo
JavaScript
3
star
66

hexo-renderer-nunjucks

Add support for Nunjucks templates to Hexo websites
JavaScript
3
star
67

hexo-renderer-dot

doT renderer plugin for Hexo
JavaScript
2
star
68

logo

Logo file for Hexo.
2
star
69

hexo-yuidoc

Generate YUIDoc with Hexo
JavaScript
2
star
70

.github

1
star
71

hexo-migrator-blogger

Blogger migrator for Hexo.
JavaScript
1
star