blockquote.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. 'use strict';
  2. var trim = require('trim');
  3. var interrupt = require('../util/interrupt');
  4. module.exports = blockquote;
  5. var C_NEWLINE = '\n';
  6. var C_TAB = '\t';
  7. var C_SPACE = ' ';
  8. var C_GT = '>';
  9. /* Tokenise a blockquote. */
  10. function blockquote(eat, value, silent) {
  11. var self = this;
  12. var offsets = self.offset;
  13. var tokenizers = self.blockTokenizers;
  14. var interruptors = self.interruptBlockquote;
  15. var now = eat.now();
  16. var currentLine = now.line;
  17. var length = value.length;
  18. var values = [];
  19. var contents = [];
  20. var indents = [];
  21. var add;
  22. var index = 0;
  23. var character;
  24. var rest;
  25. var nextIndex;
  26. var content;
  27. var line;
  28. var startIndex;
  29. var prefixed;
  30. var exit;
  31. while (index < length) {
  32. character = value.charAt(index);
  33. if (character !== C_SPACE && character !== C_TAB) {
  34. break;
  35. }
  36. index++;
  37. }
  38. if (value.charAt(index) !== C_GT) {
  39. return;
  40. }
  41. if (silent) {
  42. return true;
  43. }
  44. index = 0;
  45. while (index < length) {
  46. nextIndex = value.indexOf(C_NEWLINE, index);
  47. startIndex = index;
  48. prefixed = false;
  49. if (nextIndex === -1) {
  50. nextIndex = length;
  51. }
  52. while (index < length) {
  53. character = value.charAt(index);
  54. if (character !== C_SPACE && character !== C_TAB) {
  55. break;
  56. }
  57. index++;
  58. }
  59. if (value.charAt(index) === C_GT) {
  60. index++;
  61. prefixed = true;
  62. if (value.charAt(index) === C_SPACE) {
  63. index++;
  64. }
  65. } else {
  66. index = startIndex;
  67. }
  68. content = value.slice(index, nextIndex);
  69. if (!prefixed && !trim(content)) {
  70. index = startIndex;
  71. break;
  72. }
  73. if (!prefixed) {
  74. rest = value.slice(index);
  75. /* Check if the following code contains a possible
  76. * block. */
  77. if (interrupt(interruptors, tokenizers, self, [eat, rest, true])) {
  78. break;
  79. }
  80. }
  81. line = startIndex === index ? content : value.slice(startIndex, nextIndex);
  82. indents.push(index - startIndex);
  83. values.push(line);
  84. contents.push(content);
  85. index = nextIndex + 1;
  86. }
  87. index = -1;
  88. length = indents.length;
  89. add = eat(values.join(C_NEWLINE));
  90. while (++index < length) {
  91. offsets[currentLine] = (offsets[currentLine] || 0) + indents[index];
  92. currentLine++;
  93. }
  94. exit = self.enterBlock();
  95. contents = self.tokenizeBlock(contents.join(C_NEWLINE), now);
  96. exit();
  97. return add({
  98. type: 'blockquote',
  99. children: contents
  100. });
  101. }