@nectary/components 2.6.0 → 2.7.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.
@@ -1,6 +1,6 @@
1
1
  import '../text';
2
2
  import '../icon';
3
- import { defineCustomElement, getAttribute, getBooleanAttribute, getLiteralAttribute, getReactEventHandler, isAttrTrue, NectaryElement, setClass, subscribeContext, updateAttribute, updateBooleanAttribute, updateExplicitBooleanAttribute, updateLiteralAttribute, Context, isAttrEqual } from '../utils';
3
+ import { defineCustomElement, getAttribute, getBooleanAttribute, getLiteralAttribute, getReactEventHandler, isAttrTrue, NectaryElement, setClass, subscribeContext, updateAttribute, updateBooleanAttribute, updateLiteralAttribute, Context, isAttrEqual } from '../utils';
4
4
  import { DEFAULT_SIZE, sizeValues } from '../utils/size';
5
5
  const templateHTML = '<style>:host{display:inline-block;vertical-align:middle;outline:0;cursor:pointer}:host([disabled]){cursor:initial}#wrapper{position:relative;display:flex;flex-direction:row;align-items:center;gap:8px;box-sizing:border-box;width:100%;height:var(--sinch-local-size);padding:0 8px 0 12px;background-color:var(--sinch-comp-select-button-color-default-background-initial);border-radius:var(--sinch-local-shape-radius);--sinch-local-size:var(--sinch-comp-select-button-size-container-m);--sinch-global-size-icon:var(--sinch-comp-select-button-size-icon-m);--sinch-local-shape-radius:var(--sinch-comp-select-button-shape-radius-size-m);--sinch-global-color-icon:var(--sinch-comp-select-button-color-default-icon-initial)}:host([data-size="l"])>#wrapper{--sinch-local-size:var(--sinch-comp-select-button-size-container-l);--sinch-global-size-icon:var(--sinch-comp-select-button-size-icon-l);--sinch-local-shape-radius:var(--sinch-comp-select-button-shape-radius-size-l)}:host([data-size="m"])>#wrapper{--sinch-local-size:var(--sinch-comp-select-button-size-container-m);--sinch-global-size-icon:var(--sinch-comp-select-button-size-icon-m);--sinch-local-shape-radius:var(--sinch-comp-select-button-shape-radius-size-m)}:host([data-size="s"])>#wrapper{--sinch-local-size:var(--sinch-comp-select-button-size-container-s);--sinch-global-size-icon:var(--sinch-comp-select-button-size-icon-s);--sinch-local-shape-radius:var(--sinch-comp-select-button-shape-radius-size-s)}:host([data-size="l"]) #wrapper{padding:0 12px}:host([data-size="m"]) #wrapper{padding:0 8px 0 12px}:host([data-size="s"]) #wrapper{padding:0 4px 0 12px}:host([disabled]) #wrapper{--sinch-global-color-icon:var(--sinch-comp-select-button-color-disabled-icon-initial)}#text{flex:1;min-width:0;--sinch-comp-text-font:var(--sinch-comp-select-button-font-input);--sinch-global-color-text:var(--sinch-comp-select-button-color-default-text-initial)}#text:empty{display:none}#placeholder{display:none;flex:1;min-width:0;--sinch-comp-text-font:var(--sinch-comp-select-button-font-placeholder);--sinch-global-color-text:var(--sinch-comp-select-button-color-default-placeholder-initial)}#text:empty+#placeholder{display:block}#border{position:absolute;border:1px solid var(--sinch-comp-select-button-color-default-border-initial);border-radius:var(--sinch-local-shape-radius);inset:0;pointer-events:none}:host(:focus-visible) #border{border-color:var(--sinch-comp-select-button-color-default-border-focus);border-width:2px}:host([invalid]) #border{border-color:var(--sinch-comp-select-button-color-invalid-border-initial)}:host([disabled]) #border{border-color:var(--sinch-comp-select-button-color-disabled-border-initial)}:host([disabled]) #text{--sinch-global-color-text:var(--sinch-comp-select-button-color-disabled-text-initial)}:host([disabled]) #placeholder{--sinch-global-color-text:var(--sinch-comp-select-button-color-disabled-placeholder-initial)}#left{display:flex;flex-direction:row;align-self:stretch;align-items:center;gap:4px;margin-left:-4px}#left.empty{display:none}#dropdown-icon{margin-left:-4px}</style><div id="wrapper" inert><div id="border"></div><div id="left"><slot name="left"></slot></div><slot name="icon"></slot><sinch-text id="text" type="m" ellipsis></sinch-text><sinch-text id="placeholder" type="m" ellipsis></sinch-text><sinch-icon id="dropdown-icon" name="keyboard_arrow_down"></sinch-icon></div>';
