@diplodoc/transform 4.73.0 → 4.73.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -89,17 +89,48 @@ function termReplace(str: string, env: EnvTerm, escape: (str: string) => string)
89
89
  return termCode || str;
90
90
  }
91
91
 
92
- function addLineNumbers(code: string, {lineWrapping}: {lineWrapping: boolean}): string {
93
- const lines = code.split('\n');
94
- const lineCount = lines.length;
95
- const maxDigits = String(lineCount).length;
92
+ const SPAN_TAG_RE = /<span[^>]*>|<\/span>/g;
93
+
94
+ /**
95
+ * Balances hljs span tags within each line so that every line is a
96
+ * self-contained HTML fragment with no unclosed or unopened spans.
97
+ *
98
+ * highlight.js can produce spans that cross multiple lines (e.g. multiline strings).
99
+ * Before:
100
+ * `['<span class="hljs-string">\'line1', 'line2\'</span>']`
101
+ * After:
102
+ * `['<span class="hljs-string">\'line1</span>', '<span class="hljs-string">line2\'</span>']`
103
+ */
104
+ function balanceSpansPerLine(lines: string[]): string[] {
105
+ const openTagStack: string[] = [];
106
+
107
+ return lines.map((line) => {
108
+ const prefix = openTagStack.join('');
109
+
110
+ SPAN_TAG_RE.lastIndex = 0;
111
+ let match: RegExpExecArray | null;
112
+ while ((match = SPAN_TAG_RE.exec(line)) !== null) {
113
+ if (match[0] === '</span>') {
114
+ openTagStack.pop();
115
+ } else {
116
+ openTagStack.push(match[0]);
117
+ }
118
+ }
96
119
 
97
- // Remove trailing empty line if it exists
120
+ const suffix = '</span>'.repeat(openTagStack.length);
121
+ return prefix + line + suffix;
122
+ });
123
+ }
124
+
125
+ function addLineNumbers(code: string, {lineWrapping}: {lineWrapping: boolean}): string {
98
126
  const hasTrailingNewline = code.endsWith('\n');
127
+ const lines = code.split('\n');
99
128
  const linesToProcess = hasTrailingNewline ? lines.slice(0, -1) : lines;
129
+ const normalized = balanceSpansPerLine(linesToProcess);
130
+ const maxDigits = String(normalized.length).length;
100
131
 
101
132
  return (
102
- linesToProcess
133
+ normalized
103
134
  .map((line, index) => {
104
135
  const lineNumber = String(index + 1).padStart(maxDigits, ' ');
105
136
  return lineWrapping
@@ -763,6 +763,22 @@ function sanitizeStyleTags(dom: cheerio.CheerioAPI, cssWhiteList: CssWhiteList)
763
763
  function sanitizeStyleAttrs(dom: cheerio.CheerioAPI, cssWhiteList: CssWhiteList) {
764
764
  const options = {
765
765
  whiteList: cssWhiteList,
766
+ safeAttrValue: (name: string, value: string) => {
767
+ if (!isSafeCssValue(name, value)) {
768
+ return null;
769
+ }
770
+
771
+ return cssfilter.safeAttrValue(name, value);
772
+ },
773
+ onIgnoreAttr: (name: string, value: string) => {
774
+ const isCssVariable = CSS_VAR_DECLARATION_RE.test(name);
775
+
776
+ if (isCssVariable) {
777
+ return `${name}:${value}`;
778
+ }
779
+
780
+ return null;
781
+ },
766
782
  };
767
783
  const cssSanitizer = new cssfilter.FilterCSS(options);
768
784