From 5ee2b796b86c49f4748624f36a46e26cbac07018 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Mon, 30 Nov 2020 11:01:19 +0800 Subject: [PATCH 1/2] feat: add descriptionLists options --- index.js | 3 ++- lib/renderer.js | 12 ++++++++---- test/index.js | 6 ++++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 915fb99..43b1202 100644 --- a/index.js +++ b/index.js @@ -24,7 +24,8 @@ hexo.config.marked = Object.assign({ enable: false, exclude: [], nofollow: false - } + }, + descriptionLists: true }, hexo.config.marked); renderer.disableNunjucks = Boolean(hexo.config.marked.disableNunjucks); diff --git a/lib/renderer.js b/lib/renderer.js index bbcfde8..865de36 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -7,6 +7,7 @@ const MarkedRenderer = marked.Renderer; const MarkedTokenizer = marked.Tokenizer; const { basename, dirname, extname, join } = require('path').posix; const rATag = /]+\s+?)?href=["'](?:#)([^<>"']+)["'][^<>]*>/i; +const rDlSyntax = /(?:^|\s)(\S.+)
:\s+(\S.+)/; const anchorId = (str, transformOption) => { return slugize(str.trim(), {transform: transformOption}); @@ -99,11 +100,14 @@ class Renderer extends MarkedRenderer { // Support Basic Description Lists paragraph(text) { - const dlTest = /(?:^|\s)(\S.+)
:\s+(\S.+)/; - const dl = '
$1
$2
'; - if (dlTest.test(text)) { - return text.replace(dlTest, dl); + const { descriptionLists = true } = this.options; + + if (descriptionLists) { + if (rDlSyntax.test(text)) { + return text.replace(rDlSyntax, '
$1
$2
'); + } } + return `

${text}

\n`; } diff --git a/test/index.js b/test/index.js index a86343a..664ec72 100644 --- a/test/index.js +++ b/test/index.js @@ -140,6 +140,12 @@ describe('Marked renderer', () => { }); // Description List tests + it('shouldn\'t render description lists when options.descriptionLists is disabled', () => { + hexo.config.marked.descriptionLists = false; + + const result = r({text: 'Description Term
: This is the Description'}); + result.should.eql('

Description Term
: This is the Description

\n'); + }); it('should render description lists with a single space after the colon', () => { const result = r({text: 'Description Term
: This is the Description'}); From cef174337d72c38fab90ea00009126da423a88f9 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Mon, 30 Nov 2020 11:07:02 +0800 Subject: [PATCH 2/2] docs: descriptionLists option --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index fdec0de..e2a04b3 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ marked: exclude: [] nofollow: false disableNunjucks: false + descriptionLists: true ``` - **gfm** - Enables [GitHub flavored markdown](https://help.github.com/articles/github-flavored-markdown) @@ -82,6 +83,9 @@ marked: * **nofollow** - Add `rel="noopener external nofollow noreferrer"` to all external links for security, privacy and SEO. [Read more](https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types). _This can be enabled regardless of `external_link.enable`_ - Example: `[foo](http://bar.com)` becomes `foo` - **disableNunjucks**: If true, Nunjucks tags `{{ }}` or `{% %}` (usually used by [tag plugins](https://hexo.io/docs/tag-plugins)) will not be rendered. +- **descriptionLists**: Enable support for [description lists syntax](https://kramdown.gettalong.org/syntax.html#definition-lists). + * Currently description lists syntax is not in neither [CommonMark](http://commonmark.org/) or [GFM](https://github.github.com/gfm/#task-list-items-extension-), `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](https://marked.js.org/using_advanced#options). 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](https://github.com/hexojs/hexo-renderer-marked/issues) to us for clarification.