@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.cjs CHANGED
@@ -2856,7 +2856,16 @@ function deltaToHtml(delta, options = {}) {
2856
2856
  counters = [];
2857
2857
  const codeLines = collectCodeBlockLines(lines, i);
2858
2858
  const language = getCodeBlockLanguage(line.attributes);
2859
- html += renderCodeBlock(codeLines, language, embedRenderers, pretty, blockHandlers, options);
2859
+ const codeBlockId = getCodeBlockId(line.attributes);
2860
+ html += renderCodeBlock(
2861
+ codeLines,
2862
+ language,
2863
+ embedRenderers,
2864
+ pretty,
2865
+ blockHandlers,
2866
+ options,
2867
+ codeBlockId
2868
+ );
2860
2869
  i += codeLines.length - 1;
2861
2870
  continue;
2862
2871
  }
@@ -3127,11 +3136,13 @@ function collectCodeBlockLines(lines, startIndex) {
3127
3136
  const startLine = lines[startIndex];
3128
3137
  if (!startLine) return codeLines;
3129
3138
  const startLang = getCodeBlockLanguage(startLine.attributes);
3139
+ const startId = getCodeBlockId(startLine.attributes);
3130
3140
  for (let i = startIndex; i < lines.length; i++) {
3131
3141
  const line = lines[i];
3132
3142
  if (!line || !line.attributes?.["code-block"]) break;
3133
3143
  const lang = getCodeBlockLanguage(line.attributes);
3134
- if (i > startIndex && lang !== startLang) break;
3144
+ const id = getCodeBlockId(line.attributes);
3145
+ if (i > startIndex && (lang !== startLang || id !== startId)) break;
3135
3146
  codeLines.push(line);
3136
3147
  }
3137
3148
  return codeLines;
@@ -3144,14 +3155,20 @@ function getCodeBlockLanguage(attributes) {
3144
3155
  }
3145
3156
  return void 0;
3146
3157
  }
3147
- function renderCodeBlock(codeLines, language, embedRenderers, pretty, blockHandlers, options) {
3158
+ function getCodeBlockId(attributes) {
3159
+ if (!attributes) return void 0;
3160
+ const id = attributes["code-block-id"];
3161
+ return typeof id === "string" ? id : void 0;
3162
+ }
3163
+ function renderCodeBlock(codeLines, language, embedRenderers, pretty, blockHandlers, options, codeBlockId) {
3148
3164
  const lineContents = codeLines.map(
3149
3165
  (line) => renderLineContent(line.ops, embedRenderers, blockHandlers, options)
3150
3166
  );
3151
3167
  const code = lineContents.join("\n");
3152
3168
  const langClass = language ? ` class="language-${escapeHtml(language)}"` : "";
3153
3169
  const langAttr = language ? ` data-language="${escapeHtml(language)}"` : "";
3154
- const html = `<pre${langAttr}><code${langClass}>${code}
3170
+ const idAttr = codeBlockId ? ` data-code-block-id="${escapeHtml(codeBlockId)}"` : "";
3171
+ const html = `<pre${langAttr}${idAttr}><code${langClass}>${code}
3155
3172
  </code></pre>`;
3156
3173
  return pretty ? html + "\n" : html;
3157
3174
  }
@@ -3365,6 +3382,7 @@ function htmlToDelta(html, options = {}) {
3365
3382
  let currentBlockAttributes = {};
3366
3383
  let pendingText = "";
3367
3384
  let atLineStart = true;
3385
+ let codeBlockIdSeq = 0;
3368
3386
  const context = {
3369
3387
  delta,
3370
3388
  get attributes() {
@@ -3577,6 +3595,8 @@ function htmlToDelta(html, options = {}) {
3577
3595
  }
3578
3596
  const codeBlockValue = language || true;
3579
3597
  currentBlockAttributes["code-block"] = codeBlockValue;
3598
+ const existingId = element.getAttribute("data-code-block-id");
3599
+ currentBlockAttributes["code-block-id"] = existingId || `cb-${++codeBlockIdSeq}`;
3580
3600
  const sourceElement = codeElement || element;
3581
3601
  const rawText = sourceElement.textContent ?? "";
3582
3602
  const text = rawText.endsWith("\n") ? rawText.slice(0, -1) : rawText;
@@ -4312,11 +4332,13 @@ function collectCodeBlock(lines, startIndex) {
4312
4332
  const startLine = lines[startIndex];
4313
4333
  if (!startLine) return codeLines;
4314
4334
  const startLang = getCodeBlockLanguage2(startLine.attributes);
4335
+ const startId = getCodeBlockId2(startLine.attributes);
4315
4336
  for (let i = startIndex; i < lines.length; i++) {
4316
4337
  const line = lines[i];
4317
4338
  if (!line || !line.attributes["code-block"]) break;
4318
4339
  const lang = getCodeBlockLanguage2(line.attributes);
4319
- if (i > startIndex && lang !== startLang) break;
4340
+ const id = getCodeBlockId2(line.attributes);
4341
+ if (i > startIndex && (lang !== startLang || id !== startId)) break;
4320
4342
  codeLines.push(line);
4321
4343
  }
4322
4344
  return codeLines;
@@ -4434,6 +4456,10 @@ function getCodeBlockLanguage2(attributes) {
4434
4456
  }
4435
4457
  return void 0;
4436
4458
  }
4459
+ function getCodeBlockId2(attributes) {
4460
+ const id = attributes["code-block-id"];
4461
+ return typeof id === "string" ? id : void 0;
4462
+ }
4437
4463
  function renderLineContent2(ops, embedRenderers, inCodeBlock, useLatexDelimiters = false, blockHandlers, prettyHtml = false, registry, softBreakStyle = "spaces", inTableCell = false) {
4438
4464
  let result = "";
4439
4465
  for (const op of ops) {
@@ -4787,6 +4813,7 @@ function astToDelta(tree, customHandlers, mathBlock, mermaidBlock, plantumlBlock
4787
4813
  let pendingText = "";
4788
4814
  const spanAttrStack = [];
4789
4815
  const footnoteDefinitions = /* @__PURE__ */ new Map();
4816
+ let codeBlockIdSeq = 0;
4790
4817
  const context = {
4791
4818
  delta,
4792
4819
  pushText(text, attrs) {
@@ -5102,7 +5129,8 @@ function astToDelta(tree, customHandlers, mathBlock, mermaidBlock, plantumlBlock
5102
5129
  }
5103
5130
  const lines = code.split("\n");
5104
5131
  const codeBlockAttr = {
5105
- "code-block": lang ?? true
5132
+ "code-block": lang ?? true,
5133
+ "code-block-id": `cb-${++codeBlockIdSeq}`
5106
5134
  };
5107
5135
  for (const line of lines) {
5108
5136
  context.pushText(line);
@@ -5116,7 +5144,10 @@ function astToDelta(tree, customHandlers, mathBlock, mermaidBlock, plantumlBlock
5116
5144
  context.pushNewline();
5117
5145
  } else {
5118
5146
  const lines = value.split("\n");
5119
- const mathBlockAttr = { "code-block": "math" };
5147
+ const mathBlockAttr = {
5148
+ "code-block": "math",
5149
+ "code-block-id": `cb-${++codeBlockIdSeq}`
5150
+ };
5120
5151
  for (const line of lines) {
5121
5152
  context.pushText(line);
5122
5153
  context.pushNewline(mathBlockAttr);