@scrider/formatter 1.10.1 → 1.10.4

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
@@ -456,7 +456,7 @@ var EMBED_RENDERERS = {
456
456
  if (h && h !== "auto") styles.push(`height: ${/^\d+$/.test(h) ? h + "px" : h}`);
457
457
  }
458
458
  const style = styles.length > 0 ? ` style="${styles.join("; ")}"` : "";
459
- const embedSrc = toCodeWidgetEmbedUrl(src);
459
+ const embedSrc = resolveCodeWidgetEmbedSrc(src, context);
460
460
  return `<iframe data-code-widget src="${escapeHtml(embedSrc)}" frameborder="0" allowfullscreen${renderEmbedIframeIsolationAttrs(context, "codeWidget")}${float}${style}></iframe>`;
461
461
  },
462
462
  // `data-scrider-embed` marks this span as 1 Delta char for DomBridge before
@@ -556,31 +556,6 @@ function fromVideoEmbedUrl(embedUrl) {
556
556
  }
557
557
  return embedUrl;
558
558
  }
559
- function splitUrl(url) {
560
- let rest = url;
561
- let hash = "";
562
- const hashIdx = rest.indexOf("#");
563
- if (hashIdx >= 0) {
564
- hash = rest.slice(hashIdx);
565
- rest = rest.slice(0, hashIdx);
566
- }
567
- let query = "";
568
- const qIdx = rest.indexOf("?");
569
- if (qIdx >= 0) {
570
- query = rest.slice(qIdx);
571
- rest = rest.slice(0, qIdx);
572
- }
573
- return { base: rest, query, hash };
574
- }
575
- function hasQueryParam(url, key) {
576
- const { query } = splitUrl(url);
577
- return new RegExp(`[?&]${key}=`, "i").test(query);
578
- }
579
- function appendQueryParam(url, key, value) {
580
- const { base, query, hash } = splitUrl(url);
581
- const next = query ? `${query}&${key}=${value}` : `?${key}=${value}`;
582
- return `${base}${next}${hash}`;
583
- }
584
559
  var CODE_WIDGET_IFRAME_ALLOW = "accelerometer; camera; encrypted-media; geolocation; gyroscope; microphone; midi; payment; usb; xr-spatial-tracking; cross-origin-isolated";
585
560
  function renderEmbedIframeIsolationAttrs(context, kind) {
586
561
  const opts = context?.embed;
@@ -593,38 +568,13 @@ function renderEmbedIframeIsolationAttrs(context, kind) {
593
568
  }
594
569
  return parts.length ? ` ${parts.join(" ")}` : "";
595
570
  }
