hexo-renderer-markdown-it
Advanced tools
Comparing version
23
index.js
@@ -28,15 +28,20 @@ /* global hexo */ | ||
case: 0, | ||
separator: '' | ||
separator: '-' | ||
}, hexo.config.markdown.anchors); | ||
const renderer = require('./lib/renderer'); | ||
const Renderer = require('./lib/renderer'); | ||
const renderer = new Renderer(hexo); | ||
renderer.disableNunjucks = Boolean(hexo.config.markdown.disableNunjucks); | ||
hexo.extend.renderer.register('md', 'html', renderer, true); | ||
hexo.extend.renderer.register('markdown', 'html', renderer, true); | ||
hexo.extend.renderer.register('mkd', 'html', renderer, true); | ||
hexo.extend.renderer.register('mkdn', 'html', renderer, true); | ||
hexo.extend.renderer.register('mdwn', 'html', renderer, true); | ||
hexo.extend.renderer.register('mdtxt', 'html', renderer, true); | ||
hexo.extend.renderer.register('mdtext', 'html', renderer, true); | ||
function render(data, options) { | ||
return renderer.parser.render(data.text); | ||
} | ||
hexo.extend.renderer.register('md', 'html', render, true); | ||
hexo.extend.renderer.register('markdown', 'html', render, true); | ||
hexo.extend.renderer.register('mkd', 'html', render, true); | ||
hexo.extend.renderer.register('mkdn', 'html', render, true); | ||
hexo.extend.renderer.register('mdwn', 'html', render, true); | ||
hexo.extend.renderer.register('mdtxt', 'html', render, true); | ||
hexo.extend.renderer.register('mdtext', 'html', render, true); |
'use strict'; | ||
module.exports = function(data, options) { | ||
const MdIt = require('markdown-it'); | ||
let { markdown } = this.config; | ||
const MarkdownIt = require('markdown-it'); | ||
// Temporary backward compatibility | ||
if (typeof markdown === 'string') { | ||
markdown = { | ||
preset: markdown | ||
}; | ||
this.log.warn(`Deprecated config detected. Please use\n\nmarkdown:\n preset: ${markdown.preset}\n\nSee https://github.com/hexojs/hexo-renderer-markdown-it#options`); | ||
} | ||
class Renderer { | ||
const { preset, render, enable_rules, disable_rules, plugins, anchors } = markdown; | ||
let parser = new MdIt(preset, render); | ||
/** | ||
* constructor | ||
* | ||
* @param {*} hexo context of hexo | ||
*/ | ||
constructor(hexo) { | ||
if (enable_rules) { | ||
parser.enable(enable_rules); | ||
} | ||
let { markdown } = hexo.config; | ||
if (disable_rules) { | ||
parser.disable(disable_rules); | ||
} | ||
// Temporary backward compatibility | ||
if (typeof markdown === 'string') { | ||
markdown = { | ||
preset: markdown | ||
}; | ||
hexo.log.warn(`Deprecated config detected. Please use\n\nmarkdown:\n preset: ${markdown.preset}\n\nSee https://github.com/hexojs/hexo-renderer-markdown-it#options`); | ||
} | ||
if (plugins) { | ||
parser = plugins.reduce((parser, pugs) => { | ||
if (pugs instanceof Object && pugs.name) { | ||
return parser.use(require(pugs.name), pugs.options); | ||
} | ||
return parser.use(require(pugs)); | ||
const { preset, render, enable_rules, disable_rules, plugins, anchors } = markdown; | ||
this.parser = new MarkdownIt(preset, render); | ||
}, parser); | ||
} | ||
if (enable_rules) { | ||
this.parser.enable(enable_rules); | ||
} | ||
if (anchors) { | ||
parser = parser.use(require('./anchors'), anchors); | ||
if (disable_rules) { | ||
this.parser.disable(disable_rules); | ||
} | ||
if (plugins) { | ||
this.parser = plugins.reduce((parser, pugs) => { | ||
if (pugs instanceof Object && pugs.name) { | ||
return parser.use(require(pugs.name), pugs.options); | ||
} | ||
return parser.use(require(pugs)); | ||
}, this.parser); | ||
} | ||
if (anchors) { | ||
this.parser.use(require('./anchors'), anchors); | ||
} | ||
hexo.execFilterSync('markdown-it:renderer', this.parser, { context: this }); | ||
} | ||
} | ||
this.execFilterSync('markdown-it:renderer', parser, { context: this }); | ||
return parser.render(data.text); | ||
}; | ||
module.exports = Renderer; |
{ | ||
"name": "hexo-renderer-markdown-it", | ||
"version": "5.0.0", | ||
"version": "6.0.0", | ||
"description": "Markdown-it is a Markdown parser, done right. A faster and CommonMark compliant alternative for Hexo.", | ||
@@ -9,3 +9,3 @@ "main": "index.js", | ||
"test": "mocha test/index.js", | ||
"test-cov": "nyc npm run test" | ||
"test-cov": "nyc --reporter=lcovonly npm run test" | ||
}, | ||
@@ -36,3 +36,3 @@ "repository": "hexojs/hexo-renderer-markdown-it", | ||
"hexo-util": "^2.1.0", | ||
"markdown-it": "^11.0.0", | ||
"markdown-it": "^12.0.0", | ||
"markdown-it-abbr": "^1.0.4", | ||
@@ -42,3 +42,3 @@ "markdown-it-cjk-breaks": "^1.1.2", | ||
"markdown-it-deflist": "^2.0.3", | ||
"markdown-it-emoji": "^1.4.0", | ||
"markdown-it-emoji": "^2.0.0", | ||
"markdown-it-footnote": "^3.0.1", | ||
@@ -52,11 +52,11 @@ "markdown-it-ins": "^3.0.0", | ||
"chai": "^4.0.0", | ||
"eslint": "^7.1.0", | ||
"eslint-config-hexo": "^4.0.0", | ||
"hexo": "^5.2.0", | ||
"mocha": "^8.0.1", | ||
"eslint": "^8.6.0", | ||
"eslint-config-hexo": "^5.0.0", | ||
"hexo": "^6.0.0", | ||
"mocha": "^9.1.3", | ||
"nyc": "^15.0.0" | ||
}, | ||
"engines": { | ||
"node": ">=10.13.0" | ||
"node": ">=12.13.0" | ||
} | ||
} |
276
README.md
# hexo-renderer-markdown-it | ||
[](https://travis-ci.org/hexojs/hexo-renderer-markdown-it) | ||
[](https://github.com/hexojs/hexo-renderer-markdown-it/actions?query=workflow%3ATester) | ||
[](https://www.npmjs.com/package/hexo-renderer-markdown-it) | ||
@@ -11,2 +11,3 @@ [](https://david-dm.org/hexojs/hexo-renderer-markdown-it) | ||
## Main Features | ||
- Support for [Markdown], [GFM] and [CommonMark] | ||
@@ -23,4 +24,17 @@ - Extensive configuration | ||
## Installation | ||
Follow the [installation guide](https://github.com/hexojs/hexo-renderer-markdown-it/wiki/Getting-Started). | ||
**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`. | ||
``` sh | ||
$ 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` | ||
``` sh | ||
$ npm i hexo-renderer-markdown-it --save | ||
``` | ||
## Options | ||
@@ -34,2 +48,3 @@ | ||
xhtmlOut: false | ||
langPrefix: 'language-' | ||
breaks: true | ||
@@ -50,7 +65,243 @@ linkify: true | ||
case: 0 | ||
separator: '' | ||
separator: '-' | ||
``` | ||
Refer to [the wiki](https://github.com/hexojs/hexo-renderer-markdown-it/wiki) for more details. | ||
See below for more details. | ||
## Advanced Configuration | ||
### [Preset](https://markdown-it.github.io/markdown-it/#MarkdownIt.new) options | ||
``` yml | ||
markdown: | ||
preset: 'default' | ||
``` | ||
- **"commonmark"** - configures parser to strict [CommonMark](https://commonmark.org/) mode. | ||
- **"default"** - similar to [GFM](https://github.github.com/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](https://github.com/hexojs/hexo-renderer-markdown-it#options) render and anchor options override some options in the preset. If you prefer to have the preset only: | ||
``` yml | ||
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. | ||
``` yaml | ||
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. | ||
``` yaml | ||
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. | ||
``` yaml | ||
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). | ||
``` yaml | ||
langPrefix: 'language-' # default | ||
``` | ||
_This option only applies when both Hexo's built-in highlighters are [**disabled**](https://hexo.io/docs/syntax-highlight#Disabled)._ | ||
**Example:** | ||
``` yml | ||
langPrefix: 'lang-' | ||
``` | ||
Source: | ||
```` | ||
``` js | ||
example | ||
``` | ||
```` | ||
HTML: | ||
```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>`. | ||
``` yaml | ||
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. | ||
``` yaml | ||
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`. | ||
``` yaml | ||
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 | ||
``` yaml | ||
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](#preset-options). For example, "zero" preset disables all rules, to enable specific rules: | ||
``` yml | ||
markdown: | ||
preset: 'zero' | ||
# Single rule | ||
enable_rules: 'link' | ||
# Multiple rules | ||
enable_rules: | ||
- 'link' | ||
- 'image' | ||
``` | ||
"default" preset enables all rules, to disable specific rules: | ||
``` yml | ||
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: | ||
- [parser_block.js](https://github.com/markdown-it/markdown-it/blob/master/lib/parser_block.js) | ||
- [parser_core.js](https://github.com/markdown-it/markdown-it/blob/master/lib/parser_core.js) | ||
- [parser_inline.js](https://github.com/markdown-it/markdown-it/blob/master/lib/parser_inline.js) | ||
### 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: | ||
``` yaml | ||
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-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: | ||
``` yaml | ||
markdown: | ||
plugins: | ||
- markdown-it-abbr | ||
# installed external plugins also can be enabled | ||
- markdown-it-table-of-contents | ||
``` | ||
#### Plugin option | ||
``` yaml | ||
markdown: | ||
plugins: | ||
- name: 'markdown-it-emoji' | ||
options: | ||
shortcuts: | ||
laughing: ':D' | ||
- name: 'other-plugin' | ||
options: ... | ||
``` | ||
## Extensibility | ||
@@ -64,5 +315,19 @@ | ||
hexo.extend.filter.register('markdown-it:renderer', function(md) { | ||
const { config } = this; // Skip this line if you don't need user config from _config.yml | ||
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 | ||
}); | ||
}); | ||
``` | ||
@@ -75,2 +340,3 @@ | ||
## Requests and bug reports | ||
If you have any feature requests or bugs to report, you're welcome to [file an issue](https://github.com/hexojs/hexo-renderer-markdown-it/issues). | ||
@@ -77,0 +343,0 @@ |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
17093
76.47%125
9.65%344
341.03%1
Infinity%+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
Updated
Updated