6
6
  const template = document.createElement('template');
@@ -87,13 +87,15 @@ defineCustomElement('sinch-select-button', class extends NectaryElement {
87
87
  case 'invalid':
88
88
  {
89
89
  const isInvalid = isAttrTrue(newVal);
90
- updateExplicitBooleanAttribute(this, 'aria-invalid', isInvalid);
90
+ this.ariaInvalid = isInvalid.toString();
91
91
  updateBooleanAttribute(this, 'invalid', isInvalid);
92
92
  break;
93
93
  }
94
94
  case 'disabled':
95
95
  {
96
- updateBooleanAttribute(this, 'disabled', isAttrTrue(newVal));
96
+ const isDisabled = isAttrTrue(newVal);
97
+ this.ariaDisabled = isDisabled.toString();
98
+ updateBooleanAttribute(this, 'disabled', isDisabled);
97
99
  break;
98
100
  }
99
101
  case 'size':
package/textarea/index.js CHANGED
@@ -1,17 +1,20 @@
1
- import { Context, defineCustomElement, getAttribute, getBooleanAttribute, getIntegerAttribute, getReactEventHandler, isAttrEqual, isAttrTrue, NectaryElement, setClass, updateAttribute, updateBooleanAttribute, updateExplicitBooleanAttribute } from '../utils';
1
+ import { Context, defineCustomElement, getAttribute, getBooleanAttribute, getIntegerAttribute, getReactEventHandler, getRect, isAttrEqual, isAttrTrue, NectaryElement, setClass, updateAttribute, updateBooleanAttribute } from '../utils';
2
2
  import { DEFAULT_SIZE } from '../utils/size';
3
- const templateHTML = '<style>:host{all:initial;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);padding-right:2px;padding-bottom:2px;overflow:hidden;--sinch-local-shape-radius:var(--sinch-comp-textarea-shape-radius)}#input{all:initial;display:block;font:var(--sinch-comp-textarea-font-input);color:var(--sinch-comp-textarea-color-default-text-initial);resize:none;white-space:pre-wrap;overflow-wrap:break-word;padding:8px 10px 8px 12px;border:none;box-sizing:border-box}#input::placeholder{color:var(--sinch-comp-textarea-color-default-text-placeholder);opacity:1}#input:disabled{color:var(--sinch-comp-textarea-color-disabled-text-initial);-webkit-text-fill-color:var(--sinch-comp-textarea-color-disabled-text-initial)}:host([resizable]) #input{resize:vertical}#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)}#input:focus+#border{border-color:var(--sinch-comp-textarea-color-default-border-focus);border-width:2px}#input:disabled+#border{border-color:var(--sinch-comp-textarea-color-disabled-border-initial)}#bottom{display:flex;flex-direction:row;align-items:center;gap:8px;padding:8px 10px 10px 12px}#bottom.empty{display:none}</style><div id="wrapper"><textarea id="input"></textarea><div id="border"></div><div id="bottom"><slot name="bottom"></slot></div></div>';
3
+ 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);padding-right:2px;overflow:hidden;--sinch-local-shape-radius:var(--sinch-comp-textarea-shape-radius)}#input{all:initial;display:block;font:var(--sinch-comp-textarea-font-input);color:var(--sinch-comp-textarea-color-default-text-initial);resize:none;white-space:pre-wrap;overflow-wrap:break-word;padding:8px 10px 8px 12px;border:none;box-sizing:border-box}#input::placeholder{color:var(--sinch-comp-textarea-color-default-text-placeholder);opacity:1}#input:disabled{color:var(--sinch-comp-textarea-color-disabled-text-initial);-webkit-text-fill-color:var(--sinch-comp-textarea-color-disabled-text-initial)}#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)}#input:focus+#border{border-color:var(--sinch-comp-textarea-color-default-border-focus);border-width:2px}#input:disabled+#border{border-color:var(--sinch-comp-textarea-color-disabled-border-initial)}#bottom{display:flex;flex-direction:row;align-items:center;gap:8px;padding:12px 4px 4px}#bottom.empty{display:none}#resize-handle{display:none;position:absolute;width:20px;height:20px;bottom:36px;right:0;cursor:ns-resize}:host([resizable]) #resize-handle{display:block}#bottom.empty+#resize-handle{bottom:0}#resize-icon{display:block;pointer-events:none;fill:var(--sinch-comp-textarea-color-default-border-initial)}</style><div id="wrapper"><textarea id="input"></textarea><div id="border"></div><div id="bottom"><slot name="bottom"></slot></div><div id="resize-handle"><svg id="resize-icon" width="16" height="16"><path d="m14.833 4.724-9.61 9.61-.942-.944 9.61-9.609.942.943ZM15.443 10 10.5 14.943 9.557 14 14.5 9.057l.943.943Z"/></svg></div></div>';
4
4
  const template = document.createElement('template');
