@scrider/formatter 1.4.4 → 1.5.0

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.
package/dist/index.js CHANGED
@@ -2732,7 +2732,16 @@ function deltaToHtml(delta, options = {}) {
2732
2732
  counters = [];
2733
2733
  const codeLines = collectCodeBlockLines(lines, i);
2734
2734
  const language = getCodeBlockLanguage(line.attributes);
2735
- html += renderCodeBlock(codeLines, language, embedRenderers, pretty, blockHandlers, options);
2735
+ const codeBlockId = getCodeBlockId(line.attributes);
2736
+ html += renderCodeBlock(
2737
+ codeLines,
2738
+ language,
2739
+ embedRenderers,
2740
+ pretty,
2741
+ blockHandlers,
2742
+ options,
2743
+ codeBlockId
2744
+ );
2736
2745
  i += codeLines.length - 1;
2737
2746
  continue;
2738
2747
  }
@@ -3003,11 +3012,13 @@ function collectCodeBlockLines(lines, startIndex) {
3003
3012
  const startLine = lines[startIndex];
3004
3013
  if (!startLine) return codeLines;
3005
3014
  const startLang = getCodeBlockLanguage(startLine.attributes);
3015
+ const startId = getCodeBlockId(startLine.attributes);
3006
3016
  for (let i = startIndex; i < lines.length; i++) {
3007
3017
  const line = lines[i];
3008
3018
  if (!line || !line.attributes?.["code-block"]) break;
3009
3019
  const lang = getCodeBlockLanguage(line.attributes);
3010
- if (i > startIndex && lang !== startLang) break;
3020
+ const id = getCodeBlockId(line.attributes);
3021
+ if (i > startIndex && (lang !== startLang || id !== startId)) break;
3011
3022
  codeLines.push(line);
3012
3023
  }
3013
3024
  return codeLines;
@@ -3020,14 +3031,20 @@ function getCodeBlockLanguage(attributes) {
3020
3031
  }
3021
3032
  return void 0;
3022
3033
  }
3023
- function renderCodeBlock(codeLines, language, embedRenderers, pretty, blockHandlers, options) {
3034
+ function getCodeBlockId(attributes) {
3035
+ if (!attributes) return void 0;
3036
+ const id = attributes["code-block-id"];
3037
+ return typeof id === "string" ? id : void 0;
3038
+ }
3039
+ function renderCodeBlock(codeLines, language, embedRenderers, pretty, blockHandlers, options, codeBlockId) {
3024
3040
  const lineContents = codeLines.map(
3025
3041
  (line) => renderLineContent(line.ops, embedRenderers, blockHandlers, options)
3026
3042
  );
3027
3043
  const code = lineContents.join("\n");
3028
3044
  const langClass = language ? ` class="language-${escapeHtml(language)}"` : "";
3029
3045
  const langAttr = language ? ` data-language="${escapeHtml(language)}"` : "";
3030
- const html = `<pre${langAttr}><code${langClass}>${code}
3046
+ const idAttr = codeBlockId ? ` data-code-block-id="${escapeHtml(codeBlockId)}"` : "";
3047
+ const html = `<pre${langAttr}${idAttr}><code${langClass}>${code}
3031
3048
  </code></pre>`;
3032
3049
  return pretty ? html + "\n" : html;
3033
3050
  }
@@ -3241,6 +3258,7 @@ function htmlToDelta(html, options = {}) {
3241
3258
  let currentBlockAttributes = {};
3242
3259
  let pendingText = "";
3243
3260
  let atLineStart = true;
3261
+ let codeBlockIdSeq = 0;
3244
3262
  const context = {
3245
3263
  delta,
3246
3264
  get attributes() {
@@ -3453,6 +3471,8 @@ function htmlToDelta(html, options = {}) {
3453
3471
  }
3454
3472
  const codeBlockValue = language || true;
3455
3473
  currentBlockAttributes["code-block"] = codeBlockValue;
3474
+ const existingId = element.getAttribute("data-code-block-id");
3475
+ currentBlockAttributes["code-block-id"] = existingId || `cb-${++codeBlockIdSeq}`;
3456
3476
  const sourceElement = codeElement || element;
3457
3477
  const rawText = sourceElement.textContent ?? "";
3458
3478
  const text = rawText.endsWith("\n") ? rawText.slice(0, -1) : rawText;
@@ -4188,11 +4208,13 @@ function collectCodeBlock(lines, startIndex) {
4188
4208
  const startLine = lines[startIndex];
4189
4209
  if (!startLine) return codeLines;
4190
4210
  const startLang = getCodeBlockLanguage2(startLine.attributes);
4211
+ const startId = getCodeBlockId2(startLine.attributes);
4191
4212
  for (let i = startIndex; i < lines.length; i++) {
4192
4213
  const line = lines[i];
4193
4214
  if (!line || !line.attributes["code-block"]) break;
4194
4215
  const lang = getCodeBlockLanguage2(line.attributes);
4195
- if (i > startIndex && lang !== startLang) break;
4216
+ const id = getCodeBlockId2(line.attributes);
4217
+ if (i > startIndex && (lang !== startLang || id !== startId)) break;
4196
4218
  codeLines.push(line);
4197
4219
  }
4198
4220
  return codeLines;
@@ -4310,6 +4332,10 @@ function getCodeBlockLanguage2(attributes) {
4310
4332
  }
4311
4333
  return void 0;
4312
4334
  }
4335
+ function getCodeBlockId2(attributes) {
4336
+ const id = attributes["code-block-id"];
4337
+ return typeof id === "string" ? id : void 0;
4338
+ }
4313
4339
  function renderLineContent2(ops, embedRenderers, inCodeBlock, useLatexDelimiters = false, blockHandlers, prettyHtml = false, registry, softBreakStyle = "spaces", inTableCell = false) {
4314
4340
  let result = "";
4315
4341
  for (const op of ops) {
@@ -4663,6 +4689,7 @@ function astToDelta(tree, customHandlers, mathBlock, mermaidBlock, plantumlBlock
4663
4689
  let pendingText = "";
4664
4690
  const spanAttrStack = [];
4665
4691
  const footnoteDefinitions = /* @__PURE__ */ new Map();
4692
+ let codeBlockIdSeq = 0;
4666
4693
  const context = {
4667
4694
  delta,
4668
4695
  pushText(text, attrs) {
@@ -4978,7 +5005,8 @@ function astToDelta(tree, customHandlers, mathBlock, mermaidBlock, plantumlBlock
4978
5005
  }
4979
5006
  const lines = code.split("\n");
4980
5007
  const codeBlockAttr = {
4981
- "code-block": lang ?? true
5008
+ "code-block": lang ?? true,
5009
+ "code-block-id": `cb-${++codeBlockIdSeq}`
4982
5010
  };
4983
5011
  for (const line of lines) {
4984
5012
  context.pushText(line);
@@ -4992,7 +5020,10 @@ function astToDelta(tree, customHandlers, mathBlock, mermaidBlock, plantumlBlock
4992
5020
  context.pushNewline();
4993
5021
  } else {
4994
5022
  const lines = value.split("\n");
4995
- const mathBlockAttr = { "code-block": "math" };
5023
+ const mathBlockAttr = {
5024
+ "code-block": "math",
5025
+ "code-block-id": `cb-${++codeBlockIdSeq}`
5026
+ };
4996
5027
  for (const line of lines) {
4997
5028
  context.pushText(line);
4998
5029
  context.pushNewline(mathBlockAttr);