preprocess.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * @import {Chunk, Code, Encoding, Value} from 'micromark-util-types'
  3. */
  4. /**
  5. * @callback Preprocessor
  6. * Preprocess a value.
  7. * @param {Value} value
  8. * Value.
  9. * @param {Encoding | null | undefined} [encoding]
  10. * Encoding when `value` is a typed array (optional).
  11. * @param {boolean | null | undefined} [end=false]
  12. * Whether this is the last chunk (default: `false`).
  13. * @returns {Array<Chunk>}
  14. * Chunks.
  15. */
  16. const search = /[\0\t\n\r]/g;
  17. /**
  18. * @returns {Preprocessor}
  19. * Preprocess a value.
  20. */
  21. export function preprocess() {
  22. let column = 1;
  23. let buffer = '';
  24. /** @type {boolean | undefined} */
  25. let start = true;
  26. /** @type {boolean | undefined} */
  27. let atCarriageReturn;
  28. return preprocessor;
  29. /** @type {Preprocessor} */
  30. // eslint-disable-next-line complexity
  31. function preprocessor(value, encoding, end) {
  32. /** @type {Array<Chunk>} */
  33. const chunks = [];
  34. /** @type {RegExpMatchArray | null} */
  35. let match;
  36. /** @type {number} */
  37. let next;
  38. /** @type {number} */
  39. let startPosition;
  40. /** @type {number} */
  41. let endPosition;
  42. /** @type {Code} */
  43. let code;
  44. value = buffer + (typeof value === 'string' ? value.toString() : new TextDecoder(encoding || undefined).decode(value));
  45. startPosition = 0;
  46. buffer = '';
  47. if (start) {
  48. // To do: `markdown-rs` actually parses BOMs (byte order mark).
  49. if (value.charCodeAt(0) === 65279) {
  50. startPosition++;
  51. }
  52. start = undefined;
  53. }
  54. while (startPosition < value.length) {
  55. search.lastIndex = startPosition;
  56. match = search.exec(value);
  57. endPosition = match && match.index !== undefined ? match.index : value.length;
  58. code = value.charCodeAt(endPosition);
  59. if (!match) {
  60. buffer = value.slice(startPosition);
  61. break;
  62. }
  63. if (code === 10 && startPosition === endPosition && atCarriageReturn) {
  64. chunks.push(-3);
  65. atCarriageReturn = undefined;
  66. } else {
  67. if (atCarriageReturn) {
  68. chunks.push(-5);
  69. atCarriageReturn = undefined;
  70. }
  71. if (startPosition < endPosition) {
  72. chunks.push(value.slice(startPosition, endPosition));
  73. column += endPosition - startPosition;
  74. }
  75. switch (code) {
  76. case 0:
  77. {
  78. chunks.push(65533);
  79. column++;
  80. break;
  81. }
  82. case 9:
  83. {
  84. next = Math.ceil(column / 4) * 4;
  85. chunks.push(-2);
  86. while (column++ < next) chunks.push(-1);
  87. break;
  88. }
  89. case 10:
  90. {
  91. chunks.push(-4);
  92. column = 1;
  93. break;
  94. }
  95. default:
  96. {
  97. atCarriageReturn = true;
  98. column = 1;
  99. }
  100. }
  101. }
  102. startPosition = endPosition + 1;
  103. }
  104. if (end) {
  105. if (atCarriageReturn) chunks.push(-5);
  106. if (buffer) chunks.push(buffer);
  107. chunks.push(null);
  108. }
  109. return chunks;
  110. }
  111. }