5
5
  template.innerHTML = templateHTML;
6
6
  defineCustomElement('sinch-textarea', class extends NectaryElement {
7
7
  #$input;
8
8
  #$bottomSlot;
9
9
  #$bottomWrapper;
10
+ #$resizeHandle;
10
11
  #cursorPos = null;
11
12
  #isPendingDk = false;
12
13
  #controller = null;
13
14
  #sizeContext;
14
- #contentHeight = 0;
15
+ #prevContentHeight = 0;
16
+ #dragStartY = 0;
17
+ #intersectionObserver = null;
15
18
  constructor() {
16
19
  super();
17
20
  const shadowRoot = this.attachShadow({
@@ -21,6 +24,7 @@ defineCustomElement('sinch-textarea', class extends NectaryElement {
21
24
  this.#$input = shadowRoot.querySelector('#input');
22
25
  this.#$bottomSlot = shadowRoot.querySelector('slot[name="bottom"]');
23
26
  this.#$bottomWrapper = shadowRoot.querySelector('#bottom');
27
+ this.#$resizeHandle = shadowRoot.querySelector('#resize-handle');
24
28
  this.#sizeContext = new Context(this.#$bottomWrapper, 'size');
25
29
  }
26
30
  connectedCallback() {
@@ -37,19 +41,24 @@ defineCustomElement('sinch-textarea', class extends NectaryElement {
37
41
  this.#$input.addEventListener('keydown', this.#onSelectionChange, options);
38
42
  this.#$input.addEventListener('focus', this.#onInputFocus, options);
39
43
  this.#$input.addEventListener('blur', this.#onInputBlur, options);
44
+ this.#$resizeHandle.addEventListener('mousedown', this.#onDragStart, options);
40
45
  this.#$bottomSlot.addEventListener('slotchange', this.#onBottomSlotChange, options);
41
46
  this.addEventListener('-change', this.#onChangeReactHandler, options);
42
47
  this.addEventListener('-focus', this.#onFocusReactHandler, options);
43
48
  this.addEventListener('-blur', this.#onBlurReactHandler, options);
44
49
  this.#sizeContext.listen(this.#controller.signal);
45
50
  this.#onBottomSlotChange();
46
- this.updateMinRows();
51
+ this.#updateMinRows();
47
52
  this.#onSizeUpdate();
48
53
  }
49
54
  disconnectedCallback() {
50
55
  super.disconnectedCallback();
51
56
  this.#controller.abort();
52
57
  this.#controller = null;
58
+ if (this.#intersectionObserver !== null) {
59
+ this.#intersectionObserver.disconnect();
60
+ this.#intersectionObserver = null;
61
+ }
53
62
  }
54
63
  static get observedAttributes() {
55
64
  return ['value', 'placeholder', 'invalid', 'disabled', 'rows', 'minrows', 'resizable'];
@@ -70,9 +79,9 @@ defineCustomElement('sinch-textarea', class extends NectaryElement {
70
79
  this.#$input.style.removeProperty('height');
71
80
  }
72
81
  const nextContentHeight = this.#$input.scrollHeight;
73
- if (isShrinkingContent || nextContentHeight !== this.#contentHeight) {
74
- this.#contentHeight = nextContentHeight;
75
- this.#$input.style.setProperty('height', `${this.#contentHeight}px`);
82
+ if (isShrinkingContent || nextContentHeight !== this.#prevContentHeight) {
83
+ this.#prevContentHeight = nextContentHeight;
84
+ this.#$input.style.setProperty('height', `${this.#prevContentHeight}px`);
76
85
  }
77
86
  }
78
87
  if (!isPrevCursorEnd) {
@@ -93,7 +102,7 @@ defineCustomElement('sinch-textarea', class extends NectaryElement {
93
102
  break;
94
103
  }
95
104
  const isInvalid = isAttrTrue(newVal);
96
- updateExplicitBooleanAttribute(this, 'aria-invalid', isInvalid);
105
+ this.ariaInvalid = isInvalid.toString();
97
106
  updateBooleanAttribute(this, 'invalid', isInvalid);
98
107
  break;
99
108
  }
@@ -114,7 +123,7 @@ defineCustomElement('sinch-textarea', class extends NectaryElement {
114
123
  }
115
124
  case 'minrows':
116
125
  {
117
- this.updateMinRows();
126
+ this.#updateMinRows();
118
127
  break;
119
128
  }
120
129
  case 'resizable':
@@ -196,19 +205,37 @@ defineCustomElement('sinch-textarea', class extends NectaryElement {
196
205
  blur() {
197
206
  this.#$input.blur();
198
207
  }
199
- updateMinRows() {
200
- if (!this.isDomConnected) {
201
- return;
202
- }
208
+ #updateMinRows() {
203
209
  const minRows = this.minRows;
204
210
  if (minRows <= 0) {
205
211
  this.#$input.style.removeProperty('min-height');
212
+ if (this.#intersectionObserver !== null) {
213
+ this.#intersectionObserver.disconnect();
214
+ this.#intersectionObserver = null;
215
+ }
206
216
  } else {
207
- this.#$input.rows = minRows;
208
- this.#$input.style.setProperty('min-height', `${this.#$input.clientHeight}px`);
209
- this.#$input.rows = this.rows;
217
+ if (this.isDomConnected) {
218
+ this.#calcMinRows();
219
+ }
220
+ if (this.#intersectionObserver === null) {
221
+ this.#intersectionObserver = new IntersectionObserver(this.#intersectionObserverCallback, {
222
+ root: null
223
+ });
224
+ this.#intersectionObserver.observe(this.#$input);
225
+ }
210
226
  }
211
227
  }
228
+ #intersectionObserverCallback = _ref => {
229
+ let [entry] = _ref;
230
+ if (entry != null && entry.isIntersecting) {
231
+ this.#calcMinRows();
232
+ }
233
+ };
234
+ #calcMinRows() {
235
+ this.#$input.rows = this.minRows;
236
+ this.#$input.style.setProperty('min-height', `${getRect(this.#$input).height}px`);
237
+ this.#$input.rows = this.rows;
238
+ }
212
239
  #onCompositionStart = () => {
213
240
  this.#isPendingDk = true;
214
241
  };
@@ -236,6 +263,36 @@ defineCustomElement('sinch-textarea', class extends NectaryElement {
236
263
  }));
237
264
  }
238
265
  };
266
+ #onDragStart = e => {
267
+ if (this.#dragStartY !== 0) {
268
+ return;
269
+ }
270
+ this.#dragStartY = e.clientY;
271
+ this.#prevContentHeight = getRect(this.#$input).height;
272
+ window.addEventListener('mousemove', this.#onDragResize, {
273
+ signal: this.#controller.signal,
274
+ capture: true
275
+ });
276
+ window.addEventListener('mouseup', this.#onDragEnd, {
277
+ signal: this.#controller.signal,
278
+ capture: true
279
+ });
280
+ };
281
+ #onDragResize = e => {
282
+ const dy = e.clientY - this.#dragStartY;
283
+ const height = Math.round(Math.max(0, this.#prevContentHeight + dy));
284
+ this.#$input.style.setProperty('height', `${height}px`);
285
+ };
286
+ #onDragEnd = () => {
287
+ window.removeEventListener('mousemove', this.#onDragResize, {
288
+ capture: true
289
+ });
290
+ window.removeEventListener('mouseup', this.#onDragEnd, {
291
+ capture: true
292
+ });
293
+ this.#dragStartY = 0;
294
+ this.#prevContentHeight = 0;
295
+ };
239
296
  #onInputFocus = () => {
240
297
  this.dispatchEvent(new CustomEvent('-focus'));
241
298
  };
package/tooltip/index.js CHANGED
@@ -63,7 +63,7 @@ defineCustomElement('sinch-tooltip', class extends NectaryElement {
63
63
  this.#controller = null;
64
64
  }
65
65
  static get observedAttributes() {
66
- return ['text', 'orientation', 'type'];
66
+ return ['text', 'orientation', 'type', 'aria-label', 'aria-description'];
67
67
  }
68
68
  attributeChangedCallback(name, _, newVal) {
69
69
  switch (name) {
@@ -88,6 +88,12 @@ defineCustomElement('sinch-tooltip', class extends NectaryElement {
88
88
  });
89
89
  break;
90
90
  }
91
+ case 'aria-label':
92
+ case 'aria-description':
93
+ {
94
+ updateAttribute(this.#$pop, name, newVal);
95
+ break;
96
+ }
91
97
  }
92
98
  }
93
99
  get text() {
@@ -1 +1,19 @@
1
- export declare const parseMarkdown: (md: string) => string;
1
+ export type TMarkdownInlineParams = {
2
+ isBold?: boolean;
3
+ isItalic?: boolean;
4
+ isStrikethrough?: boolean;
5
+ };
6
+ export type TMarkdownParseVisitor = {
7
+ link(text: string, href: string): void;
8
+ emoji(emojiChar: string): void;
9
+ codetag(text: string): void;
10
+ inline(text: string, params: TMarkdownInlineParams): void;
11
+ linebreak(): void;
12
+ paragraph(): void;
13
+ list(isOrdered: boolean): void;
14
+ endList(): void;
15
+ listItem(): void;
16
+ end(): Node;
17
+ };
18
+ export declare const isEmojiString: (data: string) => boolean;
19
+ export declare const parseMarkdown: (md: string, visitor: TMarkdownParseVisitor) => Node;
package/utils/markdown.js CHANGED
@@ -1,128 +1,143 @@
1
- import { filterXSS } from 'xss';
2
- const contextTags = {
3
- em1: ['<span class="em1">', '</span>'],
4
- em2: ['<span class="em2">', '</span>'],
5
- em3: ['<span class="em3">', '</span>'],
6
- s: ['<span class="strikethrough">', '</span>'],
7
- p: ['<p class="paragraph">', '</p>']
8
- };
9
- const getContextTags = token => {
10
- switch (token) {
11
- case '_':
12
- case '*':
13
- return contextTags.em1;
14
- case '__':
15
- case '**':
16
- return contextTags.em2;
17
- case '___':
18
- case '***':
19
- return contextTags.em3;
20
- case '~~':
21
- return contextTags.s;
22
- case 'p':
23
- return contextTags.p;
24
- default:
25
- return null;
1
+ const regLinebreak = /(?:<br>\n|<br>|\n)/;
2
+ const regParagraph = /\n{2,}/;
3
+ const regEm3Star = /\*\*\*(?<em3>.+?)\*\*\*/;
4
+ const regEm2Star = /\*\*(?<em2>.+?)\*\*/;
5
+ const regEm1Star = /\*(?<em1>.+?)\*/;
6
+ const regEm3Underscore = /___(?<em3>.+?)___/;
7
+ const regEm2Underscore = /__(?<em2>.+?)__/;
8
+ const regEm1Underscore = /_(?<em1>.+?)_/;
9
+ const regCodeTag = /`(?<code>.+?)`/;
10
+ const regStrikethrough = /~~(?<strike>.+?)~~/;
11
+ const regLink = /!?\[(?<linktext>[^\]]*?)\]\((?<linkhref>[^)]+?)\)/;
12
+ const regEmoji = /(?<emoji>(?![0-9*#])\p{Emoji})/u;
13
+ const regUList = /^(?<indent>[\t ]*?)[*+-][\t ]+(?<ultext>.*?)[\t ]*?$/;
14
+ const regOList = /^(?<indent>[\t ]*?)\d+\.[\t ]+(?<oltext>.*?)[\t ]*?$/;
15
+ const allRegs = [regCodeTag, regLink, regEm3Star, regEm2Star, regEm1Star, regEm3Underscore, regEm2Underscore, regEm1Underscore, regStrikethrough, regEmoji];
16
+ export const isEmojiString = data => regEmoji.test(data);
17
+ const excludeRegs = function (regs) {
18
+ for (var _len = arguments.length, excluding = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
19
+ excluding[_key - 1] = arguments[_key];
26
20
  }
21
+ return regs.filter(r => !excluding.includes(r));
27
22
  };
28
- const encodeAttr = str => {
29
- return `${str}`.replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
30
- };
31
- const tokenizer = /(?<em>___|\*\*\*|__|\*\*|[_*]|~~)|(?:!?\[(?<linktext>[^\]]*?)\]\((?<linkhref>[^)]+?)\))|(?:`(?<code>[^`].*?)`)|(?<paragraph>\n{2,})|(?<newline>\n)/gm;
32
- export const parseMarkdown = md => {
33
- const context = [];
34
- let out = '';
35
- let lastIndex = 0;
36
- const isContextEnd = token => {
37
- return context.length > 0 && context.at(-1).token === token;
38
- };
39
- const pushContext = token => {
40
- const desc = getContextTags(token);
41
- if (desc === null) {
42
- return;
23
+ const matchClosest = (regs, line) => {
24
+ return regs.map(reg => reg.exec(line)).reduce((res, match) => {
25
+ if (match === null) {
26
+ return res;
43
27
  }
44
- context.push({
45
- token,
46
- out: ''
47
- });
48
- };
49
- const popContext = (token, prev) => {
50
- const desc = getContextTags(token);
51
- if (desc === null) {
52
- return token;
53
- }
54
- const ctx = context.pop();
55
- return `${desc[0]}${ctx.out}${prev}${desc[1]}`;
56
- };
57
- const flushContext = prev => {
58
- let str = '';
59
- while (context.length > 1) {
60
- const ctx = context.pop();
61
- str = `${ctx.token}${ctx.out}${str}`;
28
+ if (res === null || res.index > match.index) {
29
+ return match;
62
30
  }
63
- if (context.length === 0) {
64
- pushContext('p');
65
- }
66
- return popContext(context.at(-1).token, str + prev);
67
- };
68
- md = md.trim();
69
- while (true) {
70
- const tokens = tokenizer.exec(md);
71
- if (tokens === null) {
72
- break;
31
+ return res;
32
+ }, null);
33
+ };
34
+ const INITIAL_CONTEXT = {
35
+ isBold: false,
36
+ isItalic: false,
37
+ isStrikethrough: false
38
+ };
39
+ const createLineParser = visitor => function parseLine(regs, md) {
40
+ let context = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : INITIAL_CONTEXT;
41
+ let line = md;
42
+ let match = null;
43
+ while ((match = matchClosest(regs, line)) !== null) {
44
+ const groups = match.groups;
45
+ const matchedStr = match[0];
46
+ if (match.index > 0) {
47
+ visitor.inline(line.substring(0, match.index), context);
73
48
  }
74
- let prev = md.substring(lastIndex, tokens.index);
75
- let chunk = tokens[0];
76
- const groups = tokens.groups;
77
- lastIndex = tokenizer.lastIndex;
78
- if (context.length === 0) {
79
- pushContext('p');
49
+ line = line.substring(match.index + matchedStr.length);
50
+ if (groups.linkhref != null) {
51
+ visitor.link(groups.linktext, groups.linkhref);
80
52
  }
81
- if (prev.match(/[^\\](\\\\)*\\$/) != null) {
82
- prev = prev.substring(0, prev.length - 1);
83
- context.at(-1).out += prev + chunk;
84
- continue;
53
+ if (groups.code != null) {
54
+ visitor.codetag(groups.code);
85
55
  }
86
- if (groups.em != null) {
87
- const isCloseTag = isContextEnd(groups.em);
88
- if (isCloseTag) {
89
- chunk = popContext(groups.em, prev);
90
- context.at(-1).out += chunk;
91
- continue;
92
- }
93
- context.at(-1).out += prev;
94
- pushContext(groups.em);
95
- continue;
56
+ if (groups.emoji != null) {
57
+ visitor.emoji(groups.emoji);
96
58
  }
97
- if (groups.linkhref != null) {
98
- const href = groups.linkhref;
99
- const text = groups.linktext.length > 0 ? groups.linktext : href;
100
- chunk = `<a class="link" target="_blank" href="${encodeAttr(href)}">${encodeAttr(text)}</a>`;
101
- context.at(-1).out += prev + chunk;
102
- continue;
59
+ if (groups.em1 != null) {
60
+ parseLine(excludeRegs(regs, regEm1Star, regEm1Underscore), groups.em1, {
61
+ ...context,
62
+ isItalic: true
63
+ });
103
64
  }
104
- if (groups.code != null) {
105
- chunk = `<span class="code">${encodeAttr(groups.code)}</span>`;
106
- context.at(-1).out += prev + chunk;
107
- continue;
65
+ if (groups.em2 != null) {
66
+ parseLine(excludeRegs(regs, regEm2Star, regEm2Underscore), groups.em2, {
67
+ ...context,
68
+ isBold: true
69
+ });
108
70
  }
109
- if (groups.newline != null) {
110
- chunk = '<br/>';
111
- context.at(-1).out += prev + chunk;
112
- continue;
71
+ if (groups.em3 != null) {
72
+ parseLine(excludeRegs(regs, regEm3Star, regEm3Underscore, regEm2Star, regEm2Underscore, regEm1Star, regEm1Underscore), groups.em3, {
73
+ ...context,
74
+ isBold: true,
75
+ isItalic: true
76
+ });
113
77
  }
114
- if (groups.paragraph != null) {
115
- out += flushContext(prev);
116
- continue;
78
+ if (groups.strike != null) {
79
+ parseLine(excludeRegs(regs, regStrikethrough), groups.strike, {
80
+ ...context,
81
+ isStrikethrough: true
82
+ });
117
83
  }
118
84
  }
119
- out = (out + flushContext(md.substring(lastIndex))).trim();
120
- return filterXSS(out, {
121
- whiteList: {
122
- p: ['class'],
123
- span: ['class'],
124
- a: ['href', 'class', 'target'],
125
- br: []
85
+ if (line.length > 0) {
86
+ visitor.inline(line, context);
87
+ }
88
+ };
89
+ const createBlockParser = visitor => {
90
+ const parseLine = createLineParser(visitor);
91
+ const listRegs = [regUList, regOList];
92
+ const getListItemText = match => {
93
+ return match.groups.ultext ?? match.groups.oltext;
94
+ };
95
+ const BLOCK_MODE_PARAGRAPH = 1;
96
+ const BLOCK_MODE_ULIST = 2;
97
+ const BLOCK_MODE_OLIST = 2;
98
+ return function parseBlock(lines) {
99
+ let mode = 0;
100
+ for (let i = 0; i < lines.length; ++i) {
101
+ const listMatch = matchClosest(listRegs, lines[i]);
102
+ const isListLine = listMatch !== null;
103
+ if (isListLine) {
104
+ const groups = listMatch.groups;
105
+ const isOrderedList = groups.oltext != null;
106
+ mode = isOrderedList ? BLOCK_MODE_OLIST : BLOCK_MODE_ULIST;
107
+ visitor.list(isOrderedList);
108
+ const listItemInitialIndent = groups.indent;
109
+ let listItemLines = [getListItemText(listMatch)];
110
+ visitor.listItem();
111
+ for (++i; i < lines.length; ++i) {
112
+ const listItemMatch = isOrderedList ? regOList.exec(lines[i]) : regUList.exec(lines[i]);
113
+ if (listItemMatch !== null && listItemMatch.groups.indent === listItemInitialIndent) {
114
+ parseBlock(listItemLines);
115
+ listItemLines = [getListItemText(listItemMatch)];
116
+ visitor.listItem();
117
+ } else {
118
+ listItemLines.push(lines[i]);
119
+ }
120
+ }
121
+ parseBlock(listItemLines);
122
+ visitor.endList();
123
+ mode = 0;
124
+ } else {
125
+ if (mode === BLOCK_MODE_PARAGRAPH) {
126
+ visitor.linebreak();
127
+ }
128
+ if (mode !== BLOCK_MODE_PARAGRAPH) {
129
+ mode = BLOCK_MODE_PARAGRAPH;
130
+ visitor.paragraph();
131
+ }
132
+ parseLine(allRegs, lines[i]);
133
+ }
126
134
  }
127
- });
135
+ };
136
+ };
137
+ export const parseMarkdown = (md, visitor) => {
138
+ const parseBlock = createBlockParser(visitor);
139
+ for (const block of md.trim().split(regParagraph)) {
140
+ parseBlock(block.trim().split(regLinebreak));
141
+ }
142
+ return visitor.end();
128
143
  };