@gmb/bitmark-parser-generator 5.40.0 → 5.41.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
@@ -5036,6 +5036,9 @@ var GROUPS = {
5036
5036
  key: ConfigKey.tag_item,
5037
5037
  jsonKey: "item",
5038
5038
  exportJsonKey: { item: "$" },
5039
+ // PLAN-140 W3 (NISO import): the clause/note label of a mapped
5040
+ // source element ([%1], [%ANMERKUNG 1]) — same as group_standardItemLead.
5041
+ mappingKeys: { "xml-niso-iec": { "@el": "label", "@text": "$" } },
5039
5042
  description: "The item for the bit"
5040
5043
  }
5041
5044
  ]
@@ -16661,7 +16664,7 @@ var instance2 = new Config();
16661
16664
  // src/generated/package_info.ts
16662
16665
  var PACKAGE_INFO = {
16663
16666
  "name": "@gmb/bitmark-parser-generator",
16664
- "version": "5.40.0",
16667
+ "version": "5.41.0",
16665
16668
  "author": "Get More Brain Ltd",
16666
16669
  "license": "ISC",
16667
16670
  "description": "A bitmark parser and generator using Peggy.js"
@@ -18515,6 +18518,752 @@ var AstWalkerGenerator = class {
18515
18518
  }
18516
18519
  };
18517
18520
 
