开源软件名称(OpenSource Name):hexojs/hexo-renderer-marked开源软件地址(OpenSource Url):https://github.com/hexojs/hexo-renderer-marked开源编程语言(OpenSource Language):JavaScript 100.0%开源软件介绍(OpenSource Introduction):hexo-renderer-markedAdd support for Markdown. This plugin uses marked as its render engine. Important note on securityBy 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:
Installation$ npm install hexo-renderer-marked --save
OptionsYou can configure this plugin in 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
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. ExtrasSanitize HTML with DOMPurifyDOMPurify 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
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. ExtensibilityThis 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 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 TokenizerIt 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
};
}
}
}); |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论