From 26bc1831be4ec3440aff627850cfd21c685589c1 Mon Sep 17 00:00:00 2001 From: Isaac Halvorson Date: Thu, 18 Jan 2018 22:59:58 +0530 Subject: [PATCH] Add support for basic description/definition lists (#55) * Add support for basic description lists * Add tests, and fix regex based on test results * Fix eslint errors * Update README to include documentation on definition/description lists --- README.md | 46 +++++++++++++++++++++++++++++++++++++++++++++- lib/renderer.js | 20 ++++++++++++++++++++ test/index.js | 27 +++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f4b1837..4a8ad24 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![NPM Dependencies](https://david-dm.org/hexojs/hexo-renderer-marked.svg)](https://david-dm.org/hexojs/hexo-renderer-marked) [![NPM DevDependencies](https://david-dm.org/hexojs/hexo-renderer-marked/dev-status.svg)](https://david-dm.org/hexojs/hexo-renderer-marked?type=dev) -Add support for [Markdown]. This plugin uses [marked] as render engine. +Add support for [Markdown]. This plugin uses [marked] as its render engine. ## Installation @@ -42,5 +42,49 @@ marked: - **modifyAnchors** - Use for transform anchorIds. if 1 to lowerCase and if 2 to upperCase. - **autolink** - Enable autolink for URLs. E.g. `https://hexo.io` will become `https://hexo.io`. +## Extras + +### Definition/Description Lists + +`hexo-renderer-marked` also implements description/definition lists using the same syntax as [PHP Markdown Extra][PHP Markdown Extra]. + +This Markdown: + +```markdown +Definition Term +: This is the definition for the term +``` + +will generate this html: + +```html +
+
Definition Term
+
This is the definition for the term
+
+``` + +Note: There is currently a limitation in this implementation. If multiple definitions are provided, the rendered HTML will be incorrect. + +For example, this Markdown: + +```markdown +Definition Term +: Definition 1 +: Definition 2 +``` + +will generate this HTML: + +```html +
+
Definition Term
: Definition 1
+
Definition 2
+
+``` + +If you've got ideas on how to support multiple definitions, please provide a pull request. We'd love to support it. + [Markdown]: https://daringfireball.net/projects/markdown/ [marked]: https://github.com/chjj/marked +[PHP Markdown Extra]: https://michelf.ca/projects/php-markdown/extra/#def-list diff --git a/lib/renderer.js b/lib/renderer.js index b0cd9a7..a8a1d2a 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -83,6 +83,26 @@ Renderer.prototype.link = function(href, title, text) { return out; }; +// Support Basic Description Lists +Renderer.prototype.paragraph = function(text) { + var result = ''; + var dlTest = /(^|\s)(\S.+)(
:(\s+))(\S.+)/; + + var dl + = '
' + + '
$2
' + + '
$5
' + + '
'; + + if (text.match(dlTest)) { + result = text.replace(dlTest, dl); + } else { + result = '

' + text + '

\n'; + } + + return result; +}; + marked.setOptions({ langPrefix: '', highlight: function(code, lang) { diff --git a/test/index.js b/test/index.js index 7b9845a..cce65cf 100644 --- a/test/index.js +++ b/test/index.js @@ -83,6 +83,33 @@ describe('Marked renderer', function() { ].join('')); }); + // Description List tests + + it('should render description lists with a single space after the colon', function() { + var result = r({text: 'Description Term
: This is the Description'}); + result.should.eql('
Description Term
This is the Description
'); + }); + + it('should render description lists with multiple spaces after the colon', function() { + var result = r({text: 'Description Term
: This is the Description'}); + result.should.eql('
Description Term
This is the Description
'); + }); + + it('should render description lists with a tab after the colon', function() { + var result = r({text: 'Description Term
: This is the Description'}); + result.should.eql('
Description Term
This is the Description
'); + }); + + it('should render description lists with a carriage return after the colon', function() { + var result = r({text: 'Description Term
:\nThis is the Description'}); + result.should.eql('
Description Term
This is the Description
'); + }); + + it('should not render regular paragraphs as description lists', function() { + var result = r({text: 'Description Term
:This is the Description'}); + result.should.eql('

Description Term
:This is the Description

\n'); + }); + describe('autolink option tests', function() { var ctx = { config: {