From 68cc4965a568f0e22bbbe926cb099268b39f6523 Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Wed, 3 Apr 2024 21:24:47 -0700 Subject: [PATCH] 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 --- index.js | 2 +- test/index.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index fefc79a..329e38e 100644 --- a/index.js +++ b/index.js @@ -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; diff --git a/test/index.js b/test/index.js index 85771eb..f8a990f 100644 --- a/test/index.js +++ b/test/index.js @@ -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]); + } + }); });