Browse Source

Add position and size for lheading

pull/204/head
Samy Pessé 9 years ago
parent
commit
1e7db76630
  1. 6
      lib/rules_block/lheading.js
  2. 22
      test/annotation.js

6
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;
};

22
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);

Loading…
Cancel
Save