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 Markdown List Support #32

Merged
merged 16 commits into from
Apr 3, 2017
10 changes: 10 additions & 0 deletions lib/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ function Renderer() {

require('util').inherits(Renderer, MarkedRenderer);

// Support To-Do List
Renderer.prototype.listitem = function(text) {
if (/^\s*\[[x ]\]\s*/.test(text)) {
text = text.replace(/^\s*\[ \]\s*/, '<input type="checkbox"></input> ').replace(/^\s*\[x\]\s*/, '<input type="checkbox" checked></input> ');
return '<li style="list-style: none">' + text + '</li>\n';
} else {
return '<li>' + text + '</li>\n';
}
};

// Add id attribute to headings
Renderer.prototype.heading = function(text, level) {
var transformOption = this.options.modifyAnchors;
Expand Down
24 changes: 24 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,30 @@ describe('Marked renderer', function() {
result.should.eql('<h1 id="中文"><a href="#中文" class="headerlink" title="中文"></a>中文</h1>');
});

it('to-do list testing', function() {
var body = [
'- [ ] test unchecked',
'- [x] test checked',
'- normal list [x] [ ]',
'',
'normal text [x] [ ]',
'',
'[x] [ ] normal text'
].join('\n');

var result = r({text: body});

result.should.eql([
'<ul>',
'<li style="list-style: none"><input type="checkbox"></input> test unchecked</li>',
'<li style="list-style: none"><input type="checkbox" checked></input> test checked</li>',
'<li>normal list [x] [ ]</li>',
'</ul>',
'<p>normal text [x] [ ]</p>',
'<p>[x] [ ] normal text</p>'
].join('\n') + '\n');
});

describe('modifyAnchors option tests', function() {
var body = [
'- [Example](#example)',
Expand Down