18521
+ // src/generator/text/TextGrammarConstraints.ts
18522
+ var COLORS = [
18523
+ "aqua",
18524
+ "black",
18525
+ "blue",
18526
+ "brown",
18527
+ "fuchsia",
18528
+ "lightgrey",
18529
+ "gray",
18530
+ "darkgray",
18531
+ "green",
18532
+ "lime",
18533
+ "magenta",
18534
+ "maroon",
18535
+ "navy",
18536
+ "olive",
18537
+ "orange",
18538
+ "pink",
18539
+ "purple",
18540
+ "red",
18541
+ "silver",
18542
+ "teal",
18543
+ "violet",
18544
+ "white",
18545
+ "yellow"
18546
+ ];
18547
+ var HIGHLIGHT_COLORS = [
18548
+ "orange",
18549
+ "yellow",
18550
+ "green",
18551
+ "blue",
18552
+ "purple",
18553
+ "pink",
18554
+ "brown",
18555
+ "white",
18556
+ "black",
18557
+ "gray"
18558
+ ];
18559
+ var ALTERNATIVE_STYLE_TAGS = [
18560
+ "bold",
18561
+ "italic",
18562
+ "light",
18563
+ "highlightOrange",
18564
+ "highlightYellow",
18565
+ "highlightGreen",
18566
+ "highlightBlue",
18567
+ "highlightPurple",
18568
+ "highlightPink",
18569
+ "highlightBrown",
18570
+ "highlightBlack",
18571
+ "highlightWhite",
18572
+ "highlightGray",
18573
+ "highlight",
18574
+ "strike",
18575
+ "subscript",
18576
+ "superscript",
18577
+ "ins",
18578
+ "del",
18579
+ "underline",
18580
+ "doubleUnderline",
18581
+ "smallcaps",
18582
+ "circle",
18583
+ "languageEmRed",
18584
+ "languageEmOrange",
18585
+ "languageEmYellow",
18586
+ "languageEmGreen",
18587
+ "languageEmBlue",
18588
+ "languageEmPurple",
18589
+ "languageEmPink",
18590
+ "languageEmBrown",
18591
+ "languageEmBlack",
18592
+ "languageEmWhite",
18593
+ "languageEmGray",
18594
+ "languageEm",
18595
+ "userUnderline",
18596
+ "userDoubleUnderline",
18597
+ "userStrike",
18598
+ "userCircle",
18599
+ "userHighlight",
18600
+ "notranslate"
18601
+ ];
18602
+ var MEDIA_ALIGNMENTS = ["left", "center", "right"];
18603
+ var INLINE_MEDIA_ALIGNMENTS = [
18604
+ "top",
18605
+ "middle",
18606
+ "bottom",
18607
+ "baseline",
18608
+ "sub",
18609
+ "super",
18610
+ "text-bottom",
18611
+ "text-top"
18612
+ ];
18613
+ var INLINE_MEDIA_SIZES = [
18614
+ "line-height",
18615
+ "font-height",
18616
+ "super",
18617
+ "sub",
18618
+ "explicit"
18619
+ ];
18620
+ var LINE_TERMINATOR_REGEX = /[\n\r\u2028\u2029]/;
18621
+ var CHAIN_STRING_INVALID_REGEX = /[|\n\r\u2028\u2029]/;
18622
+ var URL_CHAR_CLASS = "a-zA-Z0-9!*'()=+,\\-./_?#@[\\]$&;%:{}~^";
18623
+ var URL_CHAR_REGEX = new RegExp(`^[${URL_CHAR_CLASS}]$`);
18624
+ var URL_HTTP_REGEX = new RegExp(`^https?://[${URL_CHAR_CLASS}]*$`);
18625
+ var URL_REGEX = new RegExp(`^(https?://|mailto:)[${URL_CHAR_CLASS}]*$`);
18626
+ function isChainString(v) {
18627
+ return typeof v === "string" && !CHAIN_STRING_INVALID_REGEX.test(v);
18628
+ }
18629
+ function isSingleLine(v) {
18630
+ return typeof v === "string" && !LINE_TERMINATOR_REGEX.test(v);
18631
+ }
18632
+ function hasLineTerminator(v) {
18633
+ return LINE_TERMINATOR_REGEX.test(v);
18634
+ }
18635
+ function isColor(v) {
18636
+ return typeof v === "string" && COLORS.indexOf(v) !== -1;
18637
+ }
18638
+ function isHighlightColor(v) {
18639
+ return typeof v === "string" && HIGHLIGHT_COLORS.indexOf(v) !== -1;
18640
+ }
18641
+ function isMediaAlignment(v) {
18642
+ return typeof v === "string" && MEDIA_ALIGNMENTS.indexOf(v) !== -1;
18643
+ }
18644
+ function isInlineMediaAlignment(v) {
18645
+ return typeof v === "string" && INLINE_MEDIA_ALIGNMENTS.indexOf(v) !== -1;
18646
+ }
18647
+ function isInlineMediaSize(v) {
18648
+ return typeof v === "string" && INLINE_MEDIA_SIZES.indexOf(v) !== -1;
18649
+ }
18650
+ function toUInt(v) {
18651
+ if (typeof v === "number") return Number.isInteger(v) && v >= 0 ? v : void 0;
18652
+ if (typeof v === "string" && /^[0-9]+$/.test(v)) return parseInt(v, 10);
18653
+ return void 0;
18654
+ }
18655
+ function toBoolean(v) {
18656
+ if (typeof v === "boolean") return v;
18657
+ if (v === "true") return true;
18658
+ if (v === "false") return false;
18659
+ return void 0;
18660
+ }
18661
+ function isDuration(v) {
18662
+ return isChainString(v) && v.trim().startsWith("P");
18663
+ }
18664
+ function isUrlHttp(v) {
18665
+ return typeof v === "string" && URL_HTTP_REGEX.test(v);
18666
+ }
18667
+ function isUrl(v) {
18668
+ return typeof v === "string" && URL_REGEX.test(v);
18669
+ }
18670
+ function isUrlChar(c) {
18671
+ return URL_CHAR_REGEX.test(c);
18672
+ }
18673
+ function bareUrlText(href) {
18674
+ return href.replace(/^(https?:\/\/|mailto:)/, "");
18675
+ }
18676
+ function isInlineAlt(v) {
18677
+ return typeof v === "string" && v.length > 0 && v.indexOf("==") === -1 && !v.startsWith("=") && !v.endsWith("=");
18678
+ }
18679
+ function isHeadingLevel(v) {
18680
+ return typeof v === "number" && Number.isInteger(v) && v >= 1 && v <= 3;
18681
+ }
18682
+ function normalizeInlineCodeLanguage(v) {
18683
+ if (!isChainString(v)) return void 0;
18684
+ return v.trim().toLowerCase();
18685
+ }
18686
+ function normalizeBlockCodeLanguage(v) {
18687
+ if (!isSingleLine(v)) return void 0;
18688
+ return v.trim().toLowerCase();
18689
+ }
18690
+
18691
+ // src/generator/text/TextMediaAttrs.ts
18692
+ var uint = (v) => toUInt(v);
18693
+ var bool = (v) => toBoolean(v);
18694
+ var alignment = (v) => isMediaAlignment(v) ? v : void 0;
18695
+ var chainString = (v) => isChainString(v) ? v.trim() : void 0;
18696
+ var inlineAlignment = (v) => isInlineMediaAlignment(v) ? v : void 0;
18697
+ var inlineSize = (v) => isInlineMediaSize(v) ? v : void 0;
18698
+ var VALIDATORS = {
18699
+ image: {
18700
+ alt: chainString,
18701
+ title: chainString,
18702
+ alignment,
18703
+ textAlign: alignment,
18704
+ zoomDisabled: bool,
18705
+ width: uint,
18706
+ height: uint,
18707
+ comment: chainString
18708
+ },
18709
+ imageInline: {
18710
+ srcAlt: chainString,
18711
+ alignmentVertical: inlineAlignment,
18712
+ size: inlineSize,
18713
+ width: uint,
18714
+ height: uint,
18715
+ comment: chainString
18716
+ },
18717
+ symbol: {
18718
+ alt: chainString,
18719
+ title: chainString,
18720
+ caption: chainString,
18721
+ alignment,
18722
+ textAlign: alignment,
18723
+ captionAlign: alignment,
18724
+ zoomDisabled: bool,
18725
+ width: uint,
18726
+ height: uint,
18727
+ comment: chainString
18728
+ }
18729
+ };
18730
+ var TAG_NAMES = {
18731
+ textAlign: "captionAlign",
18732
+ title: "caption"
18733
+ };
18734
+ var DEFAULTS = {
18735
+ image: { alignment: "center", textAlign: "left", zoomDisabled: true },
18736
+ imageInline: { alignmentVertical: "top", size: "line-height" },
18737
+ symbol: {}
18738
+ };
18739
+ function sanitizeMediaAttrs(kind, attrs) {
18740
+ const result = {};
18741
+ if (!attrs || typeof attrs !== "object") return result;
18742
+ const validators = VALIDATORS[kind];
18743
+ for (const [k, v] of Object.entries(attrs)) {
18744
+ const validator = validators[k];
18745
+ if (!validator || v == null) continue;
18746
+ const valid = validator(v);
18747
+ if (valid !== void 0) result[k] = valid;
18748
+ }
18749
+ if (kind === "symbol") {
18750
+ if ("caption" in result) delete result.title;
18751
+ if ("captionAlign" in result) delete result.textAlign;
18752
+ }
18753
+ return result;
18754
+ }
18755
+ function serializeMediaChain(kind, attrs) {
18756
+ const defaults = DEFAULTS[kind];
18757
+ let s = "";
18758
+ for (const k of Object.keys(VALIDATORS[kind])) {
18759
+ const v = attrs[k];
18760
+ if (v == null) continue;
18761
+ if (k in defaults && defaults[k] === v) continue;
18762
+ if (k === "comment") {
18763
+ if (v) s += `|#${v}`;
18764
+ } else if (typeof v === "string" && v === "") {
18765
+ continue;
18766
+ } else {
18767
+ s += `|${TAG_NAMES[k] ?? k}:${v}`;
18768
+ }
18769
+ }
18770
+ return s;
18771
+ }
18772
+
18773
+ // src/generator/text/TextMarkSanitizer.ts
18774
+ var STANDARD_SHORT_FORM_MARKS = [
18775
+ TextMarkType.bold,
18776
+ TextMarkType.italic,
18777
+ TextMarkType.light,
18778
+ TextMarkType.highlight
18779
+ ];
18780
+ function attrsOf(mark2) {
18781
+ const a = mark2.attrs;
18782
+ return a && typeof a === "object" && !Array.isArray(a) ? a : {};
18783
+ }
18784
+ function chainValue(v) {
18785
+ if (v == null) return "";
18786
+ return isChainString(v) ? v.trim() : void 0;
18787
+ }
18788
+ function mark(type, attrs) {
18789
+ return attrs ? { type, attrs } : { type };
18790
+ }
18791
+ function sanitizeMark(m, options) {
18792
+ const attrs = attrsOf(m);
18793
+ switch (m.type) {
18794
+ case TextMarkType.highlight:
18795
+ case TextMarkType.userHighlight: {
18796
+ const color = attrs.color;
18797
+ return isHighlightColor(color) ? mark(m.type, { color }) : mark(m.type);
18798
+ }
18799
+ case TextMarkType.textStyle:
18800
+ case TextMarkType.color: {
18801
+ const color = attrs.color;
18802
+ return isColor(color) ? mark(TextMarkType.textStyle, { color }) : void 0;
18803
+ }
18804
+ case TextMarkType.link: {
18805
+ const href = chainValue(attrs.href);
18806
+ return href === void 0 ? void 0 : mark(m.type, { href });
18807
+ }
18808
+ case TextMarkType.ref: {
18809
+ const reference = chainValue(attrs.reference);
18810
+ return reference === void 0 ? void 0 : mark(m.type, { reference });
18811
+ }
18812
+ case TextMarkType.xref: {
18813
+ const xref = chainValue(attrs.xref);
18814
+ if (xref === void 0) return void 0;
18815
+ const reference = chainValue(attrs.reference) ?? "";
18816
+ return mark(m.type, { xref, reference });
18817
+ }
18818
+ case TextMarkType.extref: {
18819
+ const extref = chainValue(attrs.extref);
18820
+ const provider = chainValue(attrs.provider);
18821
+ if (extref === void 0 || provider === void 0) return void 0;
18822
+ const refs = Array.isArray(attrs.references) ? attrs.references : [];
18823
+ const references = refs.map((r) => chainValue(r)).filter((r) => r !== void 0);
18824
+ return mark(m.type, { extref, references, provider });
18825
+ }
18826
+ case TextMarkType.footnote:
18827
+ case TextMarkType.footnoteStar: {
18828
+ const content = options.normalizeChainValue ? options.normalizeChainValue(attrs.content) : Array.isArray(attrs.content) ? attrs.content : [];
18829
+ return content === void 0 ? void 0 : mark(m.type, { content });
18830
+ }
18831
+ case TextMarkType.symbol: {
18832
+ const src = chainValue(attrs.src);
18833
+ if (src === void 0) return void 0;
18834
+ return mark(m.type, { src, ...sanitizeMediaAttrs("symbol", attrs) });
18835
+ }
18836
+ case TextMarkType.var: {
18837
+ const name = chainValue(attrs.name);
18838
+ return name === void 0 ? void 0 : mark(m.type, { name });
18839
+ }
18840
+ case TextMarkType.code: {
18841
+ const language = normalizeInlineCodeLanguage(attrs.language);
18842
+ return language === void 0 ? mark(m.type) : mark(m.type, { language });
18843
+ }
18844
+ case TextMarkType.timer: {
18845
+ const duration = attrs.duration;
18846
+ if (!isDuration(duration)) return void 0;
18847
+ const name = chainValue(attrs.name) ?? "";
18848
+ return mark(m.type, { name, duration: duration.trim() });
18849
+ }
18850
+ case TextMarkType.colorPicker: {
18851
+ const propertyRef = chainValue(attrs.propertyRef);
18852
+ return propertyRef === void 0 ? void 0 : mark(m.type, { propertyRef });
18853
+ }
18854
+ case TextMarkType.comment: {
18855
+ const comment = chainValue(m.comment);
18856
+ return comment === void 0 ? void 0 : { type: m.type, comment };
18857
+ }
18858
+ default:
18859
+ if (ALTERNATIVE_STYLE_TAGS.indexOf(m.type) !== -1) return mark(m.type);
18860
+ return void 0;
18861
+ }
18862
+ }
18863
+ function mergeLegacyTimer(marks) {
18864
+ const timer = marks.find((m) => m.type === TextMarkType.timer);
18865
+ const duration = marks.find((m) => m.type === TextMarkType.duration);
18866
+ if (!timer || !duration || attrsOf(timer).duration) return marks;
18867
+ return marks.map(
18868
+ (m) => m === timer ? { ...timer, attrs: { ...attrsOf(timer), duration: attrsOf(duration).duration } } : m
18869
+ );
18870
+ }
18871
+ function orderMarks(marks) {
18872
+ const a = (m) => m.attrs ?? {};
18873
+ const isBareHighlight = (m) => (m.type === TextMarkType.highlight || m.type === TextMarkType.userHighlight) && !a(m).color;
18874
+ const isHighlightColorStyle = (m) => m.type === TextMarkType.textStyle && isHighlightColor(a(m).color);
18875
+ const isEmptyXref = (m) => m.type === TextMarkType.xref && !a(m).reference;
18876
+ const result = [];
18877
+ for (const m of marks) {
18878
+ if (m.type === TextMarkType.ref) {
18879
+ const xrefIdx = result.findIndex(isEmptyXref);
18880
+ if (xrefIdx !== -1) {
18881
+ result.splice(xrefIdx, 0, m);
18882
+ continue;
18883
+ }
18884
+ }
18885
+ result.push(m);
18886
+ }
18887
+ for (let i = 0; i < result.length - 1; i++) {
18888
+ if (isBareHighlight(result[i]) && isHighlightColorStyle(result[i + 1])) {
18889
+ [result[i], result[i + 1]] = [result[i + 1], result[i]];
18890
+ }
18891
+ }
18892
+ const symbols = result.filter((m) => m.type === TextMarkType.symbol);
18893
+ if (symbols.length === 0) return result;
18894
+ return [...result.filter((m) => m.type !== TextMarkType.symbol), symbols[0]];
18895
+ }
18896
+ function sanitizeMarks(marks, options = {}) {
18897
+ if (!Array.isArray(marks)) return [];
18898
+ let loose = marks.filter(
18899
+ (m) => !!m && typeof m === "object" && typeof m.type === "string"
18900
+ );
18901
+ loose = mergeLegacyTimer(loose);
18902
+ const result = [];
18903
+ for (const m of loose) {
18904
+ const s = sanitizeMark(m, options);
18905
+ if (s) result.push(s);
18906
+ }
18907
+ if (options.chainValue) {
18908
+ if (result.length === 1 && STANDARD_SHORT_FORM_MARKS.indexOf(result[0].type) !== -1) {
18909
+ return [mark(result[0].type)];
18910
+ }
18911
+ if (result.length === 1 && result[0].type === TextMarkType.link) return result;
18912
+ return [];
18913
+ }
18914
+ return orderMarks(result);
18915
+ }
18916
+
18917
+ // src/generator/text/TextAstNormalizer.ts
18918
+ var LIST_TYPES = [
18919
+ TextNodeType.noBulletList,
18920
+ TextNodeType.bulletList,
18921
+ TextNodeType.orderedList,
18922
+ TextNodeType.orderedListRoman,
18923
+ TextNodeType.orderedListRomanLower,
18924
+ TextNodeType.letteredList,
18925
+ TextNodeType.letteredListLower,
18926
+ TextNodeType.taskList
18927
+ ];
18928
+ var ORDERED_LIST_TYPES = [
18929
+ TextNodeType.orderedList,
18930
+ TextNodeType.orderedListRoman,
18931
+ TextNodeType.orderedListRomanLower
18932
+ ];
18933
+ var BODY_BIT_TYPES = [
18934
+ TextNodeType.gap,
18935
+ TextNodeType.select,
18936
+ TextNodeType.highlight,
18937
+ TextNodeType.mark
18938
+ ];
18939
+ var LEADING_TAB_REGEX = /(^|[\n\r\u2028\u2029])\t+/g;
18940
+ var isList = (n) => LIST_TYPES.indexOf(n.type) !== -1;
18941
+ var isListItem = (n) => n.type === TextNodeType.listItem || n.type === TextNodeType.taskItem;
18942
+ var isBodyBit = (n) => BODY_BIT_TYPES.indexOf(n.type) !== -1;
18943
+ var isNode = (n) => !!n && typeof n === "object" && typeof n.type === "string";
18944
+ var contentOf = (n) => Array.isArray(n.content) ? n.content.filter(isNode) : [];
18945
+ var attrsOf2 = (n) => n.attrs && typeof n.attrs === "object" ? n.attrs : {};
18946
+ var asNode = (n) => n;
18947
+ var paragraph = (content) => asNode({ type: TextNodeType.paragraph, attrs: {}, content });
18948
+ var hardBreak = () => ({ type: TextNodeType.hardBreak });
18949
+ var isHardBreak = (n) => n?.type === TextNodeType.hardBreak;
18950
+ var hasText = (nodes) => nodes.some(
18951
+ (n) => n.type !== TextNodeType.hardBreak && (n.type !== TextNodeType.text || typeof n.text === "string" && n.text.trim() !== "")
18952
+ );
18953
+ function canFollowBareUrl(next) {
18954
+ if (!next) return true;
18955
+ if (next.type === TextNodeType.hardBreak) return true;
18956
+ if (next.type !== TextNodeType.text) return false;
18957
+ if (next.marks && next.marks.length > 0) return false;
18958
+ const first = (next.text ?? "").charAt(0);
18959
+ return first === "" || !isUrlChar(first);
18960
+ }
18961
+ function isSimpleLinkNode(node, next) {
18962
+ if (node.type !== TextNodeType.text || node.marks?.length !== 1) return false;
18963
+ const m = node.marks[0];
18964
+ if (m.type !== TextMarkType.link) return false;
18965
+ const href = m.attrs?.href;
18966
+ return isUrl(href) && bareUrlText(href) === node.text && canFollowBareUrl(next);
18967
+ }
18968
+ var FLATTENED_BLOCK_TYPES = [
18969
+ TextNodeType.paragraph,
18970
+ TextNodeType.heading,
18971
+ TextNodeType.section,
18972
+ TextNodeType.listItem,
18973
+ TextNodeType.taskItem,
18974
+ TextNodeType.codeBlock
18975
+ ];
18976
+ var TextAstNormalizer = class {
18977
+ constructor(options) {
18978
+ __publicField(this, "options");
18979
+ this.options = options;
18980
+ }
18981
+ normalize(ast) {
18982
+ if (!Array.isArray(ast)) return [];
18983
+ const nodes = ast.filter(isNode);
18984
+ if (this.options.chainValue) return this.inline(nodes, { plain: true, chainValue: true });
18985
+ if (this.options.location === TextLocation.tag) {
18986
+ return nodes.flatMap((n) => this.inlineRootNode(n, {}));
18987
+ }
18988
+ return nodes.flatMap((n) => this.block(n));
18989
+ }
18990
+ //
18991
+ // Block context
18992
+ //
18993
+ block(n) {
18994
+ switch (n.type) {
18995
+ case TextNodeType.paragraph:
18996
+ return [{ ...n, content: this.inline(contentOf(n)) }];
18997
+ case TextNodeType.heading:
18998
+ return this.heading(n);
18999
+ case TextNodeType.section:
19000
+ return [paragraph(this.inline(contentOf(n)))];
19001
+ case TextNodeType.image: {
19002
+ const image = this.image(n);
19003
+ return image ? [image] : [];
19004
+ }
19005
+ case TextNodeType.codeBlock:
19006
+ return [{ ...n, content: this.code(contentOf(n)) }];
19007
+ case TextNodeType.text:
19008
+ case TextNodeType.hardBreak:
19009
+ case TextNodeType.imageInline:
19010
+ case TextNodeType.latex:
19011
+ return this.inline([n]);
19012
+ default:
19013
+ if (isList(n)) return this.list(n);
19014
+ if (isListItem(n)) return [paragraph(this.inline(contentOf(n)))];
19015
+ if (isBodyBit(n)) return [n];
19016
+ if (Array.isArray(n.content))
19017
+ return [{ ...n, content: this.blocks(contentOf(n)) }];
19018
+ return [n];
19019
+ }
19020
+ }
19021
+ blocks(nodes) {
19022
+ return nodes.flatMap((n) => this.block(n));
19023
+ }
19024
+ heading(n) {
19025
+ const level = attrsOf2(n).level;
19026
+ if (level != null && !isHeadingLevel(level)) {
19027
+ return [paragraph(this.inline(contentOf(n)))];
19028
+ }
19029
+ const content = this.inline(contentOf(n), { singleLine: true });
19030
+ if (!hasText(content)) return [];
19031
+ return [{ ...n, content }];
19032
+ }
19033
+ image(n) {
19034
+ const attrs = attrsOf2(n);
19035
+ if (!isUrlHttp(attrs.src)) return void 0;
19036
+ return asNode({ ...n, attrs: { src: attrs.src, ...sanitizeMediaAttrs("image", attrs) } });
19037
+ }
19038
+ code(nodes) {
19039
+ const result = [];
19040
+ for (const n of nodes) {
19041
+ if (n.type === TextNodeType.text) {
19042
+ if (typeof n.text === "string" && n.text) {
19043
+ result.push({ type: TextNodeType.text, text: n.text });
19044
+ }
19045
+ } else if (n.type === TextNodeType.hardBreak) {
19046
+ result.push(hardBreak());
19047
+ } else {
19048
+ result.push(...this.code(contentOf(n)));
19049
+ }
19050
+ }
19051
+ return result;
19052
+ }
19053
+ //
19054
+ // List context
19055
+ //
19056
+ list(n) {
19057
+ const attrs = attrsOf2(n);
19058
+ const ordered = ORDERED_LIST_TYPES.indexOf(n.type) !== -1;
19059
+ let start;
19060
+ if (ordered && attrs.start != null) {
19061
+ start = toUInt(attrs.start);
19062
+ if (start === void 0) {
19063
+ return contentOf(n).map(
19064
+ (item) => paragraph(this.inline([item], { stripLeadingTabs: true }))
19065
+ );
19066
+ }
19067
+ }
19068
+ const items = [];
19069
+ for (const child of contentOf(n)) {
19070
+ if (isListItem(child)) {
19071
+ items.push(...this.listItem(child));
19072
+ } else {
19073
+ items.push(...this.listItem({ type: TextNodeType.listItem, content: [child] }));
19074
+ }
19075
+ }
19076
+ if (items.length === 0) return [];
19077
+ const newAttrs = { ...attrs };
19078
+ if (ordered && start !== void 0) newAttrs.start = start;
19079
+ return [asNode({ ...n, attrs: newAttrs, content: items })];
19080
+ }
19081
+ /**
19082
+ * A list item holds a paragraph, then at most one sublist. Everything else is merged into the
19083
+ * paragraph; further sublists become new items (with an empty paragraph, which keeps them
19084
+ * attached). Returns the resulting item(s).
19085
+ */
19086
+ listItem(item) {
19087
+ const inlineOpts = { stripLeadingTabs: true };
19088
+ const acc = {};
19089
+ let sublist;
19090
+ const extraItems = [];
19091
+ const merge = (inline) => {
19092
+ if (!acc.para) acc.para = inline;
19093
+ else if (inline.length > 0) acc.para.push(hardBreak(), ...inline);
19094
+ };
19095
+ for (const child of contentOf(item)) {
19096
+ if (isList(child)) {
19097
+ for (const list of this.list(child)) {
19098
+ if (list.type === TextNodeType.paragraph) {
19099
+ merge(list.content ?? []);
19100
+ } else if (!sublist) {
19101
+ sublist = list;
19102
+ } else {
19103
+ extraItems.push({ ...item, content: [paragraph([]), list] });
19104
+ }
19105
+ }
19106
+ } else if (child.type === TextNodeType.image) {
19107
+ } else {
19108
+ merge(this.inline([child], inlineOpts));
19109
+ }
19110
+ }
19111
+ const paraContent = acc.para ?? [];
19112
+ if (!hasText(paraContent) && !sublist) return extraItems;
19113
+ const content = [paragraph(paraContent)];
19114
+ if (sublist) content.push(sublist);
19115
+ return [{ ...item, content }, ...extraItems];
19116
+ }
19117
+ //
19118
+ // Inline context
19119
+ //
19120
+ /** A root node at tag location: paragraphs of inline content only */
19121
+ inlineRootNode(n, opts) {
19122
+ switch (n.type) {
19123
+ case TextNodeType.text:
19124
+ case TextNodeType.hardBreak:
19125
+ case TextNodeType.imageInline:
19126
+ case TextNodeType.latex:
19127
+ return this.inline([n], opts);
19128
+ case TextNodeType.image:
19129
+ return [];
19130
+ default:
19131
+ if (isBodyBit(n)) return [n];
19132
+ if (isList(n)) {
19133
+ return contentOf(n).map(
19134
+ (item) => paragraph(this.inline([item], { ...opts, stripLeadingTabs: true }))
19135
+ );
19136
+ }
19137
+ return [paragraph(this.inline(contentOf(n), opts))];
19138
+ }
19139
+ }
19140
+ inline(nodes, opts = {}) {
19141
+ const result = [];
19142
+ const separator = () => opts.singleLine ? this.spaceText() : hardBreak();
19143
+ const separate = () => {
19144
+ if (result.length > 0 && !isHardBreak(result[result.length - 1])) result.push(separator());
19145
+ };
19146
+ for (const n of nodes) {
19147
+ switch (n.type) {
19148
+ case TextNodeType.text: {
19149
+ const text = this.text(n, opts);
19150
+ if (text) result.push(text);
19151
+ break;
19152
+ }
19153
+ case TextNodeType.hardBreak:
19154
+ result.push(separator());
19155
+ break;
19156
+ case TextNodeType.imageInline: {
19157
+ if (opts.plain) break;
19158
+ const image = this.imageInline(n);
19159
+ if (image) result.push(image);
19160
+ break;
19161
+ }
19162
+ case TextNodeType.latex:
19163
+ if (opts.plain) break;
19164
+ if (typeof attrsOf2(n).formula === "string") result.push(n);
19165
+ break;
19166
+ case TextNodeType.image:
19167
+ break;
19168
+ default:
19169
+ if (isBodyBit(n)) {
19170
+ result.push(n);
19171
+ } else if (isList(n)) {
19172
+ for (const item of contentOf(n)) {
19173
+ separate();
19174
+ result.push(...this.inline([item], opts));
19175
+ }
19176
+ } else if (FLATTENED_BLOCK_TYPES.indexOf(n.type) !== -1) {
19177
+ separate();
19178
+ result.push(...this.inline(contentOf(n), opts));
19179
+ } else if (Array.isArray(n.content)) {
19180
+ result.push({ ...n, content: this.inline(contentOf(n), opts) });
19181
+ } else {
19182
+ result.push(n);
19183
+ }
19184
+ }
19185
+ }
19186
+ return this.dropSymbolsBeforePipes(result);
19187
+ }
19188
+ /**
19189
+ * A symbol's media chain swallows every following `...|` segment on its line (even when sealed
19190
+ * with `^`), so a symbol mark has no representable form when the rest of the line contains `|`.
19191
+ */
19192
+ dropSymbolsBeforePipes(nodes) {
19193
+ for (let i = 0; i < nodes.length; i++) {
19194
+ const n = nodes[i];
19195
+ if (!n.marks?.some((m) => m.type === TextMarkType.symbol)) continue;
19196
+ let pipeFollows = false;
19197
+ for (let j = i + 1; j < nodes.length && !isHardBreak(nodes[j]); j++) {
19198
+ const text = nodes[j].text;
19199
+ if (typeof text !== "string") continue;
19200
+ const firstLine = text.split(/[\n\r\u2028\u2029]/, 1)[0];
19201
+ if (firstLine.indexOf("|") !== -1) pipeFollows = true;
19202
+ if (hasLineTerminator(text)) break;
19203
+ }
19204
+ if (pipeFollows) {
19205
+ const marks = n.marks.filter((m) => m.type !== TextMarkType.symbol);
19206
+ const { marks: _m, ...rest } = n;
19207
+ nodes[i] = marks.length > 0 ? { ...rest, marks } : rest;
19208
+ }
19209
+ }
19210
+ return nodes;
19211
+ }
19212
+ text(n, opts) {
19213
+ if (typeof n.text !== "string") return void 0;
19214
+ let text = n.text;
19215
+ if (opts.stripLeadingTabs) text = text.replace(LEADING_TAB_REGEX, "$1");
19216
+ if (opts.singleLine) text = text.replace(/[\n\r\u2028\u2029]+/g, " ");
19217
+ if (!text) return void 0;
19218
+ const marks = sanitizeMarks(n.marks, {
19219
+ chainValue: opts.chainValue,
19220
+ normalizeChainValue: (content) => this.chainValue(content)
19221
+ });
19222
+ const { marks: _m, ...rest } = n;
19223
+ return marks.length > 0 ? { ...rest, text, marks } : { ...rest, text };
19224
+ }
19225
+ spaceText() {
19226
+ return { type: TextNodeType.text, text: " " };
19227
+ }
19228
+ imageInline(n) {
19229
+ const attrs = attrsOf2(n);
19230
+ if (!isUrl(attrs.src)) return void 0;
19231
+ const alt = attrs.alt;
19232
+ if (!isInlineAlt(alt)) return void 0;
19233
+ return asNode({
19234
+ ...n,
19235
+ attrs: { src: attrs.src, alt, ...sanitizeMediaAttrs("imageInline", attrs) }
19236
+ });
19237
+ }
19238
+ /**
19239
+ * Footnote content: a `|`-free single line of plain text with short-form standard marks.
19240
+ * Returns undefined when the content cannot be represented.
19241
+ */
19242
+ chainValue(content) {
19243
+ if (!Array.isArray(content)) return [];
19244
+ const nodes = this.inline(content.filter(isNode), {
19245
+ plain: true,
19246
+ chainValue: true,
19247
+ singleLine: true
19248
+ });
19249
+ for (let i = 0; i < nodes.length; i++) {
19250
+ const n = nodes[i];
19251
+ const text = n.text;
19252
+ if (typeof text === "string" && (text.indexOf("|") !== -1 || hasLineTerminator(text))) {
19253
+ return void 0;
19254
+ }
19255
+ if (n.marks?.some((m) => m.type === TextMarkType.link) && !isSimpleLinkNode(n, nodes[i + 1])) {
19256
+ const { marks: _m, ...rest } = n;
19257
+ nodes[i] = rest;
19258
+ }
19259
+ }
19260
+ return nodes;
19261
+ }
19262
+ };
19263
+ function normalizeTextAst(ast, options) {
19264
+ return new TextAstNormalizer(options).normalize(ast);
19265
+ }
19266
+
18518
19267
  // src/generator/text/TextGenerator.ts
