From 1e7db76630759786ac61a752a77426722d41c675 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Wed, 24 Feb 2016 15:03:19 +0100 Subject: [PATCH] Add position and size for lheading --- lib/rules_block/lheading.js | 6 ++++++ test/annotation.js | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/rules_block/lheading.js b/lib/rules_block/lheading.js index 482084c..e22d817 100644 --- a/lib/rules_block/lheading.js +++ b/lib/rules_block/lheading.js @@ -62,14 +62,20 @@ module.exports = function lheading(state, startLine, endLine/*, silent*/) { token = state.push('heading_open', 'h' + String(level), 1); token.markup = String.fromCharCode(marker); token.map = [ startLine, state.line ]; + token.position = state.bMarks[startLine]; + token.size = 0; token = state.push('inline', '', 0); token.content = content; token.map = [ startLine, state.line - 1 ]; token.children = []; + token.position = state.bMarks[startLine]; + token.size = content.length; token = state.push('heading_close', 'h' + String(level), -1); token.markup = String.fromCharCode(marker); + token.position = state.bMarks[state.line - 1]; + token.size = state.bMarks[state.line] - state.bMarks[state.line - 1]; return true; }; diff --git a/test/annotation.js b/test/annotation.js index e863012..811084d 100644 --- a/test/annotation.js +++ b/test/annotation.js @@ -2,6 +2,10 @@ var assert = require('chai').assert; +function asertTokenContent(src, token, content) { + assert.strictEqual(src.slice(token.position, token.position + token.size), content); +} + describe.only('Annotation', function() { var md = require('../')({ html: true, @@ -52,6 +56,24 @@ describe.only('Annotation', function() { assert.strictEqual(tokens[5].size, 2); }); + it('should annotate lheadings', function () { + var src = 'Hello\n=====\n\nWorld\n======='; + var tokens = md.parse(src); + assert.strictEqual(tokens.length, 6); + + // First heading + assert.strictEqual(tokens[0].position, 0); + asertTokenContent(src, tokens[0], ''); + asertTokenContent(src, tokens[1], 'Hello'); + asertTokenContent(src, tokens[2], '=====\n'); + + // Second heading + assert.strictEqual(tokens[3].position, 13); + assert.strictEqual(tokens[3].size, 0); + asertTokenContent(src, tokens[4], 'World'); + asertTokenContent(src, tokens[5], '======='); + }); + it('should annotate code blocks', function () { var tokens = md.parse('\tHello\n\tWorld\n\nt\n\n\tBlock 2\n'); assert.strictEqual(tokens.length, 5);