Skip to content

Commit

Permalink
Parser: Set ordered list_item_open token info to input marker.
Browse files Browse the repository at this point in the history
For instance, in a list

    1. Item 1
    20. Item 2

the first list_item_open token will have token.info === '1', and the
second will have token.info === '20'.

This is useful for later rendering plugins that might want to
use the actual markers.
  • Loading branch information
kohler authored and Vitaly Puzrin committed Aug 2, 2021
1 parent 77fb937 commit 8bcc82a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/rules_block/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ module.exports = function list(state, startLine, endLine, silent) {
if ((posAfterMarker = skipOrderedListMarker(state, startLine)) >= 0) {
isOrdered = true;
start = state.bMarks[startLine] + state.tShift[startLine];
markerValue = Number(state.src.substr(start, posAfterMarker - start - 1));
markerValue = Number(state.src.slice(start, posAfterMarker - 1));

// If we're starting a new ordered list right after
// a paragraph, it should start with 1.
Expand Down Expand Up @@ -254,6 +254,9 @@ module.exports = function list(state, startLine, endLine, silent) {
token = state.push('list_item_open', 'li', 1);
token.markup = String.fromCharCode(markerCharCode);
token.map = itemLines = [ startLine, 0 ];
if (isOrdered) {
token.info = state.src.slice(start, posAfterMarker - 1);
}

// change current state, then restore it after parser subcall
oldTight = state.tight;
Expand Down Expand Up @@ -330,6 +333,7 @@ module.exports = function list(state, startLine, endLine, silent) {
if (isOrdered) {
posAfterMarker = skipOrderedListMarker(state, nextLine);
if (posAfterMarker < 0) { break; }
start = state.bMarks[nextLine] + state.tShift[nextLine];
} else {
posAfterMarker = skipBulletListMarker(state, nextLine);
if (posAfterMarker < 0) { break; }
Expand Down
1 change: 1 addition & 0 deletions lib/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ function Token(type, tag, nesting) {
*
* - Info string for "fence" tokens
* - The value "auto" for autolink "link_open" and "link_close" tokens
* - The string value of the item marker for ordered-list "list_item_open" tokens
**/
this.info = '';

Expand Down
35 changes: 35 additions & 0 deletions test/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,41 @@ describe('smartquotes', function () {
});


describe('Ordered list info', function () {
var md = markdownit();

function type_filter(tokens, type) {
return tokens.filter(function (t) { return t.type === type; });
}

it('Should mark ordered list item tokens with info', function () {
var tokens = md.parse('1. Foo\n2. Bar\n20. Fuzz');
assert.strictEqual(type_filter(tokens, 'ordered_list_open').length, 1);
tokens = type_filter(tokens, 'list_item_open');
assert.strictEqual(tokens.length, 3);
assert.strictEqual(tokens[0].info, '1');
assert.strictEqual(tokens[0].markup, '.');
assert.strictEqual(tokens[1].info, '2');
assert.strictEqual(tokens[1].markup, '.');
assert.strictEqual(tokens[2].info, '20');
assert.strictEqual(tokens[2].markup, '.');

tokens = md.parse(' 1. Foo\n2. Bar\n 20. Fuzz\n 199. Flp');
assert.strictEqual(type_filter(tokens, 'ordered_list_open').length, 1);
tokens = type_filter(tokens, 'list_item_open');
assert.strictEqual(tokens.length, 4);
assert.strictEqual(tokens[0].info, '1');
assert.strictEqual(tokens[0].markup, '.');
assert.strictEqual(tokens[1].info, '2');
assert.strictEqual(tokens[1].markup, '.');
assert.strictEqual(tokens[2].info, '20');
assert.strictEqual(tokens[2].markup, '.');
assert.strictEqual(tokens[3].info, '199');
assert.strictEqual(tokens[3].markup, '.');
});
});


describe('Token attributes', function () {
it('.attrJoin', function () {
var md = markdownit();
Expand Down

0 comments on commit 8bcc82a

Please sign in to comment.