18519
19268
  var DEFAULT_OPTIONS = {
18520
19269
  bodyBitCallback: void 0,
@@ -18607,21 +19356,9 @@ var INLINE_MARK_TYPES = [
18607
19356
  TextMarkType.colorPicker,
18608
19357
  TextMarkType.comment
18609
19358
  ];
18610
- var HIGHLIGHT_COLORS = [
18611
- "orange",
18612
- "yellow",
18613
- "green",
18614
- "blue",
18615
- "purple",
18616
- "pink",
18617
- "brown",
18618
- "white",
18619
- "black",
18620
- "gray"
18621
- ];
18622
19359
  var INDENTATION_REGEX = new RegExp(/(\n|\r\n)/, "g");
18623
- var LINK_REGEX = new RegExp(/https?:\/\/|mailto:(.*)/, "g");
18624
19360
  var LINK_BREAKSCAPE_REGEX = new RegExp(/\]/, "g");
19361
+ var CHAIN_CONTINUATION_REGEX = /^[^\n\r\u2028\u2029]*\|/;
18625
19362
  var Stage = {
18626
19363
  enter: "enter",
18627
19364
  between: "between",
@@ -18658,6 +19395,8 @@ var TextGenerator = class _TextGenerator extends AstWalkerGenerator {
18658
19395
  __publicField(this, "textDepth", 0);
18659
19396
  __publicField(this, "placeholderIndex", 0);
18660
19397
  __publicField(this, "placeholders", {});
19398
+ __publicField(this, "chainJustClosed", false);
19399
+ __publicField(this, "simpleLinkAllowed", true);
18661
19400
  // For pre-text
18662
19401
  __publicField(this, "rootParagraphNodeContentIndex", 0);
18663
19402
  __publicField(this, "previousRootParagraphContextType", TextNodeType.text);
@@ -18703,6 +19442,11 @@ var TextGenerator = class _TextGenerator extends AstWalkerGenerator {
18703
19442
  generateSync(ast, textFormat, textLocation, options) {
18704
19443
  if (!ast || !Array.isArray(ast)) return "";
18705
19444
  this.generateOptions = Object.assign({}, options);
19445
+ ast = normalizeTextAst(ast, {
19446
+ format: textFormat ?? TextFormat.bitmarkText,
19447
+ location: textLocation,
19448
+ chainValue: this.generateOptions.chainValueContext
19449
+ });
18706
19450
  this.validateGenerateOptions(ast);
18707
19451
  if (!this.generateOptions.plainTextDividerAllowed) {
18708
19452
  this.resetState(textFormat, textLocation);
@@ -18746,6 +19490,8 @@ var TextGenerator = class _TextGenerator extends AstWalkerGenerator {
18746
19490
  this.textDepth = 0;
18747
19491
  this.placeholderIndex = 0;
18748
19492
  this.placeholders = {};
19493
+ this.chainJustClosed = false;
19494
+ this.simpleLinkAllowed = true;
18749
19495
  this.rootParagraphNodeContentIndex = 0;
18750
19496
  this.previousRootParagraphContextType = TextNodeType.hardBreak;
18751
19497
  this.inPreText = false;
@@ -18811,6 +19557,7 @@ var TextGenerator = class _TextGenerator extends AstWalkerGenerator {
18811
19557
  this.writeHardBreak(node);
18812
19558
  break;
18813
19559
  case TextNodeType.text: {
19560
+ this.simpleLinkAllowed = this.isSimpleLinkAllowedAfter(route);
18814
19561
  this.writeMarks(node, Stage.enter);
18815
19562
  this.writeText(node);
18816
19563
  this.writeMarks(node, Stage.between);
@@ -18824,9 +19571,6 @@ var TextGenerator = class _TextGenerator extends AstWalkerGenerator {
18824
19571
  this.writeHeading(node);
18825
19572
  this.inHeading = true;
18826
19573
  break;
18827
- case TextNodeType.section:
18828
- this.writeSection(node);
18829
- break;
18830
19574
  case TextNodeType.listItem:
18831
19575
  case TextNodeType.taskItem:
18832
19576
  this.writeBullet(node, route);
@@ -18884,7 +19628,6 @@ var TextGenerator = class _TextGenerator extends AstWalkerGenerator {
18884
19628
  break;
18885
19629
  case TextNodeType.heading:
18886
19630
  this.inHeading = false;
18887
- case TextNodeType.section:
18888
19631
  case TextNodeType.image:
18889
19632
  if (!this.inParagraph) {
18890
19633
  this.writeNL();
@@ -18996,9 +19739,9 @@ var TextGenerator = class _TextGenerator extends AstWalkerGenerator {
18996
19739
  */
18997
19740
  getLinkHref(node) {
18998
19741
  if (node.type === TextNodeType.text && node.marks) {
18999
- const href = node.marks.reduce((acc, mark) => {
19000
- if (mark.type === TextMarkType.link) {
19001
- const linkMark = mark;
19742
+ const href = node.marks.reduce((acc, mark2) => {
19743
+ if (mark2.type === TextMarkType.link) {
19744
+ const linkMark = mark2;
19002
19745
  const href2 = linkMark.attrs?.href;
19003
19746
  if (href2) return href2;
19004
19747
  }
@@ -19063,6 +19806,9 @@ var TextGenerator = class _TextGenerator extends AstWalkerGenerator {
19063
19806
  const indentationString = this.getIndentationString();
19064
19807
  s = s.replace(INDENTATION_REGEX, `$1${indentationString}`);
19065
19808
  }
19809
+ if (this.chainJustClosed && CHAIN_CONTINUATION_REGEX.test(s)) {
19810
+ s = `^${s}`;
19811
+ }
19066
19812
  if (this.havePreText && this.rootParagraphNodeContentIndex === this.preTextIndex) {
19067
19813
  this.writePlainTextDivider();
19068
19814
  this.writeNL();
@@ -19092,14 +19838,14 @@ var TextGenerator = class _TextGenerator extends AstWalkerGenerator {
19092
19838
  * @returns true if a simple link, otherwise false
19093
19839
  */
19094
19840
  isSimpleLink(node) {
19095
- if (node.text == null) return false;
19096
- if (node.marks?.length !== 1) return false;
19097
- const href = this.getLinkHref(node);
19098
- if (href) {
19099
- const hrefText = href.replace(LINK_REGEX, "$1");
19100
- return hrefText === node.text;
19101
- }
19102
- return false;
19841
+ return this.simpleLinkAllowed && isSimpleLinkNode(node, void 0);
19842
+ }
19843
+ /**
19844
+ * The bare-URL link short form is only safe when whatever is written next cannot be read as
19845
+ * part of the URL (the parser's Url rule consumes every following UrlChar).
19846
+ */
19847
+ isSimpleLinkAllowedAfter(route) {
19848
+ return canFollowBareUrl(this.getSiblingNodes(route).right);
19103
19849
  }
19104
19850
  /**
19105
19851
  * Get the link text if the node is a link, otherwise return false
@@ -19111,15 +19857,8 @@ var TextGenerator = class _TextGenerator extends AstWalkerGenerator {
19111
19857
  if (node.text == null) return false;
19112
19858
  const href = this.getLinkHref(node);
19113
19859
  if (href) {
19114
- let res;
19115
- const hrefText = href.replace(LINK_REGEX, "$1");
19116
- if (hrefText === node.text) {
19117
- res = href;
19118
- } else {
19119
- res = node.text;
19120
- }
19121
- res = (res ?? "").replace(LINK_BREAKSCAPE_REGEX, "^]");
19122
- return res;
19860
+ const res = this.isSimpleLink(node) ? href : node.text;
19861
+ return res.replace(LINK_BREAKSCAPE_REGEX, "^]");
19123
19862
  }
19124
19863
  return false;
19125
19864
  }
@@ -19176,22 +19915,17 @@ var TextGenerator = class _TextGenerator extends AstWalkerGenerator {
19176
19915
  writeMarks(node, stage) {
19177
19916
  if (node.marks) {
19178
19917
  const forceSingleMark = this.generateOptions.forceInline || !!(this.inInline || this.inHeading);
19179
- this.thisNodeIsPreText = false;
19180
- const emptyMarks = node.marks.length === 0;
19181
- if (emptyMarks) {
19182
- this.writeMarkTextWrapper(INLINE_MARK);
19183
- return;
19184
- }
19185
- const marks = this.getWritableMarks(node.marks);
19918
+ const marks = node.marks;
19186
19919
  if (marks.length === 0) return;
19920
+ this.thisNodeIsPreText = false;
19187
19921
  const singleMark = marks.length === 1 && forceSingleMark;
19188
19922
  const markStartEnd = marks.reduce(
19189
- (acc, mark) => {
19923
+ (acc, mark2) => {
19190
19924
  if (acc) return acc;
19191
- if (this.isStandardMark(mark)) {
19192
- if (singleMark) return STANDARD_MARKS[mark.type];
19925
+ if (this.isStandardMark(mark2)) {
19926
+ if (singleMark) return STANDARD_MARKS[mark2.type];
19193
19927
  return INLINE_MARK;
19194
- } else if (this.isInlineMark(mark)) {
19928
+ } else if (this.isInlineMark(mark2)) {
19195
19929
  return INLINE_MARK;
19196
19930
  } else {
19197
19931
  }
@@ -19206,56 +19940,59 @@ var TextGenerator = class _TextGenerator extends AstWalkerGenerator {
19206
19940
  this.writeMarkTextWrapper(markStartEnd);
19207
19941
  }
19208
19942
  if (stage == Stage.between) {
19209
- for (const mark of marks) {
19210
- if (this.isStandardMark(mark)) {
19943
+ for (const mark2 of marks) {
19944
+ if (this.isStandardMark(mark2)) {
19211
19945
  if (!singleMark) {
19212
19946
  this.writeInlineMarkStartEnd();
19213
- this.writeInlineMark(mark);
19947
+ this.writeInlineMark(mark2);
19214
19948
  }
19215
- } else if (TextMarkType.comment === mark.type) {
19949
+ } else if (TextMarkType.comment === mark2.type) {
19216
19950
  this.writeInlineMarkStartEnd();
19217
- this.writeCommentMark(mark);
19218
- } else if (TextMarkType.link === mark.type) {
19951
+ this.writeCommentMark(mark2);
19952
+ } else if (TextMarkType.link === mark2.type) {
19219
19953
  this.writeInlineMarkStartEnd();
19220
- this.writeLinkMark(mark);
19221
- } else if (TextMarkType.ref === mark.type) {
19954
+ this.writeLinkMark(mark2);
19955
+ } else if (TextMarkType.ref === mark2.type) {
19222
19956
  this.writeInlineMarkStartEnd();
19223
- this.writeRefMark(mark);
19224
- } else if (TextMarkType.xref === mark.type) {
19957
+ this.writeRefMark(mark2);
19958
+ } else if (TextMarkType.xref === mark2.type) {
19225
19959
  this.writeInlineMarkStartEnd();
19226
- this.writeXRefMark(mark);
19227
- } else if (TextMarkType.extref === mark.type) {
19960
+ this.writeXRefMark(mark2);
19961
+ } else if (TextMarkType.extref === mark2.type) {
19228
19962
  this.writeInlineMarkStartEnd();
19229
- this.writeExtRefMark(mark);
19230
- } else if (TextMarkType.footnote === mark.type) {
19963
+ this.writeExtRefMark(mark2);
19964
+ } else if (TextMarkType.footnote === mark2.type) {
19231
19965
  this.writeInlineMarkStartEnd();
19232
- this.writeFootnoteMark(mark);
19233
- } else if (TextMarkType.footnoteStar === mark.type) {
19966
+ this.writeFootnoteMark(mark2);
19967
+ } else if (TextMarkType.footnoteStar === mark2.type) {
19234
19968
  this.writeInlineMarkStartEnd();
19235
- this.writeFootnoteStarMark(mark);
19236
- } else if (TextMarkType.symbol === mark.type) {
19969
+ this.writeFootnoteStarMark(mark2);
19970
+ } else if (TextMarkType.symbol === mark2.type) {
19237
19971
  this.writeInlineMarkStartEnd();
19238
- this.writeSymbolMark(mark);
19239
- } else if (this.isInlineMark(mark)) {
19972
+ this.writeSymbolMark(mark2);
19973
+ } else if (this.isInlineMark(mark2)) {
19240
19974
  this.writeInlineMarkStartEnd();
19241
- this.writeInlineMark(mark);
19975
+ this.writeInlineMark(mark2);
19242
19976
  } else {
19243
19977
  }
19244
19978
  }
19245
19979
  }
19246
19980
  if (stage == Stage.exit) {
19247
19981
  let inlineMarkWritten = false;
19248
- for (const mark of marks) {
19249
- if (this.isStandardMark(mark)) {
19982
+ for (const mark2 of marks) {
19983
+ if (this.isStandardMark(mark2)) {
19250
19984
  if (!singleMark) {
19251
19985
  inlineMarkWritten = true;
19252
19986
  }
19253
- } else if (TextMarkType.comment === mark.type || TextMarkType.link === mark.type || TextMarkType.ref === mark.type || TextMarkType.xref === mark.type || TextMarkType.footnote === mark.type || TextMarkType.footnoteStar === mark.type || this.isInlineMark(mark)) {
19987
+ } else if (TextMarkType.comment === mark2.type || TextMarkType.link === mark2.type || TextMarkType.ref === mark2.type || TextMarkType.xref === mark2.type || TextMarkType.footnote === mark2.type || TextMarkType.footnoteStar === mark2.type || this.isInlineMark(mark2)) {
19254
19988
  inlineMarkWritten = true;
19255
19989
  } else {
19256
19990
  }
19257
19991
  }
19258
- if (inlineMarkWritten) this.writeInlineMarkStartEnd();
19992
+ if (inlineMarkWritten) {
19993
+ this.writeInlineMarkStartEnd();
19994
+ this.chainJustClosed = true;
19995
+ }
19259
19996
  }
19260
19997
  }
19261
19998
  }
@@ -19291,15 +20028,6 @@ var TextGenerator = class _TextGenerator extends AstWalkerGenerator {
19291
20028
  s += " ";
19292
20029
  this.write(s);
19293
20030
  }
19294
- writeSection(node) {
19295
- let s = "";
19296
- if (node.section) {
19297
- s = `|${node.section}: `;
19298
- } else {
19299
- s = "|";
19300
- }
19301
- this.write(s);
19302
- }
19303
20031
  writeBullet(node, route) {
19304
20032
  let bullet = this.getIndentationString();
19305
20033
  const listParent = this.getParentNode(route, 2);
@@ -19322,7 +20050,7 @@ var TextGenerator = class _TextGenerator extends AstWalkerGenerator {
19322
20050
  bullet += "\u2022a ";
19323
20051
  } else if (listType === TextNodeType.taskList) {
19324
20052
  const taskList = node;
19325
- const checked = taskList.attrs?.checked ?? false;
20053
+ const checked = taskList.attrs?.checked === true;
19326
20054
  bullet += checked ? "\u2022+ " : "\u2022- ";
19327
20055
  }
19328
20056
  if (bullet) this.write(bullet);
@@ -19331,28 +20059,20 @@ var TextGenerator = class _TextGenerator extends AstWalkerGenerator {
19331
20059
  if (node.attrs == null || !node.attrs.src) return;
19332
20060
  const attrs = node.attrs;
19333
20061
  const inlineImage = node.type === TextNodeType.imageInline;
19334
- let ignoreAttributes;
19335
- if (inlineImage) {
19336
- ignoreAttributes = /* @__PURE__ */ new Set(["alt", "zoomDisabled", "title"]);
19337
- const a = attrs;
19338
- if (a.alignmentVertical === "top") ignoreAttributes.add("alignmentVertical");
19339
- if (a.size === "line-height") ignoreAttributes.add("size");
19340
- }
19341
- const mediaAttrs = this.getMediaAttrs(inlineImage ? "imageInline" : "image", attrs, {
19342
- ignoreAttributes
19343
- });
20062
+ const kind = inlineImage ? "imageInline" : "image";
20063
+ const chain = serializeMediaChain(kind, attrs);
19344
20064
  let s = "";
19345
20065
  if (inlineImage) {
19346
20066
  s = `==${attrs.alt ?? ""}==`;
19347
20067
  }
19348
- s += mediaAttrs ? `|${mediaAttrs}|` : "";
20068
+ s += `|${kind}:${attrs.src}${chain}|`;
19349
20069
  this.write(s);
19350
20070
  }
19351
20071
  writeCodeBlock(node) {
19352
- if (node.attrs == null || !node.attrs.language) return;
19353
- const attrs = node.attrs;
19354
- const s = `|code:${attrs.language}
19355
- `;
20072
+ const raw = node.attrs?.language ?? node.language;
20073
+ const language = normalizeBlockCodeLanguage(raw) ?? "";
20074
+ const s = language ? `|code:${language}
20075
+ ` : "|code\n";
19356
20076
  this.write(s);
19357
20077
  }
19358
20078
  writeLatex(node) {
@@ -19368,10 +20088,10 @@ var TextGenerator = class _TextGenerator extends AstWalkerGenerator {
19368
20088
  writeMarkTextWrapper(s) {
19369
20089
  if (s) this.write(s);
19370
20090
  }
19371
- writeInlineMark(mark) {
19372
- const attrs = mark.attrs ?? {};
20091
+ writeInlineMark(mark2) {
20092
+ const attrs = mark2.attrs ?? {};
19373
20093
  let s;
19374
- switch (mark.type) {
20094
+ switch (mark2.type) {
19375
20095
  case TextMarkType.textStyle:
19376
20096
  case TextMarkType.color:
19377
20097
  s = `color:${attrs.color ?? ""}`;
@@ -19382,10 +20102,10 @@ var TextGenerator = class _TextGenerator extends AstWalkerGenerator {
19382
20102
  break;
19383
20103
  case TextMarkType.highlight:
19384
20104
  case TextMarkType.userHighlight:
19385
- s = attrs.color ? `${mark.type}|color:${attrs.color}` : `${mark.type}`;
20105
+ s = attrs.color ? `${mark2.type}|color:${attrs.color}` : `${mark2.type}`;
19386
20106
  break;
19387
20107
  default: {
19388
- s = `${mark.type}`;
20108
+ s = `${mark2.type}`;
19389
20109
  for (const [k, v] of Object.entries(attrs)) {
19390
20110
  if (k === "language" && v !== "plain text" || k === "propertyRef" || k === "name") {
19391
20111
  s = `${s}:${v}`;
@@ -19395,88 +20115,49 @@ var TextGenerator = class _TextGenerator extends AstWalkerGenerator {
19395
20115
  }
19396
20116
  this.write(s);
19397
20117
  }
19398
- /**
19399
- * Resolve the marks of a text node into the marks that will actually be written.
19400
- *
19401
- * - Merges a legacy standalone 'duration' mark into its sibling 'timer' mark.
19402
- * - Drops marks the current grammar cannot express (standalone 'duration', 'timer'
19403
- * without a duration).
19404
- * - Moves a 'textStyle'/'color' mark before a preceding bare 'highlight'/'userHighlight'
19405
- * mark when its color is a valid highlight color; written in the original order the
19406
- * output would re-parse as a highlight-with-color compound mark and change meaning.
19407
- */
19408
- getWritableMarks(nodeMarks) {
19409
- const attrsOf = (m) => m.attrs ?? {};
19410
- let marks = [...nodeMarks];
19411
- const timer = marks.find((m) => m.type === TextMarkType.timer);
19412
- const duration = marks.find((m) => m.type === TextMarkType.duration);
19413
- if (timer && duration && !attrsOf(timer).duration) {
19414
- marks = marks.map(
19415
- (m) => m === timer ? {
19416
- ...timer,
19417
- attrs: { ...attrsOf(timer), duration: attrsOf(duration).duration }
19418
- } : m
19419
- );
19420
- }
19421
- marks = marks.filter((m) => {
19422
- if (m.type === TextMarkType.duration) return false;
19423
- if (m.type === TextMarkType.timer) return !!attrsOf(m).duration;
19424
- return true;
19425
- });
19426
- const isBareHighlight = (m) => (m.type === TextMarkType.highlight || m.type === TextMarkType.userHighlight) && !attrsOf(m).color;
19427
- const isHighlightColorStyle = (m) => (m.type === TextMarkType.textStyle || m.type === TextMarkType.color) && HIGHLIGHT_COLORS.indexOf(attrsOf(m).color) !== -1;
19428
- for (let i = 0; i < marks.length - 1; i++) {
19429
- if (isBareHighlight(marks[i]) && isHighlightColorStyle(marks[i + 1])) {
19430
- const tmp = marks[i];
19431
- marks[i] = marks[i + 1];
19432
- marks[i + 1] = tmp;
19433
- }
19434
- }
19435
- return marks;
19436
- }
19437
20118
  /**
19438
20119
  * True if the mark is written using its standard form (e.g. **bold**, !!highlight!!)
19439
20120
  * when it is the only mark. A highlight mark with a color attribute is excluded - it
19440
20121
  * can only be expressed using the inline chain form (==text==|highlight|color:x|).
19441
20122
  */
19442
- isStandardMark(mark) {
19443
- if (STANDARD_MARK_TYPES.indexOf(mark.type) === -1) return false;
19444
- return !(mark.attrs && mark.attrs.color);
20123
+ isStandardMark(mark2) {
20124
+ if (STANDARD_MARK_TYPES.indexOf(mark2.type) === -1) return false;
20125
+ return !(mark2.attrs && mark2.attrs.color);
19445
20126
  }
19446
20127
  /**
19447
20128
  * True if the mark is written using the inline chain form (==text==|mark|).
19448
20129
  */
19449
- isInlineMark(mark) {
19450
- if (INLINE_MARK_TYPES.indexOf(mark.type) !== -1) return true;
19451
- return mark.type === TextMarkType.highlight && !!(mark.attrs && mark.attrs.color);
20130
+ isInlineMark(mark2) {
20131
+ if (INLINE_MARK_TYPES.indexOf(mark2.type) !== -1) return true;
20132
+ return mark2.type === TextMarkType.highlight && !!(mark2.attrs && mark2.attrs.color);
19452
20133
  }
19453
- writeCommentMark(mark) {
19454
- const comment = mark.comment || "";
20134
+ writeCommentMark(mark2) {
20135
+ const comment = mark2.comment || "";
19455
20136
  const s = `#${comment}`;
19456
20137
  this.write(s);
19457
20138
  }
19458
- writeLinkMark(mark) {
19459
- const href = mark.attrs?.href || "";
20139
+ writeLinkMark(mark2) {
20140
+ const href = mark2.attrs?.href || "";
19460
20141
  const linkText = (href ?? "").replace(LINK_BREAKSCAPE_REGEX, "^]");
19461
20142
  const s = `link:${linkText}`;
19462
20143
  this.write(s);
19463
20144
  }
19464
- writeRefMark(mark) {
19465
- const ref = mark.attrs?.reference ?? "";
20145
+ writeRefMark(mark2) {
20146
+ const ref = mark2.attrs?.reference ?? "";
19466
20147
  const s = `\u25BA${ref}`;
19467
20148
  this.write(s);
19468
20149
  }
19469
- writeXRefMark(mark) {
19470
- const xref = mark.attrs?.xref ?? "";
19471
- const ref = mark.attrs?.reference ?? "";
20150
+ writeXRefMark(mark2) {
20151
+ const xref = mark2.attrs?.xref ?? "";
20152
+ const ref = mark2.attrs?.reference ?? "";
19472
20153
  let s = `xref:${xref}`;
19473
20154
  if (ref) s += `|\u25BA${ref}`;
19474
20155
  this.write(s);
19475
20156
  }
19476
- writeExtRefMark(mark) {
19477
- const extref = mark.attrs?.extref ?? "";
19478
- const refs = mark.attrs?.references ?? [];
19479
- const provider = mark.attrs?.provider ?? "";
20157
+ writeExtRefMark(mark2) {
20158
+ const extref = mark2.attrs?.extref ?? "";
20159
+ const refs = mark2.attrs?.references ?? [];
20160
+ const provider = mark2.attrs?.provider ?? "";
19480
20161
  let s = `extref:${extref}`;
19481
20162
  for (const ref of refs) {
19482
20163
  s += `|\u25BA${ref ?? ""}`;
@@ -19484,39 +20165,39 @@ var TextGenerator = class _TextGenerator extends AstWalkerGenerator {
19484
20165
  s += `|provider:${provider}`;
19485
20166
  this.write(s);
19486
20167
  }
19487
- writeFootnoteMark(mark) {
20168
+ writeFootnoteMark(mark2) {
19488
20169
  const s = `footnote:`;
19489
20170
  this.write(s);
19490
20171
  const text = this.internalTextGenerator?.generateSync(
19491
- mark.attrs?.content,
20172
+ mark2.attrs?.content,
19492
20173
  this.textFormat,
19493
20174
  this.textLocation,
19494
20175
  {
19495
20176
  ...this.generateOptions,
19496
- forceInline: true
20177
+ forceInline: true,
20178
+ chainValueContext: true
19497
20179
  }
19498
20180
  ) ?? "";
19499
20181
  this.write(text);
19500
20182
  }
19501
- writeFootnoteStarMark(mark) {
20183
+ writeFootnoteStarMark(mark2) {
19502
20184
  const s = `footnote*:`;
19503
20185
  this.write(s);
19504
20186
  const text = this.internalTextGenerator?.generateSync(
19505
- mark.attrs?.content,
20187
+ mark2.attrs?.content,
19506
20188
  this.textFormat,
19507
20189
  this.textLocation,
19508
20190
  {
19509
20191
  ...this.generateOptions,
19510
- forceInline: true
20192
+ forceInline: true,
20193
+ chainValueContext: true
19511
20194
  }
19512
20195
  ) ?? "";
19513
20196
  this.write(text);
19514
20197
  }
19515
- writeSymbolMark(mark) {
19516
- if (mark.attrs == null) return;
19517
- const attrs = mark.attrs;
19518
- const mediaAttrs = this.getMediaAttrs("symbol", attrs);
19519
- const s = mediaAttrs ?? "";
20198
+ writeSymbolMark(mark2) {
20199
+ const attrs = mark2.attrs ?? {};
20200
+ const s = `symbol:${attrs.src ?? ""}${serializeMediaChain("symbol", attrs)}`;
19520
20201
  this.write(s);
19521
20202
  }
19522
20203
  writeInlineMarkStartEnd() {
@@ -19546,52 +20227,6 @@ var TextGenerator = class _TextGenerator extends AstWalkerGenerator {
19546
20227
  }
19547
20228
  this.writeString(tag);
19548
20229
  }
19549
- getMediaAttrs(mediaType, attrs, options) {
19550
- if (!mediaType) return void 0;
19551
- const opts = Object.assign({}, options);
19552
- const ignoreAttributes = opts.ignoreAttributes ?? /* @__PURE__ */ new Set();
19553
- let s = `${mediaType}:${attrs?.src ?? ""}`;
19554
- const entries = Object.entries(attrs).filter(([k, _v]) => k !== "src");
19555
- for (let i = 0; i < entries.length; i++) {
19556
- const [k, v] = entries[i];
19557
- if (ignoreAttributes.has(k)) continue;
19558
- switch (k) {
19559
- case "textAlign":
19560
- if (v !== "left") s += `|captionAlign:${v}`;
19561
- break;
19562
- case "alignment":
19563
- if (v !== "center") {
19564
- if (v) s += `|alignment:${v}`;
19565
- }
19566
- break;
19567
- case "title":
19568
- if (v) s += `|caption:${v}`;
19569
- break;
19570
- case "class":
19571
- if (v !== "center") {
19572
- if (v) s += `|align:${v}`;
19573
- }
19574
- break;
19575
- case "comment":
19576
- if (v) s += `|#${v}`;
19577
- break;
19578
- case "":
19579
- if (stringUtils.isString(v)) s += `|:${v}`;
19580
- else s += `|`;
19581
- break;
19582
- case "zoomDisabled":
19583
- if (!v) s += "|zoomDisabled:false";
19584
- break;
19585
- case "alt":
19586
- case "width":
19587
- case "height":
19588
- default:
19589
- if (k && v) s += `|${k}:${v}`;
19590
- break;
19591
- }
19592
- }
19593
- return s;
19594
- }
19595
20230
  //
19596
20231
  // Helper functions
19597
20232
  //
@@ -19617,6 +20252,7 @@ var TextGenerator = class _TextGenerator extends AstWalkerGenerator {
19617
20252
  */
19618
20253
  write(value) {
19619
20254
  value = this.getInterTextBreakscape(value) + value;
20255
+ this.chainJustClosed = false;
19620
20256
  if (this.options.writeCallback) {
19621
20257
  this.options.writeCallback(value);
19622
20258
  }
@@ -33802,8 +34438,8 @@ var Builder = class extends BaseBuilder {
33802
34438
  break;
33803
34439
  }
33804
34440
  case BodyBitType.mark: {
33805
- const mark = part;
33806
- instance7.fillBooleanExample([mark], __isDefaultExample, example, false);
34441
+ const mark2 = part;
34442
+ instance7.fillBooleanExample([mark2], __isDefaultExample, example, false);
33807
34443
  break;
33808
34444
  }
33809
34445
  case BodyBitType.select: {
@@ -34584,10 +35220,10 @@ var BitmarkGenerator = class extends AstWalkerGenerator {
34584
35220
  const markConfig = node.value;
34585
35221
  const parent = this.getParentNode(route);
34586
35222
  if (parent?.key !== NodeType.markConfig) return true;
34587
- const { mark, color, emphasis } = markConfig;
34588
- if (mark) {
35223
+ const { mark: mark2, color, emphasis } = markConfig;
35224
+ if (mark2) {
34589
35225
  this.writeNL();
34590
- this.writeProperty("mark", mark, route, {
35226
+ this.writeProperty("mark", mark2, route, {
34591
35227
  format: TagFormat.plainText
34592
35228
  });
34593
35229
  if (color) {
@@ -34804,9 +35440,9 @@ var BitmarkGenerator = class extends AstWalkerGenerator {
34804
35440
  }
34805
35441
  // bodyBit -> mark
34806
35442
  enter_mark(node, _route) {
34807
- const mark = node.value;
35443
+ const mark2 = node.value;
34808
35444
  this.writeOPE();
34809
- this.writeTextOrValue(mark.solution, TextFormat.plainText, TextLocation.tag);
35445
+ this.writeTextOrValue(mark2.solution, TextFormat.plainText, TextLocation.tag);
34810
35446
  this.writeCL();
34811
35447
  return true;
34812
35448
  }
@@ -34866,9 +35502,9 @@ var BitmarkGenerator = class extends AstWalkerGenerator {
34866
35502
  leaf_mark(node, route) {
34867
35503
  const root = route[0];
34868
35504
  if (root?.key !== NodeType.mark) return;
34869
- const mark = node.value;
34870
- if (mark) {
34871
- this.writeProperty("mark", mark, route, {
35505
+ const mark2 = node.value;
35506
+ if (mark2) {
35507
+ this.writeProperty("mark", mark2, route, {
34872
35508
  format: TagFormat.plainText
34873
35509
  });
34874
35510
  }
@@ -36625,8 +37261,6 @@ var MARK_TO_TAG = {
36625
37261
  highlight: "mark",
36626
37262
  code: "code"
36627
37263
  };
36628
- var INLINE_IMAGE_RE = /==([^=]*(?:=(?!=)[^=]*)*)==\|imageInline:([^|\s]+)((?:\|(?:@?[A-Za-z][A-Za-z0-9_-]*:[^|\s]+|#[^|\s]*))*)\|?/g;
36629
- var MAX_IMAGE_DIMENSION = 9999;
36630
37264
  var HtmlTableGenerator = class {
36631
37265
  constructor() {
36632
37266
  __publicField(this, "indentUnit", " ");
@@ -36768,13 +37402,13 @@ var HtmlTableGenerator = class {
36768
37402
  if (!marks || marks.length === 0) return text;
36769
37403
  let open = "";
36770
37404
  let close = "";
36771
- for (const mark of marks) {
36772
- if (mark.type === "link") {
36773
- const href = mark.attrs?.href ?? "";
37405
+ for (const mark2 of marks) {
37406
+ if (mark2.type === "link") {
37407
+ const href = mark2.attrs?.href ?? "";
36774
37408
  open += `<a href="${this.escapeAttr(href)}">`;
36775
37409
  close = `</a>${close}`;
36776
37410
  } else {
36777
- const tag = MARK_TO_TAG[mark.type];
37411
+ const tag = MARK_TO_TAG[mark2.type];
36778
37412
  if (tag) {
36779
37413
  open += `<${tag}>`;
36780
37414
  close = `</${tag}>${close}`;
@@ -36838,37 +37472,7 @@ var HtmlTableGenerator = class {
36838
37472
  return `<img ${parts.join(" ")}>`;
36839
37473
  }
36840
37474
  textToHtml(text) {
36841
- let html = "";
36842
- let lastIndex = 0;
36843
- for (const match of text.matchAll(INLINE_IMAGE_RE)) {
36844
- const index = match.index ?? 0;
36845
- html += this.escapeText(text.slice(lastIndex, index));
36846
- html += this.inlineImageTokenToHtml(match);
36847
- lastIndex = index + match[0].length;
36848
- }
36849
- html += this.escapeText(text.slice(lastIndex));
36850
- return html;
36851
- }
36852
- inlineImageTokenToHtml(match) {
36853
- const attrs = {
36854
- alt: match[1],
36855
- src: match[2]
36856
- };
36857
- const rawAttrs = match[3] ?? "";
36858
- for (const attr of rawAttrs.split("|")) {
36859
- if (!attr || attr.startsWith("#")) continue;
36860
- const colon = attr.indexOf(":");
36861
- if (colon < 1) continue;
36862
- const key = attr.slice(0, colon).replace(/^@/, "");
36863
- const value = attr.slice(colon + 1);
36864
- if (key === "width" || key === "height") {
36865
- const n = Number.parseInt(value, 10);
36866
- if (!Number.isNaN(n) && n > 0 && n <= MAX_IMAGE_DIMENSION) attrs[key] = n;
36867
- } else if (key === "class" || key === "title") {
36868
- attrs[key] = value;
36869
- }
36870
- }
36871
- return this.imageToHtml({ type: "imageInline", attrs });
37475
+ return this.escapeText(text);
36872
37476
  }
36873
37477
  // --- formulas -------------------------------------------------------------
36874
37478
  latexToHtml(node) {
@@ -41370,13 +41974,13 @@ function markChainContentProcessor(context, contentDepth, tagsConfig, content, t
41370
41974
  if (contentDepth === ContentDepth.Chain) {
41371
41975
  markTagContentProcessor(context, ContentDepth.Chain, content, target);
41372
41976
  } else {
41373
- const mark = buildMark(
41977
+ const mark2 = buildMark(
41374
41978
  context,
41375
41979
  contentDepth,
41376
41980
  tagsConfig,
41377
41981
  content
41378
41982
  );
41379
- if (mark) bodyParts.push(mark);
41983
+ if (mark2) bodyParts.push(mark2);
41380
41984
  }
41381
41985
  }
41382
41986
  function buildMark(context, _contentDepth, tagsConfig, content) {
@@ -41391,13 +41995,13 @@ function buildMark(context, _contentDepth, tagsConfig, content) {
41391
41995
  if (context.DEBUG_CHAIN_TAGS) context.debugPrint("mark TAGS", chainTags);
41392
41996
  const { solution } = tags2;
41393
41997
  const { mark: markType, ...rest } = chainTags;
41394
- const mark = {
41998
+ const mark2 = {
41395
41999
  type: BodyBitType.mark,
41396
42000
  solution: solution ?? "",
41397
42001
  mark: instance4.asSingle(markType) ?? "",
41398
42002
  ...rest
41399
42003
  };
41400
- return mark;
42004
+ return mark2;
41401
42005
  }
41402
42006
 
41403
42007
  // src/parser/bitmark/peg/contentProcessors/PropertyContentProcessor.ts
@@ -41649,7 +42253,7 @@ function markConfigChainContentProcessor(context, _contentDepth, tagsConfig, con
41649
42253
  content.chain
41650
42254
  );
41651
42255
  if (context.DEBUG_CHAIN_TAGS) context.debugPrint("mark TAGS", tags2);
41652
- const mark = instance3.unbreakscape(
42256
+ const mark2 = instance3.unbreakscape(
41653
42257
  stringUtils.trimmedString(content.value) ?? "unknown",
41654
42258
  {
41655
42259
  format: TextFormat.bitmarkText,
@@ -41657,7 +42261,7 @@ function markConfigChainContentProcessor(context, _contentDepth, tagsConfig, con
41657
42261
  }
41658
42262
  );
41659
42263
  const config = {
41660
- mark,
42264
+ mark: mark2,
41661
42265
  emphasis: "underline",
41662
42266
  ...tags2
41663
42267
  };
@@ -47473,9 +48077,9 @@ ${nested}` : line;
47473
48077
  parts.push(href);
47474
48078
  }
47475
48079
  if (marks) {
47476
- for (const mark of marks) {
47477
- if (mark.type === "footnote") {
47478
- const footnote = mark;
48080
+ for (const mark2 of marks) {
48081
+ if (mark2.type === "footnote") {
48082
+ const footnote = mark2;
47479
48083
  if (footnote.attrs?.content) {
47480
48084
  const footnoteText = footnote.attrs.content.map((c) => this.textNodeToPlainText(c)).join("");
47481
48085
  if (footnoteText) parts.push(footnoteText);