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

Start ordered lists using the initial numbers from markdown lists #1144

Merged
merged 4 commits into from Mar 21, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 12 additions & 7 deletions lib/marked.js
Expand Up @@ -164,7 +164,8 @@ Lexer.prototype.token = function(src, top) {
space,
i,
tag,
l;
l,
isordered;

while (src) {
// newline
Expand Down Expand Up @@ -279,10 +280,12 @@ Lexer.prototype.token = function(src, top) {
if (cap = this.rules.list.exec(src)) {
src = src.substring(cap[0].length);
bull = cap[2];
isordered = bull.length > 1;

this.tokens.push({
type: 'list_start',
ordered: bull.length > 1
ordered: isordered,
start: isordered ? +bull : ''
});

// Get each top-level item.
Expand Down Expand Up @@ -836,9 +839,10 @@ Renderer.prototype.hr = function() {
return this.options.xhtml ? '<hr/>\n' : '<hr>\n';
};

Renderer.prototype.list = function(body, ordered) {
var type = ordered ? 'ol' : 'ul';
return '<' + type + '>\n' + body + '</' + type + '>\n';
Renderer.prototype.list = function(body, ordered, start) {
var type = ordered ? 'ol' : 'ul',
startatt = (ordered && start !== 1) ? (' start="' + start + '"') : '';
return '<' + type + startatt + '>\n' + body + '</' + type + '>\n';
};

Renderer.prototype.listitem = function(text) {
Expand Down Expand Up @@ -1099,13 +1103,14 @@ Parser.prototype.tok = function() {
}
case 'list_start': {
body = '';
var ordered = this.token.ordered;
var ordered = this.token.ordered,
start = this.token.start;

while (this.next().type !== 'list_end') {
body += this.tok();
}

return this.renderer.list(body, ordered);
return this.renderer.list(body, ordered, start);
}
case 'list_item_start': {
body = '';
Expand Down
2 changes: 1 addition & 1 deletion marked.min.js

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions test/integration/marked-spec.js
Expand Up @@ -3,3 +3,16 @@ var marked = require('../../marked.min.js');
it('should run the test', function () {
expect(marked('Hello World!')).toBe('<p>Hello World!</p>\n');
});

// http://spec.commonmark.org/0.28/#example-230
it('should start an ordered list at 0 when requested', function () {
expect(
marked('0. ok')).
toBe("<ol start=\"0\">\n<li>ok</li>\n</ol>\n")
});

// http://spec.commonmark.org/0.28/#example-234
it('indents code within an explicitly-started ordered list', function () {
expect(marked(" 10. foo\n\n bar")).
toBe("<ol start=\"10\">\n<li><p>foo</p>\n<pre><code>bar\n</code></pre></li>\n</ol>\n");
});
16 changes: 16 additions & 0 deletions test/original/ordered_and_unordered_lists.html
Expand Up @@ -146,3 +146,19 @@ <h2>Nested</h2>

<p>that</p></li>
</ul>


<p>Ordered lists start from initial number:</p>

<ol start="3">
<li>Three</li>
<li>Four</li>
</ol>


<p>Ordered lists start from initial zero:</p>

<ol start="0">
<li>Zero</li>
<li>One</li>
</ol>
12 changes: 11 additions & 1 deletion test/original/ordered_and_unordered_lists.md
Expand Up @@ -88,7 +88,7 @@ Multiple paragraphs:

Item 2. graf two. The quick brown fox jumped over the lazy dog's
back.

2. Item 2.

3. Item 3.
Expand Down Expand Up @@ -129,3 +129,13 @@ This was an error in Markdown 1.0.1:
* sub

that

Ordered lists start from initial number:

3. Three
1. Four

Ordered lists start from initial zero:

0. Zero
1. One