heading-atx.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. 'use strict';
  2. module.exports = atxHeading;
  3. var C_NEWLINE = '\n';
  4. var C_TAB = '\t';
  5. var C_SPACE = ' ';
  6. var C_HASH = '#';
  7. var MAX_ATX_COUNT = 6;
  8. function atxHeading(eat, value, silent) {
  9. var self = this;
  10. var settings = self.options;
  11. var length = value.length + 1;
  12. var index = -1;
  13. var now = eat.now();
  14. var subvalue = '';
  15. var content = '';
  16. var character;
  17. var queue;
  18. var depth;
  19. /* Eat initial spacing. */
  20. while (++index < length) {
  21. character = value.charAt(index);
  22. if (character !== C_SPACE && character !== C_TAB) {
  23. index--;
  24. break;
  25. }
  26. subvalue += character;
  27. }
  28. /* Eat hashes. */
  29. depth = 0;
  30. while (++index <= length) {
  31. character = value.charAt(index);
  32. if (character !== C_HASH) {
  33. index--;
  34. break;
  35. }
  36. subvalue += character;
  37. depth++;
  38. }
  39. if (depth > MAX_ATX_COUNT) {
  40. return;
  41. }
  42. if (
  43. !depth ||
  44. (!settings.pedantic && value.charAt(index + 1) === C_HASH)
  45. ) {
  46. return;
  47. }
  48. length = value.length + 1;
  49. /* Eat intermediate white-space. */
  50. queue = '';
  51. while (++index < length) {
  52. character = value.charAt(index);
  53. if (character !== C_SPACE && character !== C_TAB) {
  54. index--;
  55. break;
  56. }
  57. queue += character;
  58. }
  59. /* Exit when not in pedantic mode without spacing. */
  60. if (
  61. !settings.pedantic &&
  62. queue.length === 0 &&
  63. character &&
  64. character !== C_NEWLINE
  65. ) {
  66. return;
  67. }
  68. if (silent) {
  69. return true;
  70. }
  71. /* Eat content. */
  72. subvalue += queue;
  73. queue = '';
  74. content = '';
  75. while (++index < length) {
  76. character = value.charAt(index);
  77. if (!character || character === C_NEWLINE) {
  78. break;
  79. }
  80. if (
  81. character !== C_SPACE &&
  82. character !== C_TAB &&
  83. character !== C_HASH
  84. ) {
  85. content += queue + character;
  86. queue = '';
  87. continue;
  88. }
  89. while (character === C_SPACE || character === C_TAB) {
  90. queue += character;
  91. character = value.charAt(++index);
  92. }
  93. while (character === C_HASH) {
  94. queue += character;
  95. character = value.charAt(++index);
  96. }
  97. while (character === C_SPACE || character === C_TAB) {
  98. queue += character;
  99. character = value.charAt(++index);
  100. }
  101. index--;
  102. }
  103. now.column += subvalue.length;
  104. now.offset += subvalue.length;
  105. subvalue += content + queue;
  106. return eat(subvalue)({
  107. type: 'heading',
  108. depth: depth,
  109. children: self.tokenizeInline(content, now)
  110. });
  111. }