Skip to content

Commit

Permalink
Merge pull request #122 from curbengh/renderer-opts
Browse files Browse the repository at this point in the history
feat: support preset and render options
  • Loading branch information
curbengh committed Aug 13, 2020
2 parents e5f08ba + cb9aff4 commit 34e589a
Show file tree
Hide file tree
Showing 10 changed files with 419 additions and 293 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Follow the [installation guide](https://github.com/hexojs/hexo-renderer-markdown

``` yml
markdown:
preset: 'default'
render:
html: true
xhtmlOut: false
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'use strict';

hexo.config.markdown = Object.assign({
preset: 'default',
render: {},
anchors: {}
}, hexo.config.markdown);
Expand Down
27 changes: 17 additions & 10 deletions lib/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@

module.exports = function(data, options) {
const MdIt = require('markdown-it');
const cfg = this.config.markdown;
const opt = cfg ? cfg : 'default';
let parser = opt === 'default' || opt === 'commonmark' || opt === 'zero'
? new MdIt(opt)
: new MdIt(opt.render);

if (opt.plugins) {
parser = opt.plugins.reduce((parser, pugs) => {
let { markdown } = this.config;

// 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`);
}

const { preset, render, plugins, anchors } = markdown;
let parser = new MdIt(preset, render);

if (plugins) {
parser = plugins.reduce((parser, pugs) => {
if (pugs instanceof Object && pugs.name) {
return parser.use(require(pugs.name), pugs.options);
}
Expand All @@ -18,8 +25,8 @@ module.exports = function(data, options) {
}, parser);
}

if (opt.anchors) {
parser = parser.use(require('./anchors'), opt.anchors);
if (anchors) {
parser = parser.use(require('./anchors'), anchors);
}

this.execFilterSync('markdown-it:renderer', parser, { context: this });
Expand Down
36 changes: 18 additions & 18 deletions test/fixtures/outputs/anchors.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ <h2 id="Horizontal-Rule-ver3"><a class="header-anchor" href="#Horizontal-Rule-ve
<hr>
<h2 id="Typographic-replacements"><a class="header-anchor" href="#Typographic-replacements"></a>Typographic replacements</h2>
<p>Enable typographer option to see result.</p>
<p>(c) (C) (r) (R) (tm) (TM) (p) (P) +-</p>
<p>test.. test... test..... test?..... test!....</p>
<p>!!!!!! ???? ,, -- ---</p>
<p>&quot;Smartypants, double quotes&quot; and 'single quotes'</p>
<p>© © ® ® ™ ™ § § ±</p>
<p>test test test test?.. test!..</p>
<p>!!! ??? , – —</p>
<p>Smartypants, double quotes and single quotes</p>
<h2 id="Emphasis"><a class="header-anchor" href="#Emphasis"></a>Emphasis</h2>
<p><strong>This is bold text</strong></p>
<p><strong>This is bold text</strong></p>
Expand All @@ -24,11 +24,11 @@ <h2 id="Emphasis"><a class="header-anchor" href="#Emphasis">¶</a>Emphasis</h2>
<p><s>Strikethrough</s></p>
<h2 id="Blockquotes"><a class="header-anchor" href="#Blockquotes"></a>Blockquotes</h2>
<blockquote>
<p>Blockquotes can also be nested...</p>
<p>Blockquotes can also be nested</p>
<blockquote>
<p>...by using additional greater-than signs right next to each other...</p>
<p>by using additional greater-than signs right next to each other</p>
<blockquote>
<p>...or with spaces between arrows.</p>
<p>or with spaces between arrows.</p>
</blockquote>
</blockquote>
</blockquote>
Expand Down Expand Up @@ -67,7 +67,7 @@ <h2 id="Code"><a class="header-anchor" href="#Code">¶</a>Code</h2>
line 2 of code
line 3 of code
</code></pre>
<p>Block code &quot;fences&quot;</p>
<p>Block code fences</p>
<pre><code>Sample text here...
</code></pre>
<p>Syntax highlighting</p>
Expand Down Expand Up @@ -125,13 +125,13 @@ <h2 id="Tables"><a class="header-anchor" href="#Tables">¶</a>Tables</h2>
</table>
<h2 id="Links"><a class="header-anchor" href="#Links"></a>Links</h2>
<p><a href="http://dev.nodeca.com">link text</a></p>
<p>Autoconverted link https://github.com/nodeca/pica (enable linkify to see)</p>
<p>Autoconverted link <a href="https://github.com/nodeca/pica">https://github.com/nodeca/pica</a> (enable linkify to see)</p>
<h2 id="Images"><a class="header-anchor" href="#Images"></a>Images</h2>
<p><img src="https://octodex.github.com/images/minion.png" alt="Minion">
<p><img src="https://octodex.github.com/images/minion.png" alt="Minion"><br>
<img src="https://octodex.github.com/images/stormtroopocat.jpg" alt="Stormtroopocat" title="The Stormtroopocat"></p>
<p><img src="https://octodex.github.com/images/dojocat.jpg" alt="Alt text" title="The Dojocat"></p>
<h2 id="Plugins"><a class="header-anchor" href="#Plugins"></a>Plugins</h2>
<p>The killer feature of <code>markdown-it</code> is very effective support of
<p>The killer feature of <code>markdown-it</code> is very effective support of<br>
<a href="https://www.npmjs.org/browse/keyword/markdown-it-plugin">syntax plugins</a>.</p>
<h3 id="Emojies"><a class="header-anchor" href="#Emojies"></a><a href="https://github.com/markdown-it/markdown-it-emoji">Emojies</a></h3>
<blockquote>
Expand Down Expand Up @@ -159,7 +159,7 @@ <h3 id="Footnotes"><a class="header-anchor" href="#Footnotes">¶</a><a href="htt
<p>[^second]: Footnote text.</p>
<h3 id="Definition-lists"><a class="header-anchor" href="#Definition-lists"></a><a href="https://github.com/markdown-it/markdown-it-deflist">Definition lists</a></h3>
<p>Term 1</p>
<p>: Definition 1
<p>: Definition 1<br>
with lazy continuation.</p>
<p>Term 2 with <em>inline markup</em></p>
<p>: Definition 2</p>
Expand All @@ -168,16 +168,16 @@ <h3 id="Definition-lists"><a class="header-anchor" href="#Definition-lists">¶</
Third paragraph of definition 2.
</code></pre>
<p><em>Compact style:</em></p>
<p>Term 1
<p>Term 1<br>
~ Definition 1</p>
<p>Term 2
~ Definition 2a
<p>Term 2<br>
~ Definition 2a<br>
~ Definition 2b</p>
<h3 id="Abbreviations"><a class="header-anchor" href="#Abbreviations"></a><a href="https://github.com/markdown-it/markdown-it-abbr">Abbreviations</a></h3>
<p>This is HTML abbreviation example.</p>
<p>It converts &quot;HTML&quot;, but keep intact partial entries like &quot;xxxHTMLyyy&quot; and so on.</p>
<p>It converts HTML, but keep intact partial entries like xxxHTMLyyy and so on.</p>
<p>*[HTML]: Hyper Text Markup Language</p>
<h3 id="Custom-containers"><a class="header-anchor" href="#Custom-containers"></a><a href="https://github.com/markdown-it/markdown-it-container">Custom containers</a></h3>
<p>::: warning
<em>here be dragons</em>
<p>::: warning<br>
<em>here be dragons</em><br>
:::</p>
149 changes: 149 additions & 0 deletions test/fixtures/outputs/commonmark-deprecated.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<h1>h1 Heading em português</h1>
<h2>h2 Heading :P</h2>
<h3>h3 Heading</h3>
<h4>h4 Heading</h4>
<h5>h5 Heading</h5>
<h6>h6 Heading</h6>
<h2>Horizontal Rule</h2>
<hr />
<h2>Horizontal Rule</h2>
<hr />
<h2>Horizontal Rule</h2>
<hr />
<h2>Typographic replacements</h2>
<p>Enable typographer option to see result.</p>
<p>(c) (C) (r) (R) (tm) (TM) (p) (P) +-</p>
<p>test.. test... test..... test?..... test!....</p>
<p>!!!!!! ???? ,, -- ---</p>
<p>&quot;Smartypants, double quotes&quot; and 'single quotes'</p>
<h2>Emphasis</h2>
<p><strong>This is bold text</strong></p>
<p><strong>This is bold text</strong></p>
<p><em>This is italic text</em></p>
<p><em>This is italic text</em></p>
<p>~~Strikethrough~~</p>
<h2>Blockquotes</h2>
<blockquote>
<p>Blockquotes can also be nested...</p>
<blockquote>
<p>...by using additional greater-than signs right next to each other...</p>
<blockquote>
<p>...or with spaces between arrows.</p>
</blockquote>
</blockquote>
</blockquote>
<h2>Lists</h2>
<p>Unordered</p>
<ul>
<li>Create a list by starting a line with <code>+</code>, <code>-</code>, or <code>*</code></li>
<li>Sub-lists are made by indenting 2 spaces:
<ul>
<li>Marker character change forces new list start:
<ul>
<li>Ac tristique libero volutpat at</li>
</ul>
<ul>
<li>Facilisis in pretium nisl aliquet</li>
</ul>
<ul>
<li>Nulla volutpat aliquam velit</li>
</ul>
</li>
</ul>
</li>
<li>Very easy!</li>
</ul>
<p>Ordered</p>
<ol>
<li>Lorem ipsum dolor sit amet</li>
<li>Consectetur adipiscing elit</li>
<li>Integer molestie lorem at massa</li>
</ol>
<h2>Code</h2>
<p>Inline <code>code</code></p>
<p>Indented code</p>
<pre><code>// Some comments
line 1 of code
line 2 of code
line 3 of code
</code></pre>
<p>Block code &quot;fences&quot;</p>
<pre><code>Sample text here...
</code></pre>
<p>Syntax highlighting</p>
<pre><code class="language-js">var foo = function (bar) {
return bar++;
};

console.log(foo(5));
</code></pre>
<h2>Tables</h2>
<p>| Option | Description |
| ------ | ----------- |
| data | path to data files to supply the data that will be passed into templates. |
| engine | engine to be used for processing templates. Handlebars is the default. |
| ext | extension to be used for dest files. |</p>
<p>Right aligned columns</p>
<p>| Option | Description |
| ------:| -----------:|
| data | path to data files to supply the data that will be passed into templates. |
| engine | engine to be used for processing templates. Handlebars is the default. |
| ext | extension to be used for dest files. |</p>
<h2>Links</h2>
<p><a href="http://dev.nodeca.com">link text</a></p>
<p>Autoconverted link https://github.com/nodeca/pica (enable linkify to see)</p>
<h2>Images</h2>
<p><img src="https://octodex.github.com/images/minion.png" alt="Minion" />
<img src="https://octodex.github.com/images/stormtroopocat.jpg" alt="Stormtroopocat" title="The Stormtroopocat" /></p>
<p><img src="https://octodex.github.com/images/dojocat.jpg" alt="Alt text" title="The Dojocat" /></p>
<h2>Plugins</h2>
<p>The killer feature of <code>markdown-it</code> is very effective support of
<a href="https://www.npmjs.org/browse/keyword/markdown-it-plugin">syntax plugins</a>.</p>
<h3><a href="https://github.com/markdown-it/markdown-it-emoji">Emojies</a></h3>
<blockquote>
<p>Classic markup: :wink: :crush: :cry: :tear: :laughing: :yum:</p>
<p>Shortcuts (emoticons): :-) :-( 8-) ;)</p>
</blockquote>
<p>see <a href="https://github.com/markdown-it/markdown-it-emoji#change-output">how to change output</a> with twemoji.</p>
<h3><a href="https://github.com/markdown-it/markdown-it-sub">Subscipt</a> / <a href="https://github.com/markdown-it/markdown-it-sup">Superscirpt</a></h3>
<ul>
<li>19^th^</li>
<li>H~2~O</li>
</ul>
<h3><a href="https://github.com/markdown-it/markdown-it-ins">&lt;ins&gt;</a></h3>
<p>++Inserted text++</p>
<h3><a href="https://github.com/markdown-it/markdown-it-mark">&lt;mark&gt;</a></h3>
<p>==Marked text==</p>
<h3><a href="https://github.com/markdown-it/markdown-it-footnote">Footnotes</a></h3>
<p>Footnote 1 link[^first].</p>
<p>Footnote 2 link[^second].</p>
<p>Inline footnote^[Text of inline footnote] definition.</p>
<p>Duplicated footnote reference[^second].</p>
<p>[^first]: Footnote <strong>can have markup</strong></p>
<pre><code>and multiple paragraphs.
</code></pre>
<p>[^second]: Footnote text.</p>
<h3><a href="https://github.com/markdown-it/markdown-it-deflist">Definition lists</a></h3>
<p>Term 1</p>
<p>: Definition 1
with lazy continuation.</p>
<p>Term 2 with <em>inline markup</em></p>
<p>: Definition 2</p>
<pre><code> { some code, part of Definition 2 }

Third paragraph of definition 2.
</code></pre>
<p><em>Compact style:</em></p>
<p>Term 1
~ Definition 1</p>
<p>Term 2
~ Definition 2a
~ Definition 2b</p>
<h3><a href="https://github.com/markdown-it/markdown-it-abbr">Abbreviations</a></h3>
<p>This is HTML abbreviation example.</p>
<p>It converts &quot;HTML&quot;, but keep intact partial entries like &quot;xxxHTMLyyy&quot; and so on.</p>
<p>*[HTML]: Hyper Text Markup Language</p>
<h3><a href="https://github.com/markdown-it/markdown-it-container">Custom containers</a></h3>
<p>::: warning
<em>here be dragons</em>
:::</p>
Loading

0 comments on commit 34e589a

Please sign in to comment.