Skip to content

Commit 5d3d70a

Browse files
authored
Merge pull request #1144 from paulroub/OL_initial_numbers
Start ordered lists using the initial numbers from markdown lists
2 parents 002c565 + f69a82f commit 5d3d70a

File tree

5 files changed

+53
-9
lines changed

5 files changed

+53
-9
lines changed

lib/marked.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ Lexer.prototype.token = function(src, top) {
164164
space,
165165
i,
166166
tag,
167-
l;
167+
l,
168+
isordered;
168169

169170
while (src) {
170171
// newline
@@ -279,10 +280,12 @@ Lexer.prototype.token = function(src, top) {
279280
if (cap = this.rules.list.exec(src)) {
280281
src = src.substring(cap[0].length);
281282
bull = cap[2];
283+
isordered = bull.length > 1;
282284

283285
this.tokens.push({
284286
type: 'list_start',
285-
ordered: bull.length > 1
287+
ordered: isordered,
288+
start: isordered ? +bull : ''
286289
});
287290

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

839-
Renderer.prototype.list = function(body, ordered) {
840-
var type = ordered ? 'ol' : 'ul';
841-
return '<' + type + '>\n' + body + '</' + type + '>\n';
842+
Renderer.prototype.list = function(body, ordered, start) {
843+
var type = ordered ? 'ol' : 'ul',
844+
startatt = (ordered && start !== 1) ? (' start="' + start + '"') : '';
845+
return '<' + type + startatt + '>\n' + body + '</' + type + '>\n';
842846
};
843847

844848
Renderer.prototype.listitem = function(text) {
@@ -1099,13 +1103,14 @@ Parser.prototype.tok = function() {
10991103
}
11001104
case 'list_start': {
11011105
body = '';
1102-
var ordered = this.token.ordered;
1106+
var ordered = this.token.ordered,
1107+
start = this.token.start;
11031108

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

1108-
return this.renderer.list(body, ordered);
1113+
return this.renderer.list(body, ordered, start);
11091114
}
11101115
case 'list_item_start': {
11111116
body = '';

marked.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/integration/marked-spec.js

+13
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,16 @@ var marked = require('../../marked.min.js');
33
it('should run the test', function () {
44
expect(marked('Hello World!')).toBe('<p>Hello World!</p>\n');
55
});
6+
7+
// http://spec.commonmark.org/0.28/#example-230
8+
it('should start an ordered list at 0 when requested', function () {
9+
expect(
10+
marked('0. ok')).
11+
toBe("<ol start=\"0\">\n<li>ok</li>\n</ol>\n")
12+
});
13+
14+
// http://spec.commonmark.org/0.28/#example-234
15+
it('indents code within an explicitly-started ordered list', function () {
16+
expect(marked(" 10. foo\n\n bar")).
17+
toBe("<ol start=\"10\">\n<li><p>foo</p>\n<pre><code>bar\n</code></pre></li>\n</ol>\n");
18+
});

test/original/ordered_and_unordered_lists.html

+16
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,19 @@ <h2>Nested</h2>
146146

147147
<p>that</p></li>
148148
</ul>
149+
150+
151+
<p>Ordered lists start from initial number:</p>
152+
153+
<ol start="3">
154+
<li>Three</li>
155+
<li>Four</li>
156+
</ol>
157+
158+
159+
<p>Ordered lists start from initial zero:</p>
160+
161+
<ol start="0">
162+
<li>Zero</li>
163+
<li>One</li>
164+
</ol>

test/original/ordered_and_unordered_lists.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Multiple paragraphs:
8888

8989
Item 2. graf two. The quick brown fox jumped over the lazy dog's
9090
back.
91-
91+
9292
2. Item 2.
9393

9494
3. Item 3.
@@ -129,3 +129,13 @@ This was an error in Markdown 1.0.1:
129129
* sub
130130

131131
that
132+
133+
Ordered lists start from initial number:
134+
135+
3. Three
136+
1. Four
137+
138+
Ordered lists start from initial zero:
139+
140+
0. Zero
141+
1. One

0 commit comments

Comments
 (0)