596
- function toCodeWidgetEmbedUrl(url) {
571
+ function resolveCodeWidgetEmbedSrc(url, context) {
597
572
  const u = typeof url === "string" ? url.trim() : "";
598
573
  if (!u) return "";
599
- if (/(?:\/\/|^)(?:[\w-]+\.)*stackblitz\.com\//i.test(u)) {
600
- return hasQueryParam(u, "embed") ? u : appendQueryParam(u, "embed", "1");
601
- }
602
- if (/(?:\/\/|^)(?:[\w-]+\.)*codesandbox\.io\//i.test(u)) {
603
- if (/codesandbox\.io\/embed\//i.test(u)) return u;
604
- return u.replace(/codesandbox\.io\/s\//i, "codesandbox.io/embed/");
605
- }
606
- if (/(?:\/\/|^)(?:[\w-]+\.)*replit\.com\//i.test(u)) {
607
- return hasQueryParam(u, "embed") ? u : appendQueryParam(u, "embed", "true");
608
- }
609
- if (/(?:\/\/|^)(?:[\w-]+\.)*codepen\.io\//i.test(u)) {
610
- if (/codepen\.io\/[^/]+\/embed\//i.test(u)) return u;
611
- return u.replace(/(codepen\.io\/[^/]+)\/pen\//i, "$1/embed/");
612
- }
613
- if (/(?:\/\/|^)(?:[\w-]+\.)*jsfiddle\.net\//i.test(u)) {
614
- const { base, query, hash } = splitUrl(u);
615
- if (/\/embedded(?:\/|$)/i.test(base)) return u;
616
- const trimmed = base.replace(/\/+$/, "");
617
- return `${trimmed}/embedded/${query}${hash}`;
618
- }
619
- if (/(?:\/\/|^)(?:[\w-]+\.)*trinket\.io\//i.test(u)) {
620
- if (/trinket\.io\/embed\//i.test(u)) return u;
621
- return u.replace(/trinket\.io\//i, "trinket.io/embed/");
622
- }
623
- if (/(?:\/\/|^)(?:[\w-]+\.)*onecompiler\.com\//i.test(u)) {
624
- if (/onecompiler\.com\/embed\//i.test(u)) return u;
625
- return u.replace(/onecompiler\.com\//i, "onecompiler.com/embed/");
626
- }
627
- return u;
574
+ return context?.codeWidgetEmbedUrl ? context.codeWidgetEmbedUrl(u) : u;
575
+ }
576
+ function toCodeWidgetEmbedUrl(url) {
577
+ return typeof url === "string" ? url.trim() : "";
628
578
  }
629
579
 
630
580
  // src/schema/blocks/table.ts
@@ -2467,7 +2417,7 @@ var codeWidgetFormat = {
2467
2417
  if (h && h !== "auto") styles.push(`height: ${/^\d+$/.test(h) ? h + "px" : h}`);
2468
2418
  }
2469
2419
  const style = styles.length > 0 ? ` style="${styles.join("; ")}"` : "";
2470
- const embedSrc = toCodeWidgetEmbedUrl(src);
2420
+ const embedSrc = resolveCodeWidgetEmbedSrc(src, context);
2471
2421
  return `<iframe data-code-widget src="${escapeHtml(embedSrc)}" frameborder="0" allowfullscreen${renderEmbedIframeIsolationAttrs(context, "codeWidget")}${float}${style}></iframe>`;
2472
2422
  },
2473
2423
  match(element) {
@@ -3178,6 +3128,90 @@ function joinStyleParts(parts) {
3178
3128
  return parts.length > 0 ? ` style="${parts.join("; ")}"` : "";
3179
3129
  }
3180
3130
 
3131
+ // src/conversion/html/heading-presentation.ts
3132
+ var HEADER_SIZE_PRESETS = Object.freeze({
3133
+ scrider: Object.freeze({
3134
+ 1: "32pt",
3135
+ 2: "24pt",
3136
+ 3: "20pt",
3137
+ 4: "18pt",
3138
+ 5: "16pt",
3139
+ 6: "14pt"
3140
+ }),
3141
+ google: Object.freeze({
3142
+ 1: "26pt",
3143
+ 2: "20pt",
3144
+ 3: "16pt",
3145
+ 4: "14pt",
3146
+ 5: "12pt",
3147
+ 6: "11pt"
3148
+ }),
3149
+ word: Object.freeze({
3150
+ 1: "22pt",
3151
+ 2: "18pt",
3152
+ 3: "16pt",
3153
+ 4: "14pt",
3154
+ 5: "12pt",
3155
+ 6: "12pt"
3156
+ }),
3157
+ browser: Object.freeze({
3158
+ 1: "24pt",
3159
+ 2: "18pt",
3160
+ 3: "14pt",
3161
+ 4: "12pt",
3162
+ 5: "10pt",
3163
+ 6: "8pt"
3164
+ }),
3165
+ githubEm: Object.freeze({
3166
+ 1: "32pt",
3167
+ 2: "24pt",
3168
+ 3: "20pt",
3169
+ 4: "16pt",
3170
+ 5: "14pt",
3171
+ 6: "14pt"
3172
+ })
3173
+ });
3174
+ var HEADING_TAGS = /* @__PURE__ */ new Set(["h1", "h2", "h3", "h4", "h5", "h6"]);
3175
+ function isHeaderSizePresetName(value) {
3176
+ return Object.prototype.hasOwnProperty.call(HEADER_SIZE_PRESETS, value);
3177
+ }
3178
+ function resolveHeadingPolicy(metadata) {
3179
+ if (!metadata) return void 0;
3180
+ const align = metadata.headingAlign === "left" || metadata.headingAlign === "center" || metadata.headingAlign === "right" ? metadata.headingAlign : void 0;
3181
+ const bold = metadata.headingBold === true;
3182
+ const auto = metadata.headingAuto === true;
3183
+ const presetRaw = metadata.headingSizeGridPreset;
3184
+ const sizePreset = !auto && typeof presetRaw === "string" && isHeaderSizePresetName(presetRaw) ? presetRaw : void 0;
3185
+ if (align === void 0 && !bold && sizePreset === void 0 && !auto) {
3186
+ return void 0;
3187
+ }
3188
+ return { align, bold, sizePreset, auto };
3189
+ }
3190
+ function headingLevelFromTag(tag) {
3191
+ if (!HEADING_TAGS.has(tag)) return void 0;
3192
+ const n = Number(tag.slice(1));
3193
+ if (n >= 1 && n <= 6) return n;
3194
+ return void 0;
3195
+ }
3196
+ function headingPolicyStyleParts(tag, blockAttributes, policy) {
3197
+ if (!policy) return [];
3198
+ const level = headingLevelFromTag(tag);
3199
+ if (level === void 0) return [];
3200
+ const parts = [];
3201
+ const bakedAlign = blockAttributes?.align;
3202
+ if (policy.align && policy.align !== "left" && !(typeof bakedAlign === "string" && bakedAlign.length > 0)) {
3203
+ parts.push(`text-align: ${policy.align}`);
3204
+ }
3205
+ if (policy.bold) {
3206
+ parts.push("font-weight: bold");
3207
+ }
3208
+ if (policy.sizePreset) {
3209
+ const size = HEADER_SIZE_PRESETS[policy.sizePreset][level];
3210
+ if (size) parts.push(`font-size: ${size}`);
3211
+ }
3212
+ return parts;
3213
+ }
3214
+
3181
3215
  // src/conversion/html/table-presentation.ts
3182
3216
  var DEFAULT_BORDER_COLOR = "#e7e7e7";
3183
3217
  var DEFAULT_HEADER_BG = "#f5f5f5";
@@ -3342,6 +3376,12 @@ function deltaToHtml(delta, options = {}) {
3342
3376
  const resolvedDocumentPresentation = resolveDocumentPresentation(
3343
3377
  options.documentPresentation ?? documentMetadataToPresentation(options.documentMetadata)
3344
3378
  );
3379
+ const resolvedHeadingPolicy = resolveHeadingPolicy(options.documentMetadata);
3380
+ const effectiveTablePresentation = options.tablePresentation ?? options.documentMetadata?.tablePresentation;
3381
+ const optionsWithTable = {
3382
+ ...options,
3383
+ ...effectiveTablePresentation !== void 0 ? { tablePresentation: effectiveTablePresentation } : {}
3384
+ };
3345
3385
  let html = "";
3346
3386
  let listStack = [];
3347
3387
  let counters = [];
@@ -3354,7 +3394,7 @@ function deltaToHtml(delta, options = {}) {
3354
3394
  listStack = [];
3355
3395
  counters = [];
3356
3396
  const tableLines = collectAdjacentTableLines(lines, i);
3357
- html += renderTable(tableLines, embedRenderers, pretty, blockHandlers, options);
3397
+ html += renderTable(tableLines, embedRenderers, pretty, blockHandlers, optionsWithTable);
3358
3398
  i += tableLines.length - 1;
3359
3399
  continue;
3360
3400
  }
@@ -3445,7 +3485,8 @@ function deltaToHtml(delta, options = {}) {
3445
3485
  line.attributes,
3446
3486
  pretty,
3447
3487
  headingId,
3448
- resolvedDocumentPresentation
3488
+ resolvedDocumentPresentation,
3489
+ resolvedHeadingPolicy
3449
3490
  );
3450
3491
  }
3451
3492
  }
@@ -3765,19 +3806,23 @@ function renderListItem(content, attrs, blockAttributes, pretty, indentLevel, hi
3765
3806
  const html = `${indent}<li${fullAttrs}>${innerContent}</li>`;
3766
3807
  return pretty ? html + "\n" : html;
3767
3808
  }
3768
- function renderBlock(content, tag, attributes, pretty, id, resolvedDocumentPresentation) {
3809
+ function renderBlock(content, tag, attributes, pretty, id, resolvedDocumentPresentation, resolvedHeadingPolicy) {
3769
3810
  const idAttr = id ? ` id="${escapeHtml(id)}"` : "";
3770
- const styleAttr = getBlockStyleAttribute(tag, attributes, resolvedDocumentPresentation);
3811
+ const styleAttr = getBlockStyleAttribute(
3812
+ tag,
3813
+ attributes,
3814
+ resolvedDocumentPresentation,
3815
+ resolvedHeadingPolicy
3816
+ );
3771
3817
  const innerContent = content || "<br>";
3772
3818
  const html = `<${tag}${idAttr}${styleAttr}>${innerContent}</${tag}>`;
3773
3819
  return pretty ? html + "\n" : html;
3774
3820
  }
3775
- function getBlockStyleAttribute(tag, attributes, resolvedDocumentPresentation) {
3776
- const styles = blockPresentationStyleParts(
3777
- tag,
3778
- attributes,
3779
- resolvedDocumentPresentation
3780
- );
3821
+ function getBlockStyleAttribute(tag, attributes, resolvedDocumentPresentation, resolvedHeadingPolicy) {
3822
+ const styles = [
3823
+ ...blockPresentationStyleParts(tag, attributes, resolvedDocumentPresentation),
3824
+ ...headingPolicyStyleParts(tag, attributes, resolvedHeadingPolicy)
3825
+ ];
3781
3826
  if (attributes) {
3782
3827
  const alignVal = attributes.align;
3783
3828
  if (alignVal && typeof alignVal === "string" && alignVal !== "left") {
@@ -3869,7 +3914,10 @@ function renderEmbed(value, attributes, renderers, blockHandlers, options) {
3869
3914
  return "";
3870
3915
  }
3871
3916
  const embedValue = value[embedType];
3872
- const embedContext = options?.embed != null ? { embed: options.embed } : {};
3917
+ const embedContext = {
3918
+ ...options?.embed != null ? { embed: options.embed } : {},
3919
+ ...options?.codeWidgetEmbedUrl != null ? { codeWidgetEmbedUrl: options.codeWidgetEmbedUrl } : {}
3920
+ };
3873
3921
  const registry = options?.registry;
3874
3922
  if (registry) {
3875
3923
  const format = registry.get(embedType);
@@ -4703,7 +4751,8 @@ function deltaToMarkdown(delta, options = {}) {
4703
4751
  prettyHtml = false,
4704
4752
  registry,
4705
4753
  softBreakStyle = "spaces",
4706
- trimTrailingNewlines = false
4754
+ trimTrailingNewlines = false,
4755
+ codeWidgetEmbedUrl
4707
4756
  } = options;
4708
4757
  const useLatexDelimiters = mathSyntax === "latex";
4709
4758
  const lines = splitIntoLines2(delta.ops);
@@ -4745,7 +4794,9 @@ function deltaToMarkdown(delta, options = {}) {
4745
4794
  blockHandlers,
4746
4795
  false,
4747
4796
  registry,
4748
- softBreakStyle
4797
+ softBreakStyle,
4798
+ false,
4799
+ codeWidgetEmbedUrl
4749
4800
  )
4750
4801
  ).join("\n");
4751
4802
  if (language === "math") {
@@ -4797,7 +4848,9 @@ ${code}
4797
4848
  blockHandlers,
4798
4849
  prettyHtml,
4799
4850
  registry,
4800
- softBreakStyle
4851
+ softBreakStyle,
4852
+ false,
4853
+ codeWidgetEmbedUrl
4801
4854
  );
4802
4855
  if (!content && !hasBlockFormat(attrs)) {
4803
4856
  result.push(preserveEmptyLines ? "<br>" : "");
@@ -5021,7 +5074,7 @@ function getCodeBlockId2(attributes) {
5021
5074
  const id = attributes["code-block-id"];
5022
5075
  return typeof id === "string" ? id : void 0;
5023
5076
  }
5024
- function renderLineContent2(ops, embedRenderers, inCodeBlock, useLatexDelimiters = false, blockHandlers, prettyHtml = false, registry, softBreakStyle = "spaces", inTableCell = false) {
5077
+ function renderLineContent2(ops, embedRenderers, inCodeBlock, useLatexDelimiters = false, blockHandlers, prettyHtml = false, registry, softBreakStyle = "spaces", inTableCell = false, codeWidgetEmbedUrl) {
5025
5078
  let result = "";
5026
5079
  for (const op of ops) {
5027
5080
  const attrs = op.attributes;
@@ -5043,7 +5096,8 @@ function renderLineContent2(ops, embedRenderers, inCodeBlock, useLatexDelimiters
5043
5096
  prettyHtml,
5044
5097
  registry,
5045
5098
  softBreakStyle,
5046
- inTableCell
5099
+ inTableCell,
5100
+ codeWidgetEmbedUrl
5047
5101
  );
5048
5102
  }
5049
5103
  }
@@ -5077,7 +5131,7 @@ function renderInlineText2(text, attributes) {
5077
5131
  }
5078
5132
  return result;
5079
5133
  }
5080
- function renderEmbed2(embed, attributes, customRenderers, useLatexDelimiters = false, blockHandlers, prettyHtml = false, registry, softBreakStyle = "spaces", inTableCell = false) {
5134
+ function renderEmbed2(embed, attributes, customRenderers, useLatexDelimiters = false, blockHandlers, prettyHtml = false, registry, softBreakStyle = "spaces", inTableCell = false, codeWidgetEmbedUrl) {
5081
5135
  const entries = Object.entries(embed);
5082
5136
  if (entries.length === 0) return "";
5083
5137
  const firstEntry = entries[0];
@@ -5126,7 +5180,11 @@ function renderEmbed2(embed, attributes, customRenderers, useLatexDelimiters = f
5126
5180
  if (md !== null) return md;
5127
5181
  }
5128
5182
  if (format.render) {
5129
- return format.render(embedValue, attributes);
5183
+ return format.render(
5184
+ embedValue,
5185
+ attributes,
5186
+ codeWidgetEmbedUrl != null ? { codeWidgetEmbedUrl } : void 0
5187
+ );
5130
5188
  }
5131
5189
  }
5132
5190
  }
@@ -5187,7 +5245,7 @@ function renderEmbed2(embed, attributes, customRenderers, useLatexDelimiters = f
5187
5245
  if (h && h !== "auto") styles.push(`height: ${/^\d+$/.test(h) ? h + "px" : h}`);
5188
5246
  }
5189
5247
  const styleAttr = styles.length > 0 ? ` style="${styles.join("; ")}"` : "";
5190
- const embedSrc = toCodeWidgetEmbedUrl(src);
5248
+ const embedSrc = codeWidgetEmbedUrl ? codeWidgetEmbedUrl(src) : src;
5191
5249
  return `<iframe data-code-widget src="${escapeHtml(embedSrc)}" frameborder="0" allowfullscreen${floatAttr}${styleAttr}></iframe>`;
5192
5250
  }
5193
5251
  return `![Widget](${src})`;
@@ -6097,6 +6155,7 @@ export {
6097
6155
  BOX_OVERFLOW_VALUES,
6098
6156
  BlockHandlerRegistry,
6099
6157
  BrowserDOMAdapter,
6158
+ HEADER_SIZE_PRESETS,
6100
6159
  LINE_HEIGHT_BLOCK_TAGS,
6101
6160
  NODE_TYPE,
6102
6161
  NodeDOMAdapter,
@@ -6151,6 +6210,7 @@ export {
6151
6210
  getNamedColors,
6152
6211
  headerFormat,
6153
6212
  headerIdFormat,
6213
+ headingPolicyStyleParts,
6154
6214
  htmlToDelta,
6155
6215
  imageFormat,
6156
6216
  indentFormat,
@@ -6178,7 +6238,9 @@ export {
6178
6238
  parseScriderMarginEm,
6179
6239
  preloadRemark,
6180
6240
  renderDelta,
6241
+ resolveCodeWidgetEmbedSrc,
6181
6242
  resolveDocumentPresentation,
6243
+ resolveHeadingPolicy,
6182
6244
  resolveTablePresentation,
6183
6245
  sanitizeDelta,
6184
6246
  sizeFormat,