Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for basic description/definition lists #55

Merged
merged 4 commits into from
Jan 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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