@diplodoc/transform 4.72.0 → 4.73.1

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
@@ -494,9 +494,10 @@ const svgAttrs = [
494
494
  'use',
495
495
  ];
496
496
 
497
+ const CSS_VAR_DECLARATION_RE = /^--[a-zA-Z0-9_-]+$/;
498
+
497
499
  const defaultCssWhitelist = {
498
500
  ...cssfilter.whiteList,
499
- '--method': true,
500
501
  };
501
502
 
502
503
  const yfmHtmlAttrs = ['note-type', 'term-key'];
@@ -724,24 +725,28 @@ function sanitizeStyleTags(dom: cheerio.CheerioAPI, cssWhiteList: CssWhiteList)
724
725
  return false;
725
726
  }
726
727
 
727
- const prop = String(declaration.property).toLowerCase();
728
+ const rawProp = String(declaration.property);
729
+ const prop = rawProp.toLowerCase();
728
730
  const val = String(declaration.value);
729
731
 
730
732
  if (!isSafeCssValue(prop, val)) {
731
733
  return false;
732
734
  }
733
735
 
736
+ const isCssVariable = CSS_VAR_DECLARATION_RE.test(rawProp);
734
737
  const isWhiteListed = Boolean(cssWhiteList[prop]);
735
738
 
736
- if (isWhiteListed) {
737
- declaration.value = cssfilter.safeAttrValue(prop, val);
739
+ if (!isWhiteListed && !isCssVariable) {
740
+ return false;
738
741
  }
739
742
 
743
+ declaration.value = cssfilter.safeAttrValue(prop, val);
744
+
740
745
  if (!declaration.value) {
741
746
  return false;
742
747
  }
743
748
 
744
- return isWhiteListed;
749
+ return true;
745
750
  });
746
751
  });
747
752