* Remove pinning of eslint-plugin-markdown An issue affecting Node.js source has been fixed in eslint-plugin-markdown so we don't need to pin it to beta-4 anymore. Refs: https://github.com/eslint/eslint-plugin-markdown/issues/69 * Update eslint-plugin-markdown up to 1.0.0-beta.7 * Fix docs for eslint-plugin-markdown@1.0.0-beta.7 PR-URL: https://github.com/nodejs/node/pull/14047 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
116 lines
2.2 KiB
JavaScript
116 lines
2.2 KiB
JavaScript
/**
|
|
* @author Titus Wormer
|
|
* @copyright 2015 Titus Wormer
|
|
* @license MIT
|
|
* @module remark:parse:tokenize:heading-setext
|
|
* @fileoverview Tokenise an setext-style heading.
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
module.exports = setextHeading;
|
|
|
|
var C_NEWLINE = '\n';
|
|
var C_TAB = '\t';
|
|
var C_SPACE = ' ';
|
|
var C_EQUALS = '=';
|
|
var C_DASH = '-';
|
|
|
|
var MAX_HEADING_INDENT = 3;
|
|
|
|
/* Map of characters which can be used to mark setext
|
|
* headers, mapping to their corresponding depth. */
|
|
var SETEXT_MARKERS = {};
|
|
|
|
SETEXT_MARKERS[C_EQUALS] = 1;
|
|
SETEXT_MARKERS[C_DASH] = 2;
|
|
|
|
/* Tokenise an setext-style heading. */
|
|
function setextHeading(eat, value, silent) {
|
|
var self = this;
|
|
var now = eat.now();
|
|
var length = value.length;
|
|
var index = -1;
|
|
var subvalue = '';
|
|
var content;
|
|
var queue;
|
|
var character;
|
|
var marker;
|
|
var depth;
|
|
|
|
/* Eat initial indentation. */
|
|
while (++index < length) {
|
|
character = value.charAt(index);
|
|
|
|
if (character !== C_SPACE || index >= MAX_HEADING_INDENT) {
|
|
index--;
|
|
break;
|
|
}
|
|
|
|
subvalue += character;
|
|
}
|
|
|
|
/* Eat content. */
|
|
content = '';
|
|
queue = '';
|
|
|
|
while (++index < length) {
|
|
character = value.charAt(index);
|
|
|
|
if (character === C_NEWLINE) {
|
|
index--;
|
|
break;
|
|
}
|
|
|
|
if (character === C_SPACE || character === C_TAB) {
|
|
queue += character;
|
|
} else {
|
|
content += queue + character;
|
|
queue = '';
|
|
}
|
|
}
|
|
|
|
now.column += subvalue.length;
|
|
now.offset += subvalue.length;
|
|
subvalue += content + queue;
|
|
|
|
/* Ensure the content is followed by a newline and a
|
|
* valid marker. */
|
|
character = value.charAt(++index);
|
|
marker = value.charAt(++index);
|
|
|
|
if (character !== C_NEWLINE || !SETEXT_MARKERS[marker]) {
|
|
return;
|
|
}
|
|
|
|
subvalue += character;
|
|
|
|
/* Eat Setext-line. */
|
|
queue = marker;
|
|
depth = SETEXT_MARKERS[marker];
|
|
|
|
while (++index < length) {
|
|
character = value.charAt(index);
|
|
|
|
if (character !== marker) {
|
|
if (character !== C_NEWLINE) {
|
|
return;
|
|
}
|
|
|
|
index--;
|
|
break;
|
|
}
|
|
|
|
queue += character;
|
|
}
|
|
|
|
if (silent) {
|
|
return true;
|
|
}
|
|
|
|
return eat(subvalue + queue)({
|
|
type: 'heading',
|
|
depth: depth,
|
|
children: self.tokenizeInline(content, now)
|
|
});
|
|
}
|