@nectary/components 5.45.4 → 5.46.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/bundle.js CHANGED
@@ -1620,12 +1620,19 @@ const regEmoji = new RegExp(`(?<emoji>\\p{RI}\\p{RI}|${regEmojiSeq}(?:\\u200D${r
1620
1620
  const regUList = /^(?<indent>[\t ]*?)[*+-][\t ]+(?<ultext>.*?)[\t ]*?$/;
1621
1621
  const regOList = /^(?<indent>[\t ]*?)\d+\.[\t ]+(?<oltext>.*?)[\t ]*?$/;
1622
1622
  const regEscapedChars = /\\(?<escaped>[\\\*_\[\]`~\{\}])/;
1623
- const allRegs = [
1624
- regEscapedChars,
1623
+ const buildEscapedCharsReg = (extraChars) => {
1624
+ if (extraChars === "") {
1625
+ return regEscapedChars;
1626
+ }
1627
+ const classChars = Array.from(new Set(extraChars)).map((c) => c.replace(/[\\\]^-]/g, "\\$&")).join("");
1628
+ return new RegExp(regEscapedChars.source.replace(/\]\)$/, `${classChars}])`));
1629
+ };
1630
+ const buildAllRegs = (chipReg, escapeReg = regEscapedChars) => [
1631
+ escapeReg,
1625
1632
  regCodeTag,
1626
1633
  regButtonPlaceholder,
1627
1634
  regLink,
1628
- regChip,
1635
+ chipReg,
1629
1636
  regEm3Star,
1630
1637
  regEm2Star,
1631
1638
  regEm1Star,
@@ -1638,12 +1645,15 @@ const allRegs = [
1638
1645
  const isEmojiString = (data2) => regEmoji.test(data2);
1639
1646
  const excludeRegs = (regs, ...excluding) => regs.filter((r) => !excluding.includes(r));
1640
1647
  const matchClosest = (regs, line) => {
1641
- return regs.map((reg) => reg.exec(line)).reduce((res, match) => {
1642
- if (match === null) {
1648
+ return regs.map((reg) => {
1649
+ const match = reg.exec(line);
1650
+ return match === null ? null : { reg, match };
1651
+ }).reduce((res, current) => {
1652
+ if (current === null) {
1643
1653
  return res;
1644
1654
  }
1645
- if (res === null || res.index > match.index) {
1646
- return match;
1655
+ if (res === null || res.match.index > current.match.index) {
1656
+ return current;
1647
1657
  }
1648
1658
  return res;
1649
1659
  }, null);
@@ -1653,10 +1663,11 @@ const INITIAL_CONTEXT = {
1653
1663
  isItalic: false,
1654
1664
  isStrikethrough: false
1655
1665
  };
1656
- const createLineParser = (visitor) => function parseLine(regs, md, context = INITIAL_CONTEXT) {
1666
+ const createLineParser = (visitor, chipReg) => function parseLine(regs, md, context = INITIAL_CONTEXT) {
1657
1667
  let line = md;
1658
- let match = null;
1659
- while ((match = matchClosest(regs, line)) !== null) {
1668
+ let result = null;
1669
+ while ((result = matchClosest(regs, line)) !== null) {
1670
+ const { reg, match } = result;
1660
1671
  const groups = match.groups;
1661
1672
  const matchedStr = match[0];
1662
1673
  if (match.index > 0) {
@@ -1680,11 +1691,12 @@ const createLineParser = (visitor) => function parseLine(regs, md, context = INI
1680
1691
  if (groups?.code != null) {
1681
1692
  visitor.codetag(groups.code);
1682
1693
  }
1683
- if (groups?.chip != null) {
1684
- if (visitor.linkPlaceholder(groups.chip)) {
1685
- continue;
1694
+ if (reg === chipReg) {
1695
+ const chipName = groups?.chip ?? match[1];
1696
+ if (chipName != null && !visitor.linkPlaceholder(chipName)) {
1697
+ visitor.tag(chipName);
1686
1698
  }
1687
- visitor.tag(groups.chip);
1699
+ continue;
1688
1700
  }
1689
1701
  if (groups?.emoji != null) {
1690
1702
  visitor.emoji(groups.emoji);
@@ -1722,8 +1734,9 @@ const createLineParser = (visitor) => function parseLine(regs, md, context = INI
1722
1734
  visitor.inline(line, context);
1723
1735
  }
1724
1736
  };
1725
- const createBlockParser = (visitor) => {
1726
- const parseLine = createLineParser(visitor);
1737
+ const createBlockParser = (visitor, chipReg, escapeReg = regEscapedChars) => {
1738
+ const parseLine = createLineParser(visitor, chipReg);
1739
+ const allRegs = buildAllRegs(chipReg, escapeReg);
1727
1740
  const listRegs = [regUList, regOList];
1728
1741
  const getListItemText = (match) => {
1729
1742
  return match.groups.ultext ?? match.groups.oltext;
@@ -1737,12 +1750,12 @@ const createBlockParser = (visitor) => {
1737
1750
  const listMatch = matchClosest(listRegs, lines[i]);
1738
1751
  const isListLine = listMatch !== null;
1739
1752
  if (isListLine) {
1740
- const groups = listMatch.groups;
1753
+ const groups = listMatch.match.groups;
1741
1754
  const isOrderedList = groups.oltext != null;
1742
1755
  mode = isOrderedList ? BLOCK_MODE_OLIST : BLOCK_MODE_ULIST;
1743
1756
  visitor.list(isOrderedList);
1744
1757
  const listItemInitialIndent = groups.indent;
1745
- let listItemLines = [getListItemText(listMatch)];
1758
+ let listItemLines = [getListItemText(listMatch.match)];
1746
1759
  visitor.listItem();
1747
1760
  for (++i; i < lines.length; ++i) {
1748
1761
  const listItemMatch = isOrderedList ? regOList.exec(lines[i]) : regUList.exec(lines[i]);
@@ -1770,8 +1783,8 @@ const createBlockParser = (visitor) => {
1770
1783
  }
1771
1784
  };
1772
1785
  };
1773
- const parseMarkdown = (md, visitor) => {
1774
- const parseBlock = createBlockParser(visitor);
1786
+ const parseMarkdown = (md, visitor, chipReg = regChip, escapeReg = regEscapedChars) => {
1787
+ const parseBlock = createBlockParser(visitor, chipReg, escapeReg);
1775
1788
  for (const block of md.trim().split(regParagraph)) {
1776
1789
  parseBlock(block.trim().split(regLinebreak));
1777
1790
  }
@@ -13719,18 +13732,28 @@ const insertEmoji = ($root, emojiChar, range) => {
13719
13732
  )
13720
13733
  };
13721
13734
  };
13722
- const insertText = ($root, data2, range) => {
13735
+ const insertText = ($root, data2, range, chip) => {
13723
13736
  if (data2 !== null && isEmojiString(data2)) {
13724
13737
  return insertEmoji($root, data2, range);
13725
13738
  }
13739
+ const matchTagAtEnd = (text) => {
13740
+ const tagRe = chip?.pattern != null ? new RegExp(`${chip.pattern.source}$`) : new RegExp("(?<!\\\\)\\{\\{([a-zA-Z0-9_-]+)\\}\\}$");
13741
+ const match = text.match(tagRe);
13742
+ if (match !== null && match[1] != null) {
13743
+ return { name: match[1], length: match[0].length };
13744
+ }
13745
+ return null;
13746
+ };
13726
13747
  if (
13727
13748
  // Is collapsed range
13728
13749
  range.startContainer === range.endContainer && range.startOffset === range.endOffset && // Is non-empty text node
13729
13750
  isTextNode(range.startContainer) && !isEmptyText(range.startContainer.nodeValue)
13730
13751
  ) {
13731
13752
  const $inline2 = getParentInline(range.startContainer);
13732
- const isHotTextWhitespace = range.startOffset === range.startContainer.length && data2 === TEXT_WHITESPACE && (isFormatCodetag($inline2) || isFormatLink($inline2));
13733
- if (!isHotTextWhitespace) {
13753
+ const isAtEnd = range.startOffset === range.startContainer.length;
13754
+ const isHotTextWhitespace = isAtEnd && data2 === TEXT_WHITESPACE && (isFormatCodetag($inline2) || isFormatLink($inline2));
13755
+ const completesTag = isAtEnd && data2 === TEXT_WHITESPACE && matchTagAtEnd(range.startContainer.nodeValue ?? "") !== null;
13756
+ if (!isHotTextWhitespace && !completesTag) {
13734
13757
  return DEFAULT_ACTION_RESULT;
13735
13758
  }
13736
13759
  }
@@ -13779,11 +13802,16 @@ const insertText = ($root, data2, range) => {
13779
13802
  };
13780
13803
  }
13781
13804
  const content2 = $text.nodeValue;
13782
- const tagMatch = content2.match(/\{\{([a-zA-Z0-9_-]+)\}\}$/);
13783
- if (tagMatch !== null) {
13784
- const text = tagMatch[1];
13785
- const tagStartPos = content2.length - tagMatch[0].length;
13786
- const $tag = createTag(text, $root.ownerDocument);
13805
+ const tag = matchTagAtEnd(content2);
13806
+ if (tag !== null) {
13807
+ const tagStartPos = content2.length - tag.length;
13808
+ const options = chip?.resolve?.(tag.name);
13809
+ const $tag = createTag(
13810
+ tag.name,
13811
+ $root.ownerDocument,
13812
+ options?.color,
13813
+ options?.icon
13814
+ );
13787
13815
  const $space = createInlineWithText(data2, $root.ownerDocument);
13788
13816
  if (tagStartPos === 0) {
13789
13817
  getParentTextBlock($inline).replaceChild($tag, $inline);
@@ -13814,10 +13842,10 @@ const insertText = ($root, data2, range) => {
13814
13842
  range: createCollapsedRange(cursor)
13815
13843
  };
13816
13844
  };
13817
- const insertFromPaste = (data2, range, visitor) => {
13845
+ const insertFromPaste = (data2, range, visitor, parse) => {
13818
13846
  const cursor = removeContentInRange(range);
13819
13847
  const { $text, $inline, offset, isAfterInline } = cursor;
13820
- const $mdFragment = parseMarkdown(data2, visitor);
13848
+ const $mdFragment = parseMarkdown(data2, visitor, parse?.chipReg, parse?.escapeReg);
13821
13849
  if ($mdFragment.childNodes.length === 0) {
13822
13850
  return {
13823
13851
  prevent: true,
@@ -14008,7 +14036,7 @@ const serializeDescriptorReducer = (range) => (state, $n) => {
14008
14036
  if (isChip($n)) {
14009
14037
  const text2 = $n.text ?? "";
14010
14038
  if (text2.length > 0) {
14011
- state.push({ isTag: true, text: `{{${text2}}}` });
14039
+ state.push({ isTag: true, text: currentChipFormat(text2) });
14012
14040
  }
14013
14041
  return state;
14014
14042
  }
@@ -14232,13 +14260,26 @@ const serializeRoot = ($root, range) => {
14232
14260
  flushParagraphChunks();
14233
14261
  return chunks.join(MD_PARAGRAPH_JOIN);
14234
14262
  };
14235
- const serializeMarkdown = ($root, range) => {
14236
- if (range !== null) {
14237
- const aCursor = createIncomingCursorFromNodeWithOffset(range.startContainer, range.startOffset);
14238
- const bCursor = createIncomingCursorFromNodeWithOffset(range.endContainer, range.endOffset);
14239
- return serializeRoot($root, [aCursor, bCursor]);
14263
+ const defaultChipFormat = (name) => `{{${name}}}`;
14264
+ let currentChipFormat = defaultChipFormat;
14265
+ const serializeMarkdown = ($root, range, chipFormat) => {
14266
+ currentChipFormat = chipFormat ?? defaultChipFormat;
14267
+ try {
14268
+ if (range !== null) {
14269
+ const aCursor = createIncomingCursorFromNodeWithOffset(
14270
+ range.startContainer,
14271
+ range.startOffset
14272
+ );
14273
+ const bCursor = createIncomingCursorFromNodeWithOffset(
14274
+ range.endContainer,
14275
+ range.endOffset
14276
+ );
14277
+ return serializeRoot($root, [aCursor, bCursor]);
14278
+ }
14279
+ return serializeRoot($root, null);
14280
+ } finally {
14281
+ currentChipFormat = defaultChipFormat;
14240
14282
  }
14241
- return serializeRoot($root, null);
14242
14283
  };
14243
14284
  const createParseVisitor = (doc) => {
14244
14285
  let emojiBaseUrl = null;
@@ -14395,6 +14436,16 @@ class RichTextarea extends NectaryElement {
14395
14436
  * It is used to resolve the color and icon of a chip.
14396
14437
  */
14397
14438
  #chipResolver = null;
14439
+ /* @internal
14440
+ * Regex used to recognise a chip when parsing markdown (capture group 1 is
14441
+ * the chip name). When null the default `{{name}}` form is used.
14442
+ */
14443
+ #chipPattern = null;
14444
+ /* @internal
14445
+ * Formats a chip back to markdown when serialising. When null chips are
14446
+ * written as `{{name}}`.
14447
+ */
14448
+ #chipFormat = null;
14398
14449
  constructor() {
14399
14450
  super();
14400
14451
  const shadowRoot = this.attachShadow({ delegatesFocus: true });
@@ -14467,7 +14518,8 @@ class RichTextarea extends NectaryElement {
14467
14518
  "placeholder",
14468
14519
  "aria-label",
14469
14520
  "chip-color",
14470
- "chip-icon"
14521
+ "chip-icon",
14522
+ "chip-delimiter"
14471
14523
  ];
14472
14524
  }
14473
14525
  attributeChangedCallback(name, oldVal, newVal) {
@@ -14496,6 +14548,12 @@ class RichTextarea extends NectaryElement {
14496
14548
  this.#updateChipIcons(newVal);
14497
14549
  break;
14498
14550
  }
14551
+ case "chip-delimiter": {
14552
+ if (this.isConnected === true) {
14553
+ this.#dispatchChangeEvent();
14554
+ }
14555
+ break;
14556
+ }
14499
14557
  }
14500
14558
  }
14501
14559
  set value(value) {
@@ -14534,6 +14592,12 @@ class RichTextarea extends NectaryElement {
14534
14592
  get chipIcon() {
14535
14593
  return getAttribute(this, "chip-icon");
14536
14594
  }
14595
+ set chipDelimiter(value) {
14596
+ updateAttribute(this, "chip-delimiter", value);
14597
+ }
14598
+ get chipDelimiter() {
14599
+ return getAttribute(this, "chip-delimiter");
14600
+ }
14537
14601
  set chipResolver(resolver) {
14538
14602
  this.#chipResolver = resolver;
14539
14603
  this.#parseVisitor.updateChipResolver(resolver);
@@ -14542,6 +14606,49 @@ class RichTextarea extends NectaryElement {
14542
14606
  get chipResolver() {
14543
14607
  return this.#chipResolver;
14544
14608
  }
14609
+ set chipPattern(pattern) {
14610
+ this.#chipPattern = pattern;
14611
+ }
14612
+ get chipPattern() {
14613
+ return this.#chipPattern;
14614
+ }
14615
+ set chipFormat(format) {
14616
+ this.#chipFormat = format;
14617
+ if (this.isConnected === true) {
14618
+ this.#dispatchChangeEvent();
14619
+ }
14620
+ }
14621
+ get chipFormat() {
14622
+ return this.#chipFormat;
14623
+ }
14624
+ #getChipPattern() {
14625
+ if (this.#chipPattern !== null) {
14626
+ return this.#chipPattern;
14627
+ }
14628
+ const delimiter = this.chipDelimiter;
14629
+ if (delimiter !== null && delimiter !== "") {
14630
+ const escaped = delimiter.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
14631
+ return new RegExp(`(?<!\\\\)${escaped}([a-zA-Z0-9_-]+)${escaped}`);
14632
+ }
14633
+ return null;
14634
+ }
14635
+ #getChipFormat() {
14636
+ if (this.#chipFormat !== null) {
14637
+ return this.#chipFormat;
14638
+ }
14639
+ const delimiter = this.chipDelimiter;
14640
+ if (delimiter !== null && delimiter !== "") {
14641
+ return (name) => `${delimiter}${name}${delimiter}`;
14642
+ }
14643
+ return null;
14644
+ }
14645
+ #getEscapePattern() {
14646
+ const delimiter = this.chipDelimiter;
14647
+ if (delimiter !== null && delimiter !== "") {
14648
+ return buildEscapedCharsReg(delimiter);
14649
+ }
14650
+ return void 0;
14651
+ }
14545
14652
  get focusable() {
14546
14653
  return true;
14547
14654
  }
@@ -14718,6 +14825,13 @@ class RichTextarea extends NectaryElement {
14718
14825
  }
14719
14826
  this.#handleActionResult(handleResult);
14720
14827
  };
14828
+ #resolveChipOptions = (name) => {
14829
+ const resolved = this.#chipResolver?.(name);
14830
+ return {
14831
+ color: resolved?.color ?? this.getAttribute("chip-color") ?? void 0,
14832
+ icon: resolved?.icon ?? this.getAttribute("chip-icon") ?? void 0
14833
+ };
14834
+ };
14721
14835
  #handleInput(inputType, range, text, href) {
14722
14836
  if (range === null) {
14723
14837
  return { prevent: true, range: null };
@@ -14743,17 +14857,17 @@ class RichTextarea extends NectaryElement {
14743
14857
  }
14744
14858
  case "insertReplacementText":
14745
14859
  case "insertText": {
14746
- return insertText(this.#$input, text, range);
14860
+ return insertText(this.#$input, text, range, {
14861
+ pattern: this.#getChipPattern(),
14862
+ resolve: this.#resolveChipOptions
14863
+ });
14747
14864
  }
14748
14865
  case "insertLink": {
14749
14866
  return insertLink(this.#$input, text, href, range);
14750
14867
  }
14751
14868
  case "insertMention":
14752
14869
  case "insertChip": {
14753
- const resolved = this.#chipResolver?.(text);
14754
- const color = resolved?.color ?? this.getAttribute("chip-color") ?? void 0;
14755
- const icon = resolved?.icon ?? this.getAttribute("chip-icon") ?? void 0;
14756
- return insertChip(this.#$input, text, range, { color, icon });
14870
+ return insertChip(this.#$input, text, range, this.#resolveChipOptions(text));
14757
14871
  }
14758
14872
  // case 'formatUnderline':
14759
14873
  case "formatItalic":
@@ -14812,7 +14926,7 @@ class RichTextarea extends NectaryElement {
14812
14926
  this.#dispatchChangeEvent();
14813
14927
  };
14814
14928
  #dispatchChangeEvent() {
14815
- const value = serializeMarkdown(this.#$input, null);
14929
+ const value = serializeMarkdown(this.#$input, null, this.#getChipFormat());
14816
14930
  if (value === this.#prevDispatchedValue) {
14817
14931
  return;
14818
14932
  }
@@ -14855,7 +14969,12 @@ class RichTextarea extends NectaryElement {
14855
14969
  this.#cachedRange = null;
14856
14970
  this.#prevDispatchedValue = value;
14857
14971
  this.#$input.replaceChildren(
14858
- parseMarkdown(value, this.#parseVisitor.createVisitor())
14972
+ parseMarkdown(
14973
+ value,
14974
+ this.#parseVisitor.createVisitor(),
14975
+ this.#getChipPattern() ?? void 0,
14976
+ this.#getEscapePattern()
14977
+ )
14859
14978
  );
14860
14979
  this.#updateEditorEmptyClass();
14861
14980
  }
@@ -14865,7 +14984,10 @@ class RichTextarea extends NectaryElement {
14865
14984
  return;
14866
14985
  }
14867
14986
  if (e.clipboardData != null) {
14868
- e.clipboardData.setData("text/plain", serializeMarkdown(this.#$input, this.#cachedRange));
14987
+ e.clipboardData.setData(
14988
+ "text/plain",
14989
+ serializeMarkdown(this.#$input, this.#cachedRange, this.#getChipFormat())
14990
+ );
14869
14991
  }
14870
14992
  };
14871
14993
  #onCut = (e) => {
@@ -14874,7 +14996,10 @@ class RichTextarea extends NectaryElement {
14874
14996
  return;
14875
14997
  }
14876
14998
  if (e.clipboardData != null) {
14877
- e.clipboardData.setData("text/plain", serializeMarkdown(this.#$input, this.#cachedRange));
14999
+ e.clipboardData.setData(
15000
+ "text/plain",
15001
+ serializeMarkdown(this.#$input, this.#cachedRange, this.#getChipFormat())
15002
+ );
14878
15003
  }
14879
15004
  this.#handleActionResult(
14880
15005
  deleteContentBackward(this.#$input, this.#cachedRange)
@@ -14890,7 +15015,11 @@ class RichTextarea extends NectaryElement {
14890
15015
  insertFromPaste(
14891
15016
  pasteValue,
14892
15017
  this.#cachedRange,
14893
- this.#parseVisitor.createVisitor()
15018
+ this.#parseVisitor.createVisitor(),
15019
+ {
15020
+ chipReg: this.#getChipPattern() ?? void 0,
15021
+ escapeReg: this.#getEscapePattern()
15022
+ }
14894
15023
  )
14895
15024
  );
14896
15025
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nectary/components",
3
- "version": "5.45.4",
3
+ "version": "5.46.0",
4
4
  "files": [
5
5
  "**/*/*.css",
6
6
  "**/*/*.json",
@@ -21,8 +21,14 @@ export declare class RichTextarea extends NectaryElement {
21
21
  get chipColor(): string | null;
22
22
  set chipIcon(value: string | null);
23
23
  get chipIcon(): string | null;
24
+ set chipDelimiter(value: string | null);
25
+ get chipDelimiter(): string | null;
24
26
  set chipResolver(resolver: TChipResolver | null);
25
27
  get chipResolver(): TChipResolver | null;
28
+ set chipPattern(pattern: RegExp | null);
29
+ get chipPattern(): RegExp | null;
30
+ set chipFormat(format: ((name: string) => string) | null);
31
+ get chipFormat(): ((name: string) => string) | null;
26
32
  get focusable(): boolean;
27
33
  focus(): void;
28
34
  blur(): void;
@@ -4,7 +4,7 @@ import { updateAttribute, getAttribute, updateBooleanAttribute, getBooleanAttrib
4
4
  import { defineCustomElement, NectaryElement } from "../utils/element.js";
5
5
  import { isElementFocused } from "../utils/slot.js";
6
6
  import { getReactEventHandler } from "../utils/get-react-event-handler.js";
7
- import { parseMarkdown } from "../utils/markdown.js";
7
+ import { buildEscapedCharsReg, parseMarkdown } from "../utils/markdown.js";
8
8
  import { createParseVisitor, getEndRange, formatList, handleEmojiMousedown, removeChip, setBrowserCaret, formatOutdent, formatIndent, formatInline, insertChip, insertLink, insertText, insertLineBreak, deleteContentBackward, serializeMarkdown, getSelectionInfo, isSelectionEqual, insertFromPaste, isEditorEmpty } from "./utils.js";
9
9
  const templateHTML = '<style>:host{display:block}#wrapper{display:flex;flex-direction:column;position:relative;width:100%;box-sizing:border-box;background-color:var(--sinch-comp-textarea-color-default-background-initial);border-radius:var(--sinch-local-shape-radius);overflow:hidden;--sinch-local-shape-radius:var(--sinch-comp-textarea-shape-radius)}#input-wrapper{position:relative;padding:8px 10px 8px 12px;box-sizing:border-box}#input{font:var(--sinch-comp-textarea-font-input);color:var(--sinch-comp-textarea-color-default-text-initial);caret-color:var(--sinch-comp-textarea-color-default-text-initial);white-space:pre-wrap;overflow-wrap:break-word;border:none;outline:0}#placeholder{display:none;position:absolute;left:0;top:0;font:var(--sinch-comp-textarea-font-input);color:var(--sinch-comp-textarea-color-default-text-placeholder);padding:8px 10px 8px 12px;pointer-events:none;user-select:none}#input.empty+#placeholder{display:block}#border{position:absolute;border:1px solid var(--sinch-comp-textarea-color-default-border-initial);border-radius:var(--sinch-local-shape-radius);inset:0;pointer-events:none}:host([invalid]) #border{border-color:var(--sinch-comp-textarea-color-invalid-border-initial)}:host([disabled]){color:var(--sinch-comp-textarea-color-disabled-text-initial);-webkit-text-fill-color:var(--sinch-comp-textarea-color-disabled-text-initial)}:host([disabled]) #border{border-color:var(--sinch-comp-textarea-color-disabled-border-initial)}:host(:not([disabled])) #input-wrapper:focus-within~#border{border-color:var(--sinch-comp-textarea-color-default-border-focus);border-width:2px}.oli,.p,.uli{margin:0}.oli.l0,.uli.l0{margin-left:6px}.oli.l1,.uli.l1{margin-left:36px}.oli.l2,.uli.l2{margin-left:64px}.oli.l3,.uli.l3{margin-left:92px}.oli.l4,.uli.l4{margin-left:120px}.uli.l0{counter-reset:list-0 list-1 list-2 list-3 list-4}.uli.l1{counter-reset:list-1 list-2 list-3 list-4}.uli.l2{counter-reset:list-2 list-3 list-4}.uli.l3{counter-reset:list-3 list-4}.uli.l4{counter-reset:list-4}.oli.l0{counter-reset:list-1 list-2 list-3 list-4}.oli.l1{counter-reset:list-2 list-3 list-4}.oli.l2{counter-reset:list-3 list-4}.oli.l3{counter-reset:list-4}.oli.l0::before{counter-increment:list-0;content:counter(list-0,decimal) ". "}.oli.l1::before{counter-increment:list-1;content:counter(list-1,lower-alpha) ". "}.oli.l2::before{counter-increment:list-2;content:counter(list-2,lower-roman) ". "}.oli.l3::before{counter-increment:list-3;content:counter(list-3,decimal) ". "}.oli.l4::before{counter-increment:list-4;content:counter(list-4,lower-alpha) ". "}.oli.block,.oli:first-of-type,.p+.oli{counter-reset:list-0 list-1 list-2 list-3 list-4}.uli::before{content:"\\25CF";display:inline-block;width:16px}.oli+.p,.oli.block,.p+.oli,.p+.uli,.uli+.p,.uli.block{margin-top:.5em}.c{font:var(--sinch-comp-code-tag-font-text);font-size:inherit;line-height:inherit;color:var(--sinch-comp-code-tag-color-default-text-initial);border:1px solid var(--sinch-comp-code-tag-color-default-border-initial);background-color:var(--sinch-comp-code-tag-color-default-background-initial);padding:0 .25em;border-radius:var(--sinch-comp-code-tag-shape-radius)}.l{font:var(--sinch-comp-link-default-font-initial);color:var(--sinch-comp-link-color-default-text-initial);text-decoration:underline}sinch-rich-textarea-chip{display:inline-flex;vertical-align:middle;user-select:none}.i{font-style:italic}.b{font-weight:700}.s{text-decoration:line-through}.e{background-repeat:no-repeat;background-position:50% 50%;background-size:contain;width:1em;height:1em;vertical-align:-.2em}#top-wrapper{display:flex;flex-direction:row;align-items:center;gap:8px;padding:4px 4px 0}#top-wrapper.empty{display:none}#bottom-wrapper{display:flex;flex-direction:row;align-items:center;gap:8px;padding:0 4px 4px}#bottom-wrapper.empty{display:none}</style><div id="wrapper"><div id="top-wrapper"><slot id="top" name="top"></slot></div><div id="input-wrapper"><div id="input" contenteditable="true" role="textbox" aria-multiline="true" suppresscontenteditablewarning autocapitalize="false" autocorrect="false" autosave="false" spellcheck="false"></div><div id="placeholder"></div></div><div id="border"></div><div id="bottom-wrapper"><slot id="bottom" name="bottom"></slot></div></div>';
10
10
  const template = document.createElement("template");
@@ -32,6 +32,16 @@ class RichTextarea extends NectaryElement {
32
32
  * It is used to resolve the color and icon of a chip.
33
33
  */
34
34
  #chipResolver = null;
35
+ /* @internal
36
+ * Regex used to recognise a chip when parsing markdown (capture group 1 is
37
+ * the chip name). When null the default `{{name}}` form is used.
38
+ */
39
+ #chipPattern = null;
40
+ /* @internal
41
+ * Formats a chip back to markdown when serialising. When null chips are
42
+ * written as `{{name}}`.
43
+ */
44
+ #chipFormat = null;
35
45
  constructor() {
36
46
  super();
37
47
  const shadowRoot = this.attachShadow({ delegatesFocus: true });
@@ -104,7 +114,8 @@ class RichTextarea extends NectaryElement {
104
114
  "placeholder",
105
115
  "aria-label",
106
116
  "chip-color",
107
- "chip-icon"
117
+ "chip-icon",
118
+ "chip-delimiter"
108
119
  ];
109
120
  }
110
121
  attributeChangedCallback(name, oldVal, newVal) {
@@ -133,6 +144,12 @@ class RichTextarea extends NectaryElement {
133
144
  this.#updateChipIcons(newVal);
134
145
  break;
135
146
  }
147
+ case "chip-delimiter": {
148
+ if (this.isConnected === true) {
149
+ this.#dispatchChangeEvent();
150
+ }
151
+ break;
152
+ }
136
153
  }
137
154
  }
138
155
  set value(value) {
@@ -171,6 +188,12 @@ class RichTextarea extends NectaryElement {
171
188
  get chipIcon() {
172
189
  return getAttribute(this, "chip-icon");
173
190
  }
191
+ set chipDelimiter(value) {
192
+ updateAttribute(this, "chip-delimiter", value);
193
+ }
194
+ get chipDelimiter() {
195
+ return getAttribute(this, "chip-delimiter");
196
+ }
174
197
  set chipResolver(resolver) {
175
198
  this.#chipResolver = resolver;
176
199
  this.#parseVisitor.updateChipResolver(resolver);
@@ -179,6 +202,49 @@ class RichTextarea extends NectaryElement {
179
202
  get chipResolver() {
180
203
  return this.#chipResolver;
181
204
  }
205
+ set chipPattern(pattern) {
206
+ this.#chipPattern = pattern;
207
+ }
208
+ get chipPattern() {
209
+ return this.#chipPattern;
210
+ }
211
+ set chipFormat(format) {
212
+ this.#chipFormat = format;
213
+ if (this.isConnected === true) {
214
+ this.#dispatchChangeEvent();
215
+ }
216
+ }
217
+ get chipFormat() {
218
+ return this.#chipFormat;
219
+ }
220
+ #getChipPattern() {
221
+ if (this.#chipPattern !== null) {
222
+ return this.#chipPattern;
223
+ }
224
+ const delimiter = this.chipDelimiter;
225
+ if (delimiter !== null && delimiter !== "") {
226
+ const escaped = delimiter.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
227
+ return new RegExp(`(?<!\\\\)${escaped}([a-zA-Z0-9_-]+)${escaped}`);
228
+ }
229
+ return null;
230
+ }
231
+ #getChipFormat() {
232
+ if (this.#chipFormat !== null) {
233
+ return this.#chipFormat;
234
+ }
235
+ const delimiter = this.chipDelimiter;
236
+ if (delimiter !== null && delimiter !== "") {
237
+ return (name) => `${delimiter}${name}${delimiter}`;
238
+ }
239
+ return null;
240
+ }
241
+ #getEscapePattern() {
242
+ const delimiter = this.chipDelimiter;
243
+ if (delimiter !== null && delimiter !== "") {
244
+ return buildEscapedCharsReg(delimiter);
245
+ }
246
+ return void 0;
247
+ }
182
248
  get focusable() {
183
249
  return true;
184
250
  }
@@ -355,6 +421,13 @@ class RichTextarea extends NectaryElement {
355
421
  }
356
422
  this.#handleActionResult(handleResult);
357
423
  };
424
+ #resolveChipOptions = (name) => {
425
+ const resolved = this.#chipResolver?.(name);
426
+ return {
427
+ color: resolved?.color ?? this.getAttribute("chip-color") ?? void 0,
428
+ icon: resolved?.icon ?? this.getAttribute("chip-icon") ?? void 0
429
+ };
430
+ };
358
431
  #handleInput(inputType, range, text, href) {
359
432
  if (range === null) {
360
433
  return { prevent: true, range: null };
@@ -380,17 +453,17 @@ class RichTextarea extends NectaryElement {
380
453
  }
381
454
  case "insertReplacementText":
382
455
  case "insertText": {
383
- return insertText(this.#$input, text, range);
456
+ return insertText(this.#$input, text, range, {
457
+ pattern: this.#getChipPattern(),
458
+ resolve: this.#resolveChipOptions
459
+ });
384
460
  }
385
461
  case "insertLink": {
386
462
  return insertLink(this.#$input, text, href, range);
387
463
  }
388
464
  case "insertMention":
389
465
  case "insertChip": {
390
- const resolved = this.#chipResolver?.(text);
391
- const color = resolved?.color ?? this.getAttribute("chip-color") ?? void 0;
392
- const icon = resolved?.icon ?? this.getAttribute("chip-icon") ?? void 0;
393
- return insertChip(this.#$input, text, range, { color, icon });
466
+ return insertChip(this.#$input, text, range, this.#resolveChipOptions(text));
394
467
  }
395
468
  // case 'formatUnderline':
396
469
  case "formatItalic":
@@ -449,7 +522,7 @@ class RichTextarea extends NectaryElement {
449
522
  this.#dispatchChangeEvent();
450
523
  };
451
524
  #dispatchChangeEvent() {
452
- const value = serializeMarkdown(this.#$input, null);
525
+ const value = serializeMarkdown(this.#$input, null, this.#getChipFormat());
453
526
  if (value === this.#prevDispatchedValue) {
454
527
  return;
455
528
  }
@@ -492,7 +565,12 @@ class RichTextarea extends NectaryElement {
492
565
  this.#cachedRange = null;
493
566
  this.#prevDispatchedValue = value;
494
567
  this.#$input.replaceChildren(
495
- parseMarkdown(value, this.#parseVisitor.createVisitor())
568
+ parseMarkdown(
569
+ value,
570
+ this.#parseVisitor.createVisitor(),
571
+ this.#getChipPattern() ?? void 0,
572
+ this.#getEscapePattern()
573
+ )
496
574
  );
497
575
  this.#updateEditorEmptyClass();
498
576
  }
@@ -502,7 +580,10 @@ class RichTextarea extends NectaryElement {
502
580
  return;
503
581
  }
504
582
  if (e.clipboardData != null) {
505
- e.clipboardData.setData("text/plain", serializeMarkdown(this.#$input, this.#cachedRange));
583
+ e.clipboardData.setData(
584
+ "text/plain",
585
+ serializeMarkdown(this.#$input, this.#cachedRange, this.#getChipFormat())
586
+ );
506
587
  }
507
588
  };
508
589
  #onCut = (e) => {
@@ -511,7 +592,10 @@ class RichTextarea extends NectaryElement {
511
592
  return;
512
593
  }
513
594
  if (e.clipboardData != null) {
514
- e.clipboardData.setData("text/plain", serializeMarkdown(this.#$input, this.#cachedRange));
595
+ e.clipboardData.setData(
596
+ "text/plain",
597
+ serializeMarkdown(this.#$input, this.#cachedRange, this.#getChipFormat())
598
+ );
515
599
  }
516
600
  this.#handleActionResult(
517
601
  deleteContentBackward(this.#$input, this.#cachedRange)
@@ -527,7 +611,11 @@ class RichTextarea extends NectaryElement {
527
611
  insertFromPaste(
528
612
  pasteValue,
529
613
  this.#cachedRange,
530
- this.#parseVisitor.createVisitor()
614
+ this.#parseVisitor.createVisitor(),
615
+ {
616
+ chipReg: this.#getChipPattern() ?? void 0,
617
+ escapeReg: this.#getEscapePattern()
618
+ }
531
619
  )
532
620
  );
533
621
  };
@@ -24,6 +24,12 @@ export type TSinchRichTextareaProps = {
24
24
  'chip-color'?: TSinchTagColor;
25
25
  /** Default icon for chips/tags */
26
26
  'chip-icon'?: string;
27
+ /**
28
+ * Symmetric wrapping delimiter for chips in markdown, e.g. "#" produces
29
+ * `#name#`. A shorthand that derives chipPattern/chipFormat; the explicit
30
+ * chipPattern/chipFormat properties override it.
31
+ */
32
+ 'chip-delimiter'?: string;
27
33
  /** Accessible label for the rich textarea */
28
34
  'aria-label': string;
29
35
  };
@@ -43,6 +49,16 @@ export type TSinchRichTextareaMethods = {
43
49
  getCaretRect(): DOMRect | null;
44
50
  /** Resolver callback for chip icon and color based on tag name */
45
51
  chipResolver: TChipResolver | null;
52
+ /**
53
+ * Regex used to recognise a chip when parsing markdown. Capture group 1 is
54
+ * the chip name. When null the default `{{name}}` form is used.
55
+ */
56
+ chipPattern: RegExp | null;
57
+ /**
58
+ * Formats a chip back to markdown when serialising. When null chips are
59
+ * written as `{{name}}`.
60
+ */
61
+ chipFormat: ((name: string) => string) | null;
46
62
  };
47
63
  export type TSinchRichTextareaEvents = {
48
64
  /** Change value handler — fires while editing (debounced) and on blur */
@@ -28,15 +28,24 @@ export declare const insertChip: ($root: TRichTextareaRoot, text: string, range:
28
28
  icon?: string;
29
29
  }) => TActionResult;
30
30
  export declare const insertLineBreak: (range: TRange) => TActionResult;
31
- export declare const insertText: ($root: TRichTextareaRoot, data: string | null, range: TRange) => TActionResult;
32
- export declare const insertFromPaste: (data: string, range: Readonly<TRange>, visitor: TMarkdownParseVisitor) => TActionResult;
31
+ export declare const insertText: ($root: TRichTextareaRoot, data: string | null, range: TRange, chip?: {
32
+ pattern?: RegExp | null;
33
+ resolve?: (name: string) => {
34
+ color?: string;
35
+ icon?: string;
36
+ };
37
+ }) => TActionResult;
38
+ export declare const insertFromPaste: (data: string, range: Readonly<TRange>, visitor: TMarkdownParseVisitor, parse?: {
39
+ chipReg?: RegExp;
40
+ escapeReg?: RegExp;
41
+ }) => TActionResult;
33
42
  export declare const handleEmojiMousedown: ($n: Node) => TActionResult;
34
43
  export declare const getBeginRange: ($root: TRichTextareaRoot) => TRange;
35
44
  export declare const getEndRange: ($root: TRichTextareaRoot) => TRange;
36
45
  export declare const getSelectionInfo: (range: TRange) => TRichTextareaSelection;
37
46
  export declare const isSelectionEqual: (a: TRichTextareaSelection | null, b: TRichTextareaSelection | null) => boolean;
38
47
  export declare const isEditorEmpty: ($root: TRichTextareaRoot) => boolean;
39
- export declare const serializeMarkdown: ($root: TRichTextareaRoot, range: Readonly<TRange> | null) => string;
48
+ export declare const serializeMarkdown: ($root: TRichTextareaRoot, range: Readonly<TRange> | null, chipFormat?: ((name: string) => string) | null) => string;
40
49
  export declare const createParseVisitor: (doc: Document) => {
41
50
  updateEmojiBaseUrl(url: string | null): void;
42
51
  updateChipColor(color: string | null): void;
@@ -1222,18 +1222,28 @@ const insertEmoji = ($root, emojiChar, range) => {
1222
1222
  )
1223
1223
  };
1224
1224
  };
1225
- const insertText = ($root, data, range) => {
1225
+ const insertText = ($root, data, range, chip) => {
1226
1226
  if (data !== null && isEmojiString(data)) {
1227
1227
  return insertEmoji($root, data, range);
1228
1228
  }
1229
+ const matchTagAtEnd = (text) => {
1230
+ const tagRe = chip?.pattern != null ? new RegExp(`${chip.pattern.source}$`) : new RegExp("(?<!\\\\)\\{\\{([a-zA-Z0-9_-]+)\\}\\}$");
1231
+ const match = text.match(tagRe);
1232
+ if (match !== null && match[1] != null) {
1233
+ return { name: match[1], length: match[0].length };
1234
+ }
1235
+ return null;
1236
+ };
1229
1237
  if (
1230
1238
  // Is collapsed range
1231
1239
  range.startContainer === range.endContainer && range.startOffset === range.endOffset && // Is non-empty text node
1232
1240
  isTextNode(range.startContainer) && !isEmptyText(range.startContainer.nodeValue)
1233
1241
  ) {
1234
1242
  const $inline2 = getParentInline(range.startContainer);
1235
- const isHotTextWhitespace = range.startOffset === range.startContainer.length && data === TEXT_WHITESPACE && (isFormatCodetag($inline2) || isFormatLink($inline2));
1236
- if (!isHotTextWhitespace) {
1243
+ const isAtEnd = range.startOffset === range.startContainer.length;
1244
+ const isHotTextWhitespace = isAtEnd && data === TEXT_WHITESPACE && (isFormatCodetag($inline2) || isFormatLink($inline2));
1245
+ const completesTag = isAtEnd && data === TEXT_WHITESPACE && matchTagAtEnd(range.startContainer.nodeValue ?? "") !== null;
1246
+ if (!isHotTextWhitespace && !completesTag) {
1237
1247
  return DEFAULT_ACTION_RESULT;
1238
1248
  }
1239
1249
  }
@@ -1282,11 +1292,16 @@ const insertText = ($root, data, range) => {
1282
1292
  };
1283
1293
  }
1284
1294
  const content2 = $text.nodeValue;
1285
- const tagMatch = content2.match(/\{\{([a-zA-Z0-9_-]+)\}\}$/);
1286
- if (tagMatch !== null) {
1287
- const text = tagMatch[1];
1288
- const tagStartPos = content2.length - tagMatch[0].length;
1289
- const $tag = createTag(text, $root.ownerDocument);
1295
+ const tag = matchTagAtEnd(content2);
1296
+ if (tag !== null) {
1297
+ const tagStartPos = content2.length - tag.length;
1298
+ const options = chip?.resolve?.(tag.name);
1299
+ const $tag = createTag(
1300
+ tag.name,
1301
+ $root.ownerDocument,
1302
+ options?.color,
1303
+ options?.icon
1304
+ );
1290
1305
  const $space = createInlineWithText(data, $root.ownerDocument);
1291
1306
  if (tagStartPos === 0) {
1292
1307
  getParentTextBlock($inline).replaceChild($tag, $inline);
@@ -1317,10 +1332,10 @@ const insertText = ($root, data, range) => {
1317
1332
  range: createCollapsedRange(cursor)
1318
1333
  };
1319
1334
  };
1320
- const insertFromPaste = (data, range, visitor) => {
1335
+ const insertFromPaste = (data, range, visitor, parse) => {
1321
1336
  const cursor = removeContentInRange(range);
1322
1337
  const { $text, $inline, offset, isAfterInline } = cursor;
1323
- const $mdFragment = parseMarkdown(data, visitor);
1338
+ const $mdFragment = parseMarkdown(data, visitor, parse?.chipReg, parse?.escapeReg);
1324
1339
  if ($mdFragment.childNodes.length === 0) {
1325
1340
  return {
1326
1341
  prevent: true,
@@ -1511,7 +1526,7 @@ const serializeDescriptorReducer = (range) => (state, $n) => {
1511
1526
  if (isChip($n)) {
1512
1527
  const text2 = $n.text ?? "";
1513
1528
  if (text2.length > 0) {
1514
- state.push({ isTag: true, text: `{{${text2}}}` });
1529
+ state.push({ isTag: true, text: currentChipFormat(text2) });
1515
1530
  }
1516
1531
  return state;
1517
1532
  }
@@ -1735,13 +1750,26 @@ const serializeRoot = ($root, range) => {
1735
1750
  flushParagraphChunks();
1736
1751
  return chunks.join(MD_PARAGRAPH_JOIN);
1737
1752
  };
1738
- const serializeMarkdown = ($root, range) => {
1739
- if (range !== null) {
1740
- const aCursor = createIncomingCursorFromNodeWithOffset(range.startContainer, range.startOffset);
1741
- const bCursor = createIncomingCursorFromNodeWithOffset(range.endContainer, range.endOffset);
1742
- return serializeRoot($root, [aCursor, bCursor]);
1753
+ const defaultChipFormat = (name) => `{{${name}}}`;
1754
+ let currentChipFormat = defaultChipFormat;
1755
+ const serializeMarkdown = ($root, range, chipFormat) => {
1756
+ currentChipFormat = chipFormat ?? defaultChipFormat;
1757
+ try {
1758
+ if (range !== null) {
1759
+ const aCursor = createIncomingCursorFromNodeWithOffset(
1760
+ range.startContainer,
1761
+ range.startOffset
1762
+ );
1763
+ const bCursor = createIncomingCursorFromNodeWithOffset(
1764
+ range.endContainer,
1765
+ range.endOffset
1766
+ );
1767
+ return serializeRoot($root, [aCursor, bCursor]);
1768
+ }
1769
+ return serializeRoot($root, null);
1770
+ } finally {
1771
+ currentChipFormat = defaultChipFormat;
1743
1772
  }
1744
- return serializeRoot($root, null);
1745
1773
  };
1746
1774
  const createParseVisitor = (doc) => {
1747
1775
  let emojiBaseUrl = null;
package/utils/index.js CHANGED
@@ -7,7 +7,7 @@ import { getFirstFocusableElement, getFirstSlotElement, isElementFocused } from
7
7
  import { throttleAnimationFrame } from "./throttle.js";
8
8
  import { debounceAnimationFrame, debounceTimeout } from "./debounce.js";
9
9
  import { getReactEventHandler } from "./get-react-event-handler.js";
10
- import { isEmojiString, parseMarkdown } from "./markdown.js";
10
+ import { buildEscapedCharsReg, isEmojiString, parseMarkdown } from "./markdown.js";
11
11
  import { getTargetAttribute, getTargetByAttribute, getTargetIndexInParent, isTargetEqual } from "./event-target.js";
12
12
  import { isActivationKey } from "./control-keyboard-navigation.js";
13
13
  import { getUid } from "./uid.js";
@@ -19,6 +19,7 @@ export {
19
19
  attrValueToFloat,
20
20
  attrValueToInteger,
21
21
  attrValueToPixels,
22
+ buildEscapedCharsReg,
22
23
  clampNumber,
23
24
  cloneNode,
24
25
  composedContains,
@@ -20,5 +20,6 @@ export type TMarkdownParseVisitor = {
20
20
  listItem(): void;
21
21
  end(): Node;
22
22
  };
23
+ export declare const buildEscapedCharsReg: (extraChars: string) => RegExp;
23
24
  export declare const isEmojiString: (data: string) => boolean;
24
- export declare const parseMarkdown: (md: string, visitor: TMarkdownParseVisitor) => Node;
25
+ export declare const parseMarkdown: (md: string, visitor: TMarkdownParseVisitor, chipReg?: RegExp, escapeReg?: RegExp) => Node;
package/utils/markdown.js CHANGED
@@ -16,12 +16,19 @@ const regEmoji = new RegExp(`(?<emoji>\\p{RI}\\p{RI}|${regEmojiSeq}(?:\\u200D${r
16
16
  const regUList = /^(?<indent>[\t ]*?)[*+-][\t ]+(?<ultext>.*?)[\t ]*?$/;
17
17
  const regOList = /^(?<indent>[\t ]*?)\d+\.[\t ]+(?<oltext>.*?)[\t ]*?$/;
18
18
  const regEscapedChars = /\\(?<escaped>[\\\*_\[\]`~\{\}])/;
19
- const allRegs = [
20
- regEscapedChars,
19
+ const buildEscapedCharsReg = (extraChars) => {
20
+ if (extraChars === "") {
21
+ return regEscapedChars;
22
+ }
23
+ const classChars = Array.from(new Set(extraChars)).map((c) => c.replace(/[\\\]^-]/g, "\\$&")).join("");
24
+ return new RegExp(regEscapedChars.source.replace(/\]\)$/, `${classChars}])`));
25
+ };
26
+ const buildAllRegs = (chipReg, escapeReg = regEscapedChars) => [
27
+ escapeReg,
21
28
  regCodeTag,
22
29
  regButtonPlaceholder,
23
30
  regLink,
24
- regChip,
31
+ chipReg,
25
32
  regEm3Star,
26
33
  regEm2Star,
27
34
  regEm1Star,
@@ -34,12 +41,15 @@ const allRegs = [
34
41
  const isEmojiString = (data) => regEmoji.test(data);
35
42
  const excludeRegs = (regs, ...excluding) => regs.filter((r) => !excluding.includes(r));
36
43
  const matchClosest = (regs, line) => {
37
- return regs.map((reg) => reg.exec(line)).reduce((res, match) => {
38
- if (match === null) {
44
+ return regs.map((reg) => {
45
+ const match = reg.exec(line);
46
+ return match === null ? null : { reg, match };
47
+ }).reduce((res, current) => {
48
+ if (current === null) {
39
49
  return res;
40
50
  }
41
- if (res === null || res.index > match.index) {
42
- return match;
51
+ if (res === null || res.match.index > current.match.index) {
52
+ return current;
43
53
  }
44
54
  return res;
45
55
  }, null);
@@ -49,10 +59,11 @@ const INITIAL_CONTEXT = {
49
59
  isItalic: false,
50
60
  isStrikethrough: false
51
61
  };
52
- const createLineParser = (visitor) => function parseLine(regs, md, context = INITIAL_CONTEXT) {
62
+ const createLineParser = (visitor, chipReg) => function parseLine(regs, md, context = INITIAL_CONTEXT) {
53
63
  let line = md;
54
- let match = null;
55
- while ((match = matchClosest(regs, line)) !== null) {
64
+ let result = null;
65
+ while ((result = matchClosest(regs, line)) !== null) {
66
+ const { reg, match } = result;
56
67
  const groups = match.groups;
57
68
  const matchedStr = match[0];
58
69
  if (match.index > 0) {
@@ -76,11 +87,12 @@ const createLineParser = (visitor) => function parseLine(regs, md, context = INI
76
87
  if (groups?.code != null) {
77
88
  visitor.codetag(groups.code);
78
89
  }
79
- if (groups?.chip != null) {
80
- if (visitor.linkPlaceholder(groups.chip)) {
81
- continue;
90
+ if (reg === chipReg) {
91
+ const chipName = groups?.chip ?? match[1];
92
+ if (chipName != null && !visitor.linkPlaceholder(chipName)) {
93
+ visitor.tag(chipName);
82
94
  }
83
- visitor.tag(groups.chip);
95
+ continue;
84
96
  }
85
97
  if (groups?.emoji != null) {
86
98
  visitor.emoji(groups.emoji);
@@ -118,8 +130,9 @@ const createLineParser = (visitor) => function parseLine(regs, md, context = INI
118
130
  visitor.inline(line, context);
119
131
  }
120
132
  };
121
- const createBlockParser = (visitor) => {
122
- const parseLine = createLineParser(visitor);
133
+ const createBlockParser = (visitor, chipReg, escapeReg = regEscapedChars) => {
134
+ const parseLine = createLineParser(visitor, chipReg);
135
+ const allRegs = buildAllRegs(chipReg, escapeReg);
123
136
  const listRegs = [regUList, regOList];
124
137
  const getListItemText = (match) => {
125
138
  return match.groups.ultext ?? match.groups.oltext;
@@ -133,12 +146,12 @@ const createBlockParser = (visitor) => {
133
146
  const listMatch = matchClosest(listRegs, lines[i]);
134
147
  const isListLine = listMatch !== null;
135
148
  if (isListLine) {
136
- const groups = listMatch.groups;
149
+ const groups = listMatch.match.groups;
137
150
  const isOrderedList = groups.oltext != null;
138
151
  mode = isOrderedList ? BLOCK_MODE_OLIST : BLOCK_MODE_ULIST;
139
152
  visitor.list(isOrderedList);
140
153
  const listItemInitialIndent = groups.indent;
141
- let listItemLines = [getListItemText(listMatch)];
154
+ let listItemLines = [getListItemText(listMatch.match)];
142
155
  visitor.listItem();
143
156
  for (++i; i < lines.length; ++i) {
144
157
  const listItemMatch = isOrderedList ? regOList.exec(lines[i]) : regUList.exec(lines[i]);
@@ -166,14 +179,15 @@ const createBlockParser = (visitor) => {
166
179
  }
167
180
  };
168
181
  };
169
- const parseMarkdown = (md, visitor) => {
170
- const parseBlock = createBlockParser(visitor);
182
+ const parseMarkdown = (md, visitor, chipReg = regChip, escapeReg = regEscapedChars) => {
183
+ const parseBlock = createBlockParser(visitor, chipReg, escapeReg);
171
184
  for (const block of md.trim().split(regParagraph)) {
172
185
  parseBlock(block.trim().split(regLinebreak));
173
186
  }
174
187
  return visitor.end();
175
188
  };
176
189
  export {
190
+ buildEscapedCharsReg,
177
191
  isEmojiString,
178
192
  parseMarkdown
179
193
  };