Skip to content

Commit

Permalink
Add support for basic description/definition lists (#55)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
hisaac authored and NoahDragon committed Jan 18, 2018
1 parent 0096975 commit 26bc183
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
46 changes: 45 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 `<a href="https://hexo.io">https://hexo.io</a>`.

## 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
<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:

```markdown
Definition Term
: Definition 1
: Definition 2
```

will generate this HTML:

```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.

[Markdown]: https://daringfireball.net/projects/markdown/
[marked]: https://github.com/chjj/marked
[PHP Markdown Extra]: https://michelf.ca/projects/php-markdown/extra/#def-list
20 changes: 20 additions & 0 deletions lib/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.+)(<br>:(\s+))(\S.+)/;

var dl
= '<dl>'
+ '<dt>$2</dt>'
+ '<dd>$5</dd>'
+ '</dl>';

if (text.match(dlTest)) {
result = text.replace(dlTest, dl);
} else {
result = '<p>' + text + '</p>\n';
}

return result;
};

marked.setOptions({
langPrefix: '',
highlight: function(code, lang) {
Expand Down
27 changes: 27 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<br>: This is the Description'});
result.should.eql('<dl><dt>Description Term</dt><dd>This is the Description</dd></dl>');
});

it('should render description lists with multiple spaces after the colon', function() {
var result = r({text: 'Description Term<br>: This is the Description'});
result.should.eql('<dl><dt>Description Term</dt><dd>This is the Description</dd></dl>');
});

it('should render description lists with a tab after the colon', function() {
var result = r({text: 'Description Term<br>: This is the Description'});
result.should.eql('<dl><dt>Description Term</dt><dd>This is the Description</dd></dl>');
});

it('should render description lists with a carriage return after the colon', function() {
var result = r({text: 'Description Term<br>:\nThis is the Description'});
result.should.eql('<dl><dt>Description Term</dt><dd>This is the Description</dd></dl>');
});

it('should not render regular paragraphs as description lists', function() {
var result = r({text: 'Description Term<br>:This is the Description'});
result.should.eql('<p>Description Term<br>:This is the Description</p>\n');
});

describe('autolink option tests', function() {
var ctx = {
config: {
Expand Down

0 comments on commit 26bc183

Please sign in to comment.