@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.cjs CHANGED
@@ -36,6 +36,7 @@ __export(index_exports, {
36
36
  BOX_OVERFLOW_VALUES: () => BOX_OVERFLOW_VALUES,
37
37
  BlockHandlerRegistry: () => BlockHandlerRegistry,
38
38
  BrowserDOMAdapter: () => BrowserDOMAdapter,
39
+ HEADER_SIZE_PRESETS: () => HEADER_SIZE_PRESETS,
39
40
  LINE_HEIGHT_BLOCK_TAGS: () => LINE_HEIGHT_BLOCK_TAGS,
40
41
  NODE_TYPE: () => NODE_TYPE,
41
42
  NodeDOMAdapter: () => NodeDOMAdapter,
@@ -90,6 +91,7 @@ __export(index_exports, {
90
91
  getNamedColors: () => getNamedColors,
91
92
  headerFormat: () => headerFormat,
92
93
  headerIdFormat: () => headerIdFormat,
94
+ headingPolicyStyleParts: () => headingPolicyStyleParts,
93
95
  htmlToDelta: () => htmlToDelta,
94
96
  imageFormat: () => imageFormat,
95
97
  indentFormat: () => indentFormat,
@@ -117,7 +119,9 @@ __export(index_exports, {
117
119
  parseScriderMarginEm: () => parseScriderMarginEm,
118
120
  preloadRemark: () => preloadRemark,
119
121
  renderDelta: () => renderDelta,
122
+ resolveCodeWidgetEmbedSrc: () => resolveCodeWidgetEmbedSrc,
120
123
  resolveDocumentPresentation: () => resolveDocumentPresentation,
124
+ resolveHeadingPolicy: () => resolveHeadingPolicy,
121
125
  resolveTablePresentation: () => resolveTablePresentation,
122
126
  sanitizeDelta: () => sanitizeDelta,
123
127
  sizeFormat: () => sizeFormat,
@@ -592,7 +596,7 @@ var EMBED_RENDERERS = {
592
596
  if (h && h !== "auto") styles.push(`height: ${/^\d+$/.test(h) ? h + "px" : h}`);
593
597
  }
594
598
  const style = styles.length > 0 ? ` style="${styles.join("; ")}"` : "";
595
- const embedSrc = toCodeWidgetEmbedUrl(src);
599
+ const embedSrc = resolveCodeWidgetEmbedSrc(src, context);
596
600
  return `<iframe data-code-widget src="${escapeHtml(embedSrc)}" frameborder="0" allowfullscreen${renderEmbedIframeIsolationAttrs(context, "codeWidget")}${float}${style}></iframe>`;
597
601
  },
598
602
  // `data-scrider-embed` marks this span as 1 Delta char for DomBridge before
@@ -692,31 +696,6 @@ function fromVideoEmbedUrl(embedUrl) {
692
696
  }
693
697
  return embedUrl;
694
698
  }
695
- function splitUrl(url) {
696
- let rest = url;
697
- let hash = "";
698
- const hashIdx = rest.indexOf("#");
699
- if (hashIdx >= 0) {
700
- hash = rest.slice(hashIdx);
701
- rest = rest.slice(0, hashIdx);
702
- }
703
- let query = "";
704
- const qIdx = rest.indexOf("?");
705
- if (qIdx >= 0) {
706
- query = rest.slice(qIdx);
707
- rest = rest.slice(0, qIdx);
708
- }
709
- return { base: rest, query, hash };
710
- }
711
- function hasQueryParam(url, key) {
712
- const { query } = splitUrl(url);
713
- return new RegExp(`[?&]${key}=`, "i").test(query);
714
- }
715
- function appendQueryParam(url, key, value) {
716
- const { base, query, hash } = splitUrl(url);
717
- const next = query ? `${query}&${key}=${value}` : `?${key}=${value}`;
718
- return `${base}${next}${hash}`;
719
- }
720
699
  var CODE_WIDGET_IFRAME_ALLOW = "accelerometer; camera; encrypted-media; geolocation; gyroscope; microphone; midi; payment; usb; xr-spatial-tracking; cross-origin-isolated";
721
700
  function renderEmbedIframeIsolationAttrs(context, kind) {
722
701
  const opts = context?.embed;
@@ -729,38 +708,13 @@ function renderEmbedIframeIsolationAttrs(context, kind) {
729
708
  }
730
709
  return parts.length ? ` ${parts.join(" ")}` : "";
731
710
  }
732
- function toCodeWidgetEmbedUrl(url) {
711
+ function resolveCodeWidgetEmbedSrc(url, context) {
733
712
  const u = typeof url === "string" ? url.trim() : "";
734
713
  if (!u) return "";
735
- if (/(?:\/\/|^)(?:[\w-]+\.)*stackblitz\.com\//i.test(u)) {
736
- return hasQueryParam(u, "embed") ? u : appendQueryParam(u, "embed", "1");
737
- }
738
- if (/(?:\/\/|^)(?:[\w-]+\.)*codesandbox\.io\//i.test(u)) {
739
- if (/codesandbox\.io\/embed\//i.test(u)) return u;
740
- return u.replace(/codesandbox\.io\/s\//i, "codesandbox.io/embed/");
741
- }
742
- if (/(?:\/\/|^)(?:[\w-]+\.)*replit\.com\//i.test(u)) {
743
- return hasQueryParam(u, "embed") ? u : appendQueryParam(u, "embed", "true");
744
- }
745
- if (/(?:\/\/|^)(?:[\w-]+\.)*codepen\.io\//i.test(u)) {
746
- if (/codepen\.io\/[^/]+\/embed\//i.test(u)) return u;
747
- return u.replace(/(codepen\.io\/[^/]+)\/pen\//i, "$1/embed/");
748
- }
749
- if (/(?:\/\/|^)(?:[\w-]+\.)*jsfiddle\.net\//i.test(u)) {
750
- const { base, query, hash } = splitUrl(u);
751
- if (/\/embedded(?:\/|$)/i.test(base)) return u;
752
- const trimmed = base.replace(/\/+$/, "");
753
- return `${trimmed}/embedded/${query}${hash}`;
754
- }
755
- if (/(?:\/\/|^)(?:[\w-]+\.)*trinket\.io\//i.test(u)) {
756
- if (/trinket\.io\/embed\//i.test(u)) return u;
757
- return u.replace(/trinket\.io\//i, "trinket.io/embed/");
758
- }
759
- if (/(?:\/\/|^)(?:[\w-]+\.)*onecompiler\.com\//i.test(u)) {
760
- if (/onecompiler\.com\/embed\//i.test(u)) return u;
761
- return u.replace(/onecompiler\.com\//i, "onecompiler.com/embed/");
762
- }
763
- return u;
714
+ return context?.codeWidgetEmbedUrl ? context.codeWidgetEmbedUrl(u) : u;
715
+ }
716
+ function toCodeWidgetEmbedUrl(url) {
717
+ return typeof url === "string" ? url.trim() : "";
764
718
  }
765
719
 
766
720
  // src/schema/blocks/table.ts
@@ -2603,7 +2557,7 @@ var codeWidgetFormat = {
2603
2557
  if (h && h !== "auto") styles.push(`height: ${/^\d+$/.test(h) ? h + "px" : h}`);
2604
2558
  }
2605
2559
  const style = styles.length > 0 ? ` style="${styles.join("; ")}"` : "";
2606
- const embedSrc = toCodeWidgetEmbedUrl(src);
2560
+ const embedSrc = resolveCodeWidgetEmbedSrc(src, context);
2607
2561
  return `<iframe data-code-widget src="${escapeHtml(embedSrc)}" frameborder="0" allowfullscreen${renderEmbedIframeIsolationAttrs(context, "codeWidget")}${float}${style}></iframe>`;
2608
2562
  },
2609
2563
  match(element) {
@@ -3314,6 +3268,90 @@ function joinStyleParts(parts) {
3314
3268
  return parts.length > 0 ? ` style="${parts.join("; ")}"` : "";
3315
3269
  }
3316
3270
 
3271
+ // src/conversion/html/heading-presentation.ts
3272
+ var HEADER_SIZE_PRESETS = Object.freeze({
3273
+ scrider: Object.freeze({
3274
+ 1: "32pt",
3275
+ 2: "24pt",
3276
+ 3: "20pt",
3277
+ 4: "18pt",
3278
+ 5: "16pt",
3279
+ 6: "14pt"
3280
+ }),
3281
+ google: Object.freeze({
3282
+ 1: "26pt",
3283
+ 2: "20pt",
3284
+ 3: "16pt",
3285
+ 4: "14pt",
3286
+ 5: "12pt",
3287
+ 6: "11pt"
3288
+ }),
3289
+ word: Object.freeze({
3290
+ 1: "22pt",
3291
+ 2: "18pt",
3292
+ 3: "16pt",
3293
+ 4: "14pt",
3294
+ 5: "12pt",
3295
+ 6: "12pt"
3296
+ }),
3297
+ browser: Object.freeze({
3298
+ 1: "24pt",
3299
+ 2: "18pt",
3300
+ 3: "14pt",
3301
+ 4: "12pt",
3302
+ 5: "10pt",
3303
+ 6: "8pt"
3304
+ }),
3305
+ githubEm: Object.freeze({
3306
+ 1: "32pt",
3307
+ 2: "24pt",
3308
+ 3: "20pt",
3309
+ 4: "16pt",
3310
+ 5: "14pt",
3311
+ 6: "14pt"
3312
+ })
3313
+ });
3314
+ var HEADING_TAGS = /* @__PURE__ */ new Set(["h1", "h2", "h3", "h4", "h5", "h6"]);
3315
+ function isHeaderSizePresetName(value) {
3316
+ return Object.prototype.hasOwnProperty.call(HEADER_SIZE_PRESETS, value);
3317
+ }
3318
+ function resolveHeadingPolicy(metadata) {
3319
+ if (!metadata) return void 0;
3320
+ const align = metadata.headingAlign === "left" || metadata.headingAlign === "center" || metadata.headingAlign === "right" ? metadata.headingAlign : void 0;
3321
+ const bold = metadata.headingBold === true;
3322
+ const auto = metadata.headingAuto === true;
3323
+ const presetRaw = metadata.headingSizeGridPreset;
3324
+ const sizePreset = !auto && typeof presetRaw === "string" && isHeaderSizePresetName(presetRaw) ? presetRaw : void 0;
3325
+ if (align === void 0 && !bold && sizePreset === void 0 && !auto) {
3326
+ return void 0;
3327
+ }
3328
+ return { align, bold, sizePreset, auto };
3329
+ }
3330
+ function headingLevelFromTag(tag) {
3331
+ if (!HEADING_TAGS.has(tag)) return void 0;
3332
+ const n = Number(tag.slice(1));
3333
+ if (n >= 1 && n <= 6) return n;
3334
+ return void 0;
3335
+ }
3336
+ function headingPolicyStyleParts(tag, blockAttributes, policy) {
3337
+ if (!policy) return [];
3338
+ const level = headingLevelFromTag(tag);
3339
+ if (level === void 0) return [];
3340
+ const parts = [];
3341
+ const bakedAlign = blockAttributes?.align;
3342
+ if (policy.align && policy.align !== "left" && !(typeof bakedAlign === "string" && bakedAlign.length > 0)) {
3343
+ parts.push(`text-align: ${policy.align}`);
3344
+ }
3345
+ if (policy.bold) {
3346
+ parts.push("font-weight: bold");
3347
+ }
3348
+ if (policy.sizePreset) {
3349
+ const size = HEADER_SIZE_PRESETS[policy.sizePreset][level];
3350
+ if (size) parts.push(`font-size: ${size}`);
3351
+ }
3352
+ return parts;
3353
+ }
3354
+
3317
3355
  // src/conversion/html/table-presentation.ts
3318
3356
  var DEFAULT_BORDER_COLOR = "#e7e7e7";
3319
3357
  var DEFAULT_HEADER_BG = "#f5f5f5";
@@ -3478,6 +3516,12 @@ function deltaToHtml(delta, options = {}) {
3478
3516
  const resolvedDocumentPresentation = resolveDocumentPresentation(
3479
3517
  options.documentPresentation ?? documentMetadataToPresentation(options.documentMetadata)
3480
3518
  );
3519
+ const resolvedHeadingPolicy = resolveHeadingPolicy(options.documentMetadata);
3520
+ const effectiveTablePresentation = options.tablePresentation ?? options.documentMetadata?.tablePresentation;
3521
+ const optionsWithTable = {
3522
+ ...options,
3523
+ ...effectiveTablePresentation !== void 0 ? { tablePresentation: effectiveTablePresentation } : {}
3524
+ };
3481
3525
  let html = "";
3482
3526
  let listStack = [];
3483
3527
  let counters = [];
@@ -3490,7 +3534,7 @@ function deltaToHtml(delta, options = {}) {
3490
3534
  listStack = [];
3491
3535
  counters = [];
3492
3536
  const tableLines = collectAdjacentTableLines(lines, i);
3493
- html += renderTable(tableLines, embedRenderers, pretty, blockHandlers, options);
3537
+ html += renderTable(tableLines, embedRenderers, pretty, blockHandlers, optionsWithTable);
3494
3538
  i += tableLines.length - 1;
3495
3539
  continue;
3496
3540
  }
@@ -3581,7 +3625,8 @@ function deltaToHtml(delta, options = {}) {
3581
3625
  line.attributes,
3582
3626
  pretty,
3583
3627
  headingId,
3584
- resolvedDocumentPresentation
3628
+ resolvedDocumentPresentation,
3629
+ resolvedHeadingPolicy
3585
3630
  );
3586
3631
  }
3587
3632
  }
@@ -3901,19 +3946,23 @@ function renderListItem(content, attrs, blockAttributes, pretty, indentLevel, hi
3901
3946
  const html = `${indent}<li${fullAttrs}>${innerContent}</li>`;
3902
3947
  return pretty ? html + "\n" : html;
3903
3948
  }
3904
- function renderBlock(content, tag, attributes, pretty, id, resolvedDocumentPresentation) {
3949
+ function renderBlock(content, tag, attributes, pretty, id, resolvedDocumentPresentation, resolvedHeadingPolicy) {
3905
3950
  const idAttr = id ? ` id="${escapeHtml(id)}"` : "";
3906
- const styleAttr = getBlockStyleAttribute(tag, attributes, resolvedDocumentPresentation);
3951
+ const styleAttr = getBlockStyleAttribute(
3952
+ tag,
3953
+ attributes,
3954
+ resolvedDocumentPresentation,
3955
+ resolvedHeadingPolicy
3956
+ );
3907
3957
  const innerContent = content || "<br>";
3908
3958
  const html = `<${tag}${idAttr}${styleAttr}>${innerContent}</${tag}>`;
3909
3959
  return pretty ? html + "\n" : html;
3910
3960
  }
3911
- function getBlockStyleAttribute(tag, attributes, resolvedDocumentPresentation) {
3912
- const styles = blockPresentationStyleParts(
3913
- tag,
3914
- attributes,
3915
- resolvedDocumentPresentation
3916
- );
3961
+ function getBlockStyleAttribute(tag, attributes, resolvedDocumentPresentation, resolvedHeadingPolicy) {
3962
+ const styles = [
3963
+ ...blockPresentationStyleParts(tag, attributes, resolvedDocumentPresentation),
3964
+ ...headingPolicyStyleParts(tag, attributes, resolvedHeadingPolicy)
3965
+ ];
3917
3966
  if (attributes) {
3918
3967
  const alignVal = attributes.align;
3919
3968
  if (alignVal && typeof alignVal === "string" && alignVal !== "left") {
@@ -4005,7 +4054,10 @@ function renderEmbed(value, attributes, renderers, blockHandlers, options) {
4005
4054
  return "";
4006
4055
  }
4007
4056
  const embedValue = value[embedType];
4008
- const embedContext = options?.embed != null ? { embed: options.embed } : {};
4057
+ const embedContext = {
4058
+ ...options?.embed != null ? { embed: options.embed } : {},
4059
+ ...options?.codeWidgetEmbedUrl != null ? { codeWidgetEmbedUrl: options.codeWidgetEmbedUrl } : {}
4060
+ };
4009
4061
  const registry = options?.registry;
4010
4062
  if (registry) {
4011
4063
  const format = registry.get(embedType);
@@ -4839,7 +4891,8 @@ function deltaToMarkdown(delta, options = {}) {
4839
4891
  prettyHtml = false,
4840
4892
  registry,
4841
4893
  softBreakStyle = "spaces",
4842
- trimTrailingNewlines = false
4894
+ trimTrailingNewlines = false,
4895
+ codeWidgetEmbedUrl
4843
4896
  } = options;
4844
4897
  const useLatexDelimiters = mathSyntax === "latex";
4845
4898
  const lines = splitIntoLines2(delta.ops);
@@ -4881,7 +4934,9 @@ function deltaToMarkdown(delta, options = {}) {
4881
4934
  blockHandlers,
4882
4935
  false,
4883
4936
  registry,
4884
- softBreakStyle
4937
+ softBreakStyle,
4938
+ false,
4939
+ codeWidgetEmbedUrl
4885
4940
  )
4886
4941
  ).join("\n");
4887
4942
  if (language === "math") {
@@ -4933,7 +4988,9 @@ ${code}
4933
4988
  blockHandlers,
4934
4989
  prettyHtml,
4935
4990
  registry,
4936
- softBreakStyle
4991
+ softBreakStyle,
4992
+ false,
4993
+ codeWidgetEmbedUrl
4937
4994
  );
4938
4995
  if (!content && !hasBlockFormat(attrs)) {
4939
4996
  result.push(preserveEmptyLines ? "<br>" : "");
@@ -5157,7 +5214,7 @@ function getCodeBlockId2(attributes) {
5157
5214
  const id = attributes["code-block-id"];
5158
5215
  return typeof id === "string" ? id : void 0;
5159
5216
  }
5160
- function renderLineContent2(ops, embedRenderers, inCodeBlock, useLatexDelimiters = false, blockHandlers, prettyHtml = false, registry, softBreakStyle = "spaces", inTableCell = false) {
5217
+ function renderLineContent2(ops, embedRenderers, inCodeBlock, useLatexDelimiters = false, blockHandlers, prettyHtml = false, registry, softBreakStyle = "spaces", inTableCell = false, codeWidgetEmbedUrl) {
5161
5218
  let result = "";
5162
5219
  for (const op of ops) {
5163
5220
  const attrs = op.attributes;
@@ -5179,7 +5236,8 @@ function renderLineContent2(ops, embedRenderers, inCodeBlock, useLatexDelimiters
5179
5236
  prettyHtml,
5180
5237
  registry,
5181
5238
  softBreakStyle,
5182
- inTableCell
5239
+ inTableCell,
5240
+ codeWidgetEmbedUrl
5183
5241
  );
5184
5242
  }
5185
5243
  }
@@ -5213,7 +5271,7 @@ function renderInlineText2(text, attributes) {
5213
5271
  }
5214
5272
  return result;
5215
5273
  }
5216
- function renderEmbed2(embed, attributes, customRenderers, useLatexDelimiters = false, blockHandlers, prettyHtml = false, registry, softBreakStyle = "spaces", inTableCell = false) {
5274
+ function renderEmbed2(embed, attributes, customRenderers, useLatexDelimiters = false, blockHandlers, prettyHtml = false, registry, softBreakStyle = "spaces", inTableCell = false, codeWidgetEmbedUrl) {
5217
5275
  const entries = Object.entries(embed);
5218
5276
  if (entries.length === 0) return "";
5219
5277
  const firstEntry = entries[0];
@@ -5262,7 +5320,11 @@ function renderEmbed2(embed, attributes, customRenderers, useLatexDelimiters = f
5262
5320
  if (md !== null) return md;
5263
5321
  }
5264
5322
  if (format.render) {
5265
- return format.render(embedValue, attributes);
5323
+ return format.render(
5324
+ embedValue,
5325
+ attributes,
5326
+ codeWidgetEmbedUrl != null ? { codeWidgetEmbedUrl } : void 0
5327
+ );
5266
5328
  }
5267
5329
  }
5268
5330
  }
@@ -5323,7 +5385,7 @@ function renderEmbed2(embed, attributes, customRenderers, useLatexDelimiters = f
5323
5385
  if (h && h !== "auto") styles.push(`height: ${/^\d+$/.test(h) ? h + "px" : h}`);
5324
5386
  }
5325
5387
  const styleAttr = styles.length > 0 ? ` style="${styles.join("; ")}"` : "";
5326
- const embedSrc = toCodeWidgetEmbedUrl(src);
5388
+ const embedSrc = codeWidgetEmbedUrl ? codeWidgetEmbedUrl(src) : src;
5327
5389
  return `<iframe data-code-widget src="${escapeHtml(embedSrc)}" frameborder="0" allowfullscreen${floatAttr}${styleAttr}></iframe>`;
5328
5390
  }
5329
5391
  return `![Widget](${src})`;
@@ -6234,6 +6296,7 @@ function astToDelta(tree, customHandlers, mathBlock, mermaidBlock, plantumlBlock
6234
6296
  BOX_OVERFLOW_VALUES,
6235
6297
  BlockHandlerRegistry,
6236
6298
  BrowserDOMAdapter,
6299
+ HEADER_SIZE_PRESETS,
6237
6300
  LINE_HEIGHT_BLOCK_TAGS,
6238
6301
  NODE_TYPE,
6239
6302
  NodeDOMAdapter,
@@ -6288,6 +6351,7 @@ function astToDelta(tree, customHandlers, mathBlock, mermaidBlock, plantumlBlock
6288
6351
  getNamedColors,
6289
6352
  headerFormat,
6290
6353
  headerIdFormat,
6354
+ headingPolicyStyleParts,
6291
6355
  htmlToDelta,
6292
6356
  imageFormat,
6293
6357
  indentFormat,
@@ -6315,7 +6379,9 @@ function astToDelta(tree, customHandlers, mathBlock, mermaidBlock, plantumlBlock
6315
6379
  parseScriderMarginEm,
6316
6380
  preloadRemark,
6317
6381
  renderDelta,
6382
+ resolveCodeWidgetEmbedSrc,
6318
6383
  resolveDocumentPresentation,
6384
+ resolveHeadingPolicy,
6319
6385
  resolveTablePresentation,
6320
6386
  sanitizeDelta,
6321
6387
  sizeFormat,