@scrider/formatter 1.10.7 → 1.10.9

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
@@ -3548,7 +3548,25 @@ function deltaToHtml(delta, options = {}) {
3548
3548
  i += tableLines.length - 1;
3549
3549
  continue;
3550
3550
  }
3551
- const { tag, isList, listType, indent, isCodeBlock } = getBlockInfo(line.attributes);
3551
+ const { tag, isList, listType, indent, isCodeBlock, isBlockquote } = getBlockInfo(
3552
+ line.attributes
3553
+ );
3554
+ if (isBlockquote) {
3555
+ html += closeAllLists(listStack, pretty);
3556
+ listStack = [];
3557
+ counters = [];
3558
+ const quoteLines = collectBlockquoteLines(lines, i);
3559
+ html += renderBlockquote(
3560
+ quoteLines,
3561
+ embedRenderers,
3562
+ pretty,
3563
+ blockHandlers,
3564
+ options,
3565
+ resolvedDocumentPresentation
3566
+ );
3567
+ i += quoteLines.length - 1;
3568
+ continue;
3569
+ }
3552
3570
  if (isCodeBlock) {
3553
3571
  html += closeAllLists(listStack, pretty);
3554
3572
  listStack = [];
@@ -3577,13 +3595,7 @@ function deltaToHtml(delta, options = {}) {
3577
3595
  continue;
3578
3596
  }
3579
3597
  if (isList) {
3580
- html += handleListOpen(
3581
- listStack,
3582
- listType,
3583
- indent,
3584
- pretty,
3585
- resolvedDocumentPresentation
3586
- );
3598
+ html += handleListOpen(listStack, listType, indent, pretty, resolvedDocumentPresentation);
3587
3599
  if (hierarchicalNumbers && listType === "ordered") {
3588
3600
  if (counters.length > indent + 1) {
3589
3601
  counters = counters.slice(0, indent + 1);
@@ -3816,24 +3828,90 @@ function renderTableRow(cells, maxCol, cellTag, embedRenderers, pretty, depth, b
3816
3828
  }
3817
3829
  function getBlockInfo(attributes) {
3818
3830
  if (!attributes) {
3819
- return { tag: "p", isList: false, isCodeBlock: false, listType: void 0, indent: 0 };
3831
+ return {
3832
+ tag: "p",
3833
+ isList: false,
3834
+ isCodeBlock: false,
3835
+ isBlockquote: false,
3836
+ listType: void 0,
3837
+ indent: 0
3838
+ };
3820
3839
  }
3821
3840
  const indent = typeof attributes.indent === "number" ? attributes.indent : 0;
3822
3841
  if (attributes["code-block"]) {
3823
- return { tag: "pre", isList: false, isCodeBlock: true, listType: void 0, indent };
3842
+ return {
3843
+ tag: "pre",
3844
+ isList: false,
3845
+ isCodeBlock: true,
3846
+ isBlockquote: false,
3847
+ listType: void 0,
3848
+ indent
3849
+ };
3824
3850
  }
3825
3851
  if (attributes.list) {
3826
3852
  const listVal = attributes.list;
3827
3853
  const listType = typeof listVal === "string" ? listVal : "bullet";
3828
- return { tag: "li", isList: true, isCodeBlock: false, listType, indent };
3854
+ return { tag: "li", isList: true, isCodeBlock: false, isBlockquote: false, listType, indent };
3855
+ }
3856
+ if (attributes.blockquote) {
3857
+ return {
3858
+ tag: "blockquote",
3859
+ isList: false,
3860
+ isCodeBlock: false,
3861
+ isBlockquote: true,
3862
+ listType: void 0,
3863
+ indent
3864
+ };
3829
3865
  }
3830
3866
  for (const [format, tagOrFn] of Object.entries(BLOCK_FORMAT_TAGS)) {
3831
3867
  if (format in attributes && format !== "list" && format !== "code-block") {
3832
3868
  const tag = typeof tagOrFn === "function" ? tagOrFn(attributes[format]) : tagOrFn;
3833
- return { tag, isList: false, isCodeBlock: false, listType: void 0, indent };
3869
+ return {
3870
+ tag,
3871
+ isList: false,
3872
+ isCodeBlock: false,
3873
+ isBlockquote: false,
3874
+ listType: void 0,
3875
+ indent
3876
+ };
3834
3877
  }
3835
3878
  }
3836
- return { tag: "p", isList: false, isCodeBlock: false, listType: void 0, indent };
3879
+ return {
3880
+ tag: "p",
3881
+ isList: false,
3882
+ isCodeBlock: false,
3883
+ isBlockquote: false,
3884
+ listType: void 0,
3885
+ indent
3886
+ };
3887
+ }
3888
+ function collectBlockquoteLines(lines, startIndex) {
3889
+ const quoteLines = [];
3890
+ for (let i = startIndex; i < lines.length; i++) {
3891
+ const line = lines[i];
3892
+ if (!line || !line.attributes?.blockquote) break;
3893
+ quoteLines.push(line);
3894
+ }
3895
+ return quoteLines;
3896
+ }
3897
+ function renderBlockquote(quoteLines, embedRenderers, pretty, blockHandlers, options, resolvedDocumentPresentation) {
3898
+ const firstAttrs = quoteLines[0]?.attributes;
3899
+ const wrapperIndent = firstAttrs && typeof firstAttrs.indent === "number" && firstAttrs.indent > 0 ? ` style="margin-left: ${firstAttrs.indent * 2}em"` : "";
3900
+ const nl = pretty ? "\n" : "";
3901
+ const innerIndent = pretty ? " " : "";
3902
+ let html = `<blockquote${wrapperIndent}>${nl}`;
3903
+ for (const line of quoteLines) {
3904
+ const content = renderLineContent(line.ops, embedRenderers, blockHandlers, options);
3905
+ html += `${innerIndent}${renderBlockquoteParagraph(content, line.attributes, resolvedDocumentPresentation)}${nl}`;
3906
+ }
3907
+ html += `</blockquote>`;
3908
+ return pretty ? html + "\n" : html;
3909
+ }
3910
+ function renderBlockquoteParagraph(content, attributes, resolvedDocumentPresentation) {
3911
+ const styleSource = attributes ? { ...attributes, indent: void 0 } : void 0;
3912
+ const styleAttr = getBlockStyleAttribute("blockquote", styleSource, resolvedDocumentPresentation);
3913
+ const innerContent = content || "<br>";
3914
+ return `<p${styleAttr}>${innerContent}</p>`;
3837
3915
  }
3838
3916
  function collectCodeBlockLines(lines, startIndex) {
3839
3917
  const codeLines = [];
@@ -4084,6 +4162,22 @@ function renderEmbed(value, attributes, renderers, blockHandlers, options) {
4084
4162
 
4085
4163
  // src/conversion/html/html-to-delta.ts
4086
4164
  var import_delta9 = require("@scrider/delta");
4165
+ var BLOCKQUOTE_INNER_BLOCK_TAGS = /* @__PURE__ */ new Set([
4166
+ "p",
4167
+ "div",
4168
+ "h1",
4169
+ "h2",
4170
+ "h3",
4171
+ "h4",
4172
+ "h5",
4173
+ "h6",
4174
+ "ul",
4175
+ "ol",
4176
+ "li",
4177
+ "pre",
4178
+ "blockquote",
4179
+ "table"
4180
+ ]);
4087
4181
  function isCssWideKeyword(value) {
4088
4182
  return /^(inherit|initial|unset|revert|revert-layer)$/i.test(value.trim());
4089
4183
  }
@@ -4183,6 +4277,12 @@ function htmlToDelta(html, options = {}) {
4183
4277
  pendingText = "";
4184
4278
  }
4185
4279
  }
4280
+ function beginBlockEmbedLine() {
4281
+ flushText();
4282
+ if (!atLineStart) {
4283
+ context.pushNewline();
4284
+ }
4285
+ }
4186
4286
  function processNode(node) {
4187
4287
  if (node.nodeType === NODE_TYPE.TEXT_NODE) {
4188
4288
  const text = node.textContent ?? "";
@@ -4330,9 +4430,21 @@ function htmlToDelta(html, options = {}) {
4330
4430
  }
4331
4431
  }
4332
4432
  processChildren(element);
4333
- context.pushNewline();
4433
+ if (!(format.format === "blockquote" && hasBlockChildElement(element))) {
4434
+ context.pushNewline();
4435
+ }
4334
4436
  currentBlockAttributes = prevBlockAttrs;
4335
4437
  }
4438
+ function hasBlockChildElement(element) {
4439
+ const children2 = element.childNodes;
4440
+ for (let i = 0; i < children2.length; i++) {
4441
+ const child = children2[i];
4442
+ if (!child || !isElement(child)) continue;
4443
+ const tag = child.tagName.toLowerCase();
4444
+ if (BLOCKQUOTE_INNER_BLOCK_TAGS.has(tag)) return true;
4445
+ }
4446
+ return false;
4447
+ }
4336
4448
  function processCodeBlockElement(element) {
4337
4449
  const prevBlockAttrs = { ...currentBlockAttributes };
4338
4450
  let language;
@@ -4549,7 +4661,7 @@ function htmlToDelta(html, options = {}) {
4549
4661
  };
4550
4662
  const data = footnotesHandler.fromHtml(section, blockContext);
4551
4663
  if (data) {
4552
- flushText();
4664
+ beginBlockEmbedLine();
4553
4665
  delta.insert({ block: data });
4554
4666
  delta.insert("\n");
4555
4667
  atLineStart = true;
@@ -4571,7 +4683,7 @@ function htmlToDelta(html, options = {}) {
4571
4683
  };
4572
4684
  const data = alertHandler.fromHtml(element, blockContext);
4573
4685
  if (data) {
4574
- flushText();
4686
+ beginBlockEmbedLine();
4575
4687
  delta.insert({ block: data });
4576
4688
  delta.insert("\n");
4577
4689
  atLineStart = true;
@@ -4593,7 +4705,7 @@ function htmlToDelta(html, options = {}) {
4593
4705
  };
4594
4706
  const data = columnsHandler.fromHtml(element, blockContext);
4595
4707
  if (data) {
4596
- flushText();
4708
+ beginBlockEmbedLine();
4597
4709
  delta.insert({ block: data });
4598
4710
  delta.insert("\n");
4599
4711
  atLineStart = true;
@@ -4615,7 +4727,7 @@ function htmlToDelta(html, options = {}) {
4615
4727
  };
4616
4728
  const data = boxHandler.fromHtml(element, blockContext);
4617
4729
  if (data) {
4618
- flushText();
4730
+ beginBlockEmbedLine();
4619
4731
  const opAttrs = {};
4620
4732
  const dataFloat = element.getAttribute("data-float");
4621
4733
  if (dataFloat) opAttrs.float = dataFloat;
@@ -4664,7 +4776,7 @@ function htmlToDelta(html, options = {}) {
4664
4776
  let data = tableHandler.fromHtml(table, makeTableBlockParseContext());
4665
4777
  if (!data) return false;
4666
4778
  if (host) data = enrichTableBlockFromHost(data, host);
4667
- flushText();
4779
+ beginBlockEmbedLine();
4668
4780
  delta.insert({ block: data });
4669
4781
  delta.insert("\n");
4670
4782
  atLineStart = true;