Browse Source

Add position and size for heading tokens

pull/204/head
Samy Pessé 9 years ago
parent
commit
daad6568b3
  1. 10
      lib/rules_block/heading.js
  2. 14
      lib/token.js

10
lib/rules_block/heading.js

@ -6,11 +6,13 @@ var isSpace = require('../common/utils').isSpace;
module.exports = function heading(state, startLine, endLine, silent) {
var ch, level, tmp, token,
var ch, level, tmp, token, originalPos, originalMax,
pos = state.bMarks[startLine] + state.tShift[startLine],
max = state.eMarks[startLine];
ch = state.src.charCodeAt(pos);
originalPos = pos;
originalMax = max;
if (ch !== 0x23/* # */ || pos >= max) { return false; }
@ -39,14 +41,20 @@ module.exports = function heading(state, startLine, endLine, silent) {
token = state.push('heading_open', 'h' + String(level), 1);
token.markup = '########'.slice(0, level);
token.map = [ startLine, state.line ];
token.position = originalPos;
token.size = pos - originalPos;
token = state.push('inline', '', 0);
token.content = state.src.slice(pos, max).trim();
token.map = [ startLine, state.line ];
token.children = [];
token.position = pos;
token.size = max - pos;
token = state.push('heading_close', 'h' + String(level), -1);
token.markup = '########'.slice(0, level);
token.position = max;
token.size = originalMax - max;
return true;
};

14
lib/token.js

@ -110,6 +110,20 @@ function Token(type, tag, nesting) {
* to hide paragraphs.
**/
this.hidden = false;
/**
* Token#position -> Number
*
* Position in the original string
**/
this.position = 0;
/**
* Token#size -> Number
*
* Size of the token
**/
this.size = 0;
}

Loading…
Cancel
Save