Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

foldFlowLines.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = foldFlowLines;
  6. exports.FOLD_QUOTED = exports.FOLD_BLOCK = exports.FOLD_FLOW = void 0;
  7. const FOLD_FLOW = 'flow';
  8. exports.FOLD_FLOW = FOLD_FLOW;
  9. const FOLD_BLOCK = 'block';
  10. exports.FOLD_BLOCK = FOLD_BLOCK;
  11. const FOLD_QUOTED = 'quoted'; // presumes i+1 is at the start of a line
  12. // returns index of last newline in more-indented block
  13. exports.FOLD_QUOTED = FOLD_QUOTED;
  14. const consumeMoreIndentedLines = (text, i) => {
  15. let ch = text[i + 1];
  16. while (ch === ' ' || ch === '\t') {
  17. do {
  18. ch = text[i += 1];
  19. } while (ch && ch !== '\n');
  20. ch = text[i + 1];
  21. }
  22. return i;
  23. };
  24. /**
  25. * Tries to keep input at up to `lineWidth` characters, splitting only on spaces
  26. * not followed by newlines or spaces unless `mode` is `'quoted'`. Lines are
  27. * terminated with `\n` and started with `indent`.
  28. *
  29. * @param {string} text
  30. * @param {string} indent
  31. * @param {string} [mode='flow'] `'block'` prevents more-indented lines
  32. * from being folded; `'quoted'` allows for `\` escapes, including escaped
  33. * newlines
  34. * @param {Object} options
  35. * @param {number} [options.indentAtStart] Accounts for leading contents on
  36. * the first line, defaulting to `indent.length`
  37. * @param {number} [options.lineWidth=80]
  38. * @param {number} [options.minContentWidth=20] Allow highly indented lines to
  39. * stretch the line width
  40. * @param {function} options.onFold Called once if the text is folded
  41. * @param {function} options.onFold Called once if any line of text exceeds
  42. * lineWidth characters
  43. */
  44. function foldFlowLines(text, indent, mode, {
  45. indentAtStart,
  46. lineWidth = 80,
  47. minContentWidth = 20,
  48. onFold,
  49. onOverflow
  50. }) {
  51. if (!lineWidth || lineWidth < 0) return text;
  52. const endStep = Math.max(1 + minContentWidth, 1 + lineWidth - indent.length);
  53. if (text.length <= endStep) return text;
  54. const folds = [];
  55. const escapedFolds = {};
  56. let end = lineWidth - (typeof indentAtStart === 'number' ? indentAtStart : indent.length);
  57. let split = undefined;
  58. let prev = undefined;
  59. let overflow = false;
  60. let i = -1;
  61. if (mode === FOLD_BLOCK) {
  62. i = consumeMoreIndentedLines(text, i);
  63. if (i !== -1) end = i + endStep;
  64. }
  65. for (let ch; ch = text[i += 1];) {
  66. if (mode === FOLD_QUOTED && ch === '\\') {
  67. switch (text[i + 1]) {
  68. case 'x':
  69. i += 3;
  70. break;
  71. case 'u':
  72. i += 5;
  73. break;
  74. case 'U':
  75. i += 9;
  76. break;
  77. default:
  78. i += 1;
  79. }
  80. }
  81. if (ch === '\n') {
  82. if (mode === FOLD_BLOCK) i = consumeMoreIndentedLines(text, i);
  83. end = i + endStep;
  84. split = undefined;
  85. } else {
  86. if (ch === ' ' && prev && prev !== ' ' && prev !== '\n' && prev !== '\t') {
  87. // space surrounded by non-space can be replaced with newline + indent
  88. const next = text[i + 1];
  89. if (next && next !== ' ' && next !== '\n' && next !== '\t') split = i;
  90. }
  91. if (i >= end) {
  92. if (split) {
  93. folds.push(split);
  94. end = split + endStep;
  95. split = undefined;
  96. } else if (mode === FOLD_QUOTED) {
  97. // white-space collected at end may stretch past lineWidth
  98. while (prev === ' ' || prev === '\t') {
  99. prev = ch;
  100. ch = text[i += 1];
  101. overflow = true;
  102. } // i - 2 accounts for not-dropped last char + newline-escaping \
  103. folds.push(i - 2);
  104. escapedFolds[i - 2] = true;
  105. end = i - 2 + endStep;
  106. split = undefined;
  107. } else {
  108. overflow = true;
  109. }
  110. }
  111. }
  112. prev = ch;
  113. }
  114. if (overflow && onOverflow) onOverflow();
  115. if (folds.length === 0) return text;
  116. if (onFold) onFold();
  117. let res = text.slice(0, folds[0]);
  118. for (let i = 0; i < folds.length; ++i) {
  119. const fold = folds[i];
  120. const end = folds[i + 1] || text.length;
  121. if (mode === FOLD_QUOTED && escapedFolds[fold]) res += `${text[fold]}\\`;
  122. res += `\n${indent}${text.slice(fold + 1, end)}`;
  123. }
  124. return res;
  125. }