Skip to content

Commit

Permalink
Fix token.map (#20)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mjbvz committed Apr 4, 2024
1 parent 6f75729 commit 68cc496
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -115,7 +115,7 @@ module.exports = function front_matter_plugin(md, cb) {
token.hidden = true;
token.markup = state.src.slice(startLine, pos);
token.block = true;
token.map = [ startLine, pos ];
token.map = [ startLine, nextLine + (auto_closed ? 1 : 0) ];
token.meta = state.src.slice(start_content, start - 1);

state.parentType = old_parent;
Expand Down
29 changes: 29 additions & 0 deletions test/index.js
Expand Up @@ -107,4 +107,33 @@ describe('Markdown It Front Matter', () => {

assert.equal(foundFrontmatter, 'x: 1\n---');
});

it('Should set correct map for front-matter token', () => {
{
const tokens = md.parse([
'----',
'x: 1',
'---',
'# Head'
].join('\n'));

assert.strictEqual(tokens[0].type, 'front_matter');
assert.deepStrictEqual(tokens[0].map, [0, 4]);
}
{
const tokens = md.parse([
'----',
'title: Associative arrays',
'people:',
' name: John Smith',
' age: 33',
'morePeople: { name: Grace Jones, age: 21 }',
'---',
'# Head'
].join('\n'));

assert.strictEqual(tokens[0].type, 'front_matter');
assert.deepStrictEqual(tokens[0].map, [0, 8]);
}
});
});

0 comments on commit 68cc496

Please sign in to comment.