• Stars
    star
    171
  • Rank 215,948 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 10 years ago
  • Updated 2 months ago

Reviews

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

Repository Details

Markdown renderer for Hexo

hexo-renderer-marked

Build Status NPM version Coverage Status NPM Dependencies

Add support for Markdown. This plugin uses marked as its render engine.

Important note on security

By default, this plugin contains a potential security issue: It is possible to inject Markdown containing Unsafe HTML that will not be sanitized

This issue might not affect you because you checked the content of the markdown before using this plugin, but it's still a risk

There are two solutions to avoid those issues:

  1. First solution is to enable option dompurify: true, which will sanitize the rendered HTML. The side effect of this solution is that it will break any tag plugin (aka {% codeblock %}). This explains why the safer option has not been enabled by default
  2. Second solution is to migrate to hexo-renderer-markdown-it which is safe by default and does not suffer from the same limitations

Installation

$ npm install hexo-renderer-marked --save
  • Hexo 4: >= 2.0
  • Hexo 3: >= 0.2
  • Hexo 2: 0.1.x

Options

You can configure this plugin in _config.yml.

marked:
  gfm: true
  pedantic: false
  breaks: true
  smartLists: true
  smartypants: true
  quotes: 'โ€œโ€โ€˜โ€™'
  modifyAnchors: 0
  anchorAlias: false
  autolink: true
  mangle: true
  sanitizeUrl: false
  dompurify: false
  headerIds: true
  lazyload: false
  prependRoot: true
  postAsset: false
  external_link:
    enable: false
    exclude: []
    nofollow: false
  disableNunjucks: false
  descriptionLists: true
  • gfm - Enables GitHub flavored markdown
  • pedantic - Conform to obscure parts of markdown.pl as much as possible. Don't fix any of the original markdown bugs or poor behavior.
  • breaks - Enable GFM line breaks. This option requires the gfm option to be true.
  • smartLists - Use smarter list behavior than the original markdown.
  • smartypants - Use "smart" typographic punctuation for things like quotes and dashes.
  • quotes - Defines the double and single quotes used for substituting regular quotes if smartypants is enabled.
    • Example: 'ยซยปโ€œโ€'
      • "double" will be turned into ยซdoubleยป
      • 'single' will be turned into โ€œsingleโ€
    • Both double and single quotes substitution must be specified, otherwise it will be silently ignored.
  • modifyAnchors - Transform the anchorIds into lower case (1) or upper case (2).
  • autolink - Enable autolink for URLs. E.g. https://hexo.io will become <a href="https://hexo.io">https://hexo.io</a>.
  • mangle - Escape autolinked email address with HTML character references.
    • This is to obscure email address from basic crawler used by spam bot, while still readable to web browsers.
  • sanitizeUrl - Remove URLs that start with javascript:, vbscript: and data:.
  • dompurify - Enable DOMPurify to be run on the rendered Markdown. See below for configuration
  • headerIds - Insert header id, e.g. <h1 id="value">text</h1>. Useful for inserting anchor link to each paragraph with a heading.
  • anchorAlias - Enables custom header id
    • Example: ## [foo](#bar), id will be set as "bar".
    • Requires headerIds to be enabled.
  • lazyload - Lazy loading images via loading="lazy" attribute.
  • prependRoot - Prepend root value to (internal) image path.
    • Example _config.yml:
    root: /blog/
    • ![text](/path/to/image.jpg) becomes <img src="/blog/path/to/image.jpg" alt="text">
  • postAsset - Resolve post asset's image path to relative path and prepend root value when post_asset_folder is enabled.
    • "image.jpg" is located at "/2020/01/02/foo/image.jpg", which is a post asset of "/2020/01/02/foo/".
    • ![](image.jpg) becomes <img src="/2020/01/02/foo/image.jpg">
    • Requires prependRoot to be enabled.
  • external_link
    • enable - Open external links in a new tab.
    • exclude - Exclude hostname. Specify subdomain when applicable, including www.
      • Example: [foo](https://example.com) becomes <a href="https://example.com" target="_blank" rel="noopener">foo</a>
    • nofollow - Add rel="noopener external nofollow noreferrer" to all external links for security, privacy and SEO. Read more. This can be enabled regardless of external_link.enable
      • Example: [foo](https://example.com) becomes <a href="https://example.com" rel="noopener external nofollow noreferrer">foo</a>
  • disableNunjucks: If true, Nunjucks tags {{ }} or {% %} (usually used by tag plugins) will not be rendered.
  • descriptionLists: Enable support for description lists syntax.
    • Currently description lists syntax is not in neither CommonMark or GFM, hexo-renderer-marked only provides the option for backward compatibility.
    • By disabling the descriptionLists, markdown rendering performance will be improved by a lot.

For more options, see Marked. Due to the customizations implemented by this plugin, some of the Marked's options may not work as expected. Feel free to raise an issue to us for clarification.

Extras

Sanitize HTML with DOMPurify

DOMPurify can be enabled to sanitize the rendered HTML.

To enable it, pass an object containing the DOMPurify options:

dompurify: true

Or you can enable specific DOMPurify options (but according to DOMPurify authors, the default options are safe):

dompurify:
  FORBID_TAGS:
  - "style"

See https://github.com/cure53/DOMPurify#can-i-configure-dompurify for a full reference of available options

Definition/Description Lists

hexo-renderer-marked also implements description/definition lists using the same syntax as PHP Markdown Extra.

This Markdown:

Definition Term
:    This is the definition for the term

will generate this HTML:

<dl>
  <dt>Definition Term</dt>
  <dd>This is the definition for the term</dd>
</dl>

Note: There is currently a limitation in this implementation. If multiple definitions are provided, the rendered HTML will be incorrect.

For example, this Markdown:

Definition Term
:    Definition 1
:    Definition 2

will generate this HTML:

<dl>
  <dt>Definition Term<br>: Definition 1</dt>
  <dd>Definition 2</dd>
</dl>

If you've got ideas on how to support multiple definitions, please provide a pull request. We'd love to support it.

Extensibility

This plugin overrides some default behaviours of how marked 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 override how heading like # heading text is rendered:

hexo.extend.filter.register('marked:renderer', function(renderer) {
  const { config } = this; // Skip this line if you don't need user config from _config.yml
  renderer.heading = function(text, level) {
    // Default behaviour
    // return `<h${level}>${text}</h${level}>`;
    // outputs <h1>heading text</h1>

    // If you want to insert custom class name
    return `<h${level} class="headerlink">${text}</h${level}>`;
    // outputs <h1 class="headerlink">heading text</h1>
  }
})

Save the file in "scripts/" folder and run Hexo as usual.

Notice renderer.heading = function (text, level) { corresponds to this line. Refer to renderer.js on how this plugin overrides the default methods. For other methods not covered by this plugin, refer to marked's documentation.

Tokenizer

It is also possible to customize the tokenizer.

const { escapeHTML: escape } = require('hexo-util');

// https://github.com/markedjs/marked/blob/b6773fca412c339e0cedd56b63f9fa1583cfd372/src/Lexer.js#L8-L24
// Replace dashes only
const smartypants = (str) => {
  return str
    // em-dashes
    .replace(/---/g, '\u2014')
    // en-dashes
    .replace(/--/g, '\u2013')
};

hexo.extend.filter.register('marked:tokenizer', function(tokenizer) {
  const { smartypants: isSmarty } = this.config.marked;
  tokenizer.inlineText = function(src, inRawBlock) {
    const { rules } = this;

    // https://github.com/markedjs/marked/blob/b6773fca412c339e0cedd56b63f9fa1583cfd372/src/Tokenizer.js#L643-L658
    const cap = rules.inline.text.exec(src);
    if (cap) {
      let text;
      if (inRawBlock) {
        text = cap[0];
      } else {
        text = escape(isSmarty ? smartypants(cap[0]) : cap[0]);
      }
      return {
        // `type` value is a corresponding renderer method
        // https://marked.js.org/using_pro#inline-level-renderer-methods
        type: 'text',
        raw: cap[0],
        text
      };
    }
  }
});

Extensions

It is also possible to customize the extensions. For example, use KaTeX to render block-level math:

const katex = require('katex');

hexo.extend.filter.register('marked:extensions', function(extensions) {
  // Info: `extensions` is an array.
  extensions.push({
    name: 'blockMath',
    level: 'block',
    tokenizer(src) {
      const cap = /^\s{0,3}\$\$((?:[^\n]|\n[^\n])+?)\n{0,1}\$\$/.exec(src);

      if (cap !== null) {
        return {
          type: 'blockMath',
          raw: cap[0],
          math: cap[1]
        };
      }

      return undefined;
    },
    renderer(token) {
      return `<p>${katex.renderToString(token.math, {displayMode: true})}</p>\n`;
    }
  });
});

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-renderer-markdown-it

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

hexo-theme-light

A simple theme for Hexo
Stylus
316
star
8

hexo-math

A hexo plugin that uses MathJax to render math equations.
JavaScript
310
star
9

hexo-theme-landscape

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

hexo-generator-sitemap

Sitemap generator for Hexo.
JavaScript
281
star
11

warehouse

JSON database
TypeScript
200
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

create-hexo

Hexo site initializer
TypeScript
5
star
60

hexo-renderer-haml

Haml renderer for Hexo.
JavaScript
5
star
61

eslint-config-hexo

ESLint config for Hexo projects
JavaScript
5
star
62

hexo-tag-embed

TypeScript
4
star
63

hexo-tag-imgurl

A simple image tag plugin for Hexo.
JavaScript
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