| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- 'use strict';
- module.exports = atxHeading;
- var C_NEWLINE = '\n';
- var C_TAB = '\t';
- var C_SPACE = ' ';
- var C_HASH = '#';
- var MAX_ATX_COUNT = 6;
- function atxHeading(eat, value, silent) {
- var self = this;
- var settings = self.options;
- var length = value.length + 1;
- var index = -1;
- var now = eat.now();
- var subvalue = '';
- var content = '';
- var character;
- var queue;
- var depth;
- /* Eat initial spacing. */
- while (++index < length) {
- character = value.charAt(index);
- if (character !== C_SPACE && character !== C_TAB) {
- index--;
- break;
- }
- subvalue += character;
- }
- /* Eat hashes. */
- depth = 0;
- while (++index <= length) {
- character = value.charAt(index);
- if (character !== C_HASH) {
- index--;
- break;
- }
- subvalue += character;
- depth++;
- }
- if (depth > MAX_ATX_COUNT) {
- return;
- }
- if (
- !depth ||
- (!settings.pedantic && value.charAt(index + 1) === C_HASH)
- ) {
- return;
- }
- length = value.length + 1;
- /* Eat intermediate white-space. */
- queue = '';
- while (++index < length) {
- character = value.charAt(index);
- if (character !== C_SPACE && character !== C_TAB) {
- index--;
- break;
- }
- queue += character;
- }
- /* Exit when not in pedantic mode without spacing. */
- if (
- !settings.pedantic &&
- queue.length === 0 &&
- character &&
- character !== C_NEWLINE
- ) {
- return;
- }
- if (silent) {
- return true;
- }
- /* Eat content. */
- subvalue += queue;
- queue = '';
- content = '';
- while (++index < length) {
- character = value.charAt(index);
- if (!character || character === C_NEWLINE) {
- break;
- }
- if (
- character !== C_SPACE &&
- character !== C_TAB &&
- character !== C_HASH
- ) {
- content += queue + character;
- queue = '';
- continue;
- }
- while (character === C_SPACE || character === C_TAB) {
- queue += character;
- character = value.charAt(++index);
- }
- while (character === C_HASH) {
- queue += character;
- character = value.charAt(++index);
- }
- while (character === C_SPACE || character === C_TAB) {
- queue += character;
- character = value.charAt(++index);
- }
- index--;
- }
- now.column += subvalue.length;
- now.offset += subvalue.length;
- subvalue += content + queue;
- return eat(subvalue)({
- type: 'heading',
- depth: depth,
- children: self.tokenizeInline(content, now)
- });
- }
|