Skip to content

Commit 68cc496

Browse files
authored
Fix token.map (#20)
* Fix map Map on the front-matter token doesn't seem right. For a doc like: ~~~md --- a: b --- xyz ~~~ I get back `[0, 12]`. Instead it [should be `[startLine, endLine]`](https://markdown-it.github.io/markdown-it/#Token.prototype.map) * Add test
1 parent 6f75729 commit 68cc496

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ module.exports = function front_matter_plugin(md, cb) {
115115
token.hidden = true;
116116
token.markup = state.src.slice(startLine, pos);
117117
token.block = true;
118-
token.map = [ startLine, pos ];
118+
token.map = [ startLine, nextLine + (auto_closed ? 1 : 0) ];
119119
token.meta = state.src.slice(start_content, start - 1);
120120

121121
state.parentType = old_parent;

test/index.js

+29
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,33 @@ describe('Markdown It Front Matter', () => {
107107

108108
assert.equal(foundFrontmatter, 'x: 1\n---');
109109
});
110+
111+
it('Should set correct map for front-matter token', () => {
112+
{
113+
const tokens = md.parse([
114+
'----',
115+
'x: 1',
116+
'---',
117+
'# Head'
118+
].join('\n'));
119+
120+
assert.strictEqual(tokens[0].type, 'front_matter');
121+
assert.deepStrictEqual(tokens[0].map, [0, 4]);
122+
}
123+
{
124+
const tokens = md.parse([
125+
'----',
126+
'title: Associative arrays',
127+
'people:',
128+
' name: John Smith',
129+
' age: 33',
130+
'morePeople: { name: Grace Jones, age: 21 }',
131+
'---',
132+
'# Head'
133+
].join('\n'));
134+
135+
assert.strictEqual(tokens[0].type, 'front_matter');
136+
assert.deepStrictEqual(tokens[0].map, [0, 8]);
137+
}
138+
});
110139
});

0 commit comments

Comments
 (0)