Skip to content

Commit

Permalink
Merge pull request markedjs#1144 from paulroub/OL_initial_numbers
Browse files Browse the repository at this point in the history
Start ordered lists using the initial numbers from markdown lists
  • Loading branch information
joshbruce committed Mar 21, 2018
2 parents 907f44d + 256b2be commit 8d590cb
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 9 deletions.
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

0 comments on commit 8d590cb

Please sign in to comment.