UNPKG

markdown-it-synapse-heading

Version:
70 lines (53 loc) 1.9 kB
// Process '## headings' 'use strict'; module.exports = function synapse_heading_plugin(md) { function isSpace(code) { switch (code) { case 0x09: case 0x20: return true; } return false; } function synapse_heading(state, startLine, endLine, silent) { var ch, level, tmp, token, pos = state.bMarks[startLine] + state.tShift[startLine], max = state.eMarks[startLine]; ch = state.src.charCodeAt(pos); if (ch !== 0x23/* # */ || pos >= max) { return false; } // count heading level level = 1; ch = state.src.charCodeAt(++pos); while (ch === 0x23/* # */ && pos < max && level <= 6) { level++; ch = state.src.charCodeAt(++pos); } if (level > 6) { return false; } if (silent) { return true; } // Let's cut tails like ' ### ' from the end of string max = state.skipSpacesBack(max, pos); tmp = state.skipCharsBack(max, 0x23, pos); // # if (tmp > pos && isSpace(state.src.charCodeAt(tmp - 1))) { max = tmp; } state.line = startLine + 1; token = state.push('synapse_heading_open', 'h' + String(level), 1); token.markup = '########'.slice(0, level); token.map = [ startLine, state.line ]; if (ch !== 0x21/* ! */) { token.attrs = [ [ 'toc', 'true' ] ]; } else { token.markup += ch; pos++; } token = state.push('inline', '', 0); token.content = state.src.slice(pos, max).trim(); token.map = [ startLine, state.line ]; token.children = []; token = state.push('synapse_heading_close', 'h' + String(level), -1); token.markup = '########'.slice(0, level); return true; } var rulesCanBeTerminated = [ 'paragraph', 'reference', 'blockquote' ]; md.block.ruler.after('fence', 'synapse_heading', synapse_heading, { alt: (rulesCanBeTerminated).slice() }); };