securemark 0.282.0 → 0.283.1

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.
Files changed (71) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/design.md +5 -9
  3. package/dist/index.js +7373 -7195
  4. package/markdown.d.ts +2 -2
  5. package/package.json +2 -2
  6. package/src/combinator/control/manipulation/convert.ts +4 -8
  7. package/src/combinator/control/manipulation/indent.ts +3 -5
  8. package/src/combinator/control/manipulation/scope.ts +6 -3
  9. package/src/combinator/control/manipulation/surround.ts +31 -7
  10. package/src/combinator/data/parser/context.test.ts +6 -6
  11. package/src/combinator/data/parser/context.ts +25 -39
  12. package/src/combinator/data/parser.ts +2 -3
  13. package/src/parser/api/bind.ts +6 -6
  14. package/src/parser/api/parse.test.ts +35 -28
  15. package/src/parser/api/parse.ts +0 -3
  16. package/src/parser/block/blockquote.ts +4 -3
  17. package/src/parser/block/dlist.test.ts +1 -1
  18. package/src/parser/block/dlist.ts +6 -6
  19. package/src/parser/block/extension/aside.ts +4 -3
  20. package/src/parser/block/extension/example.ts +4 -3
  21. package/src/parser/block/extension/table.ts +7 -7
  22. package/src/parser/block/heading.ts +3 -2
  23. package/src/parser/block/ilist.ts +2 -1
  24. package/src/parser/block/olist.ts +4 -3
  25. package/src/parser/block/reply/cite.ts +3 -3
  26. package/src/parser/block/reply/quote.ts +3 -3
  27. package/src/parser/block/sidefence.ts +2 -1
  28. package/src/parser/block/table.ts +9 -9
  29. package/src/parser/block/ulist.ts +6 -5
  30. package/src/parser/block.ts +16 -5
  31. package/src/parser/context.ts +19 -21
  32. package/src/parser/inline/annotation.test.ts +3 -1
  33. package/src/parser/inline/annotation.ts +6 -5
  34. package/src/parser/inline/autolink/email.ts +2 -1
  35. package/src/parser/inline/autolink/url.ts +6 -5
  36. package/src/parser/inline/autolink.ts +2 -2
  37. package/src/parser/inline/bracket.ts +17 -17
  38. package/src/parser/inline/code.test.ts +1 -1
  39. package/src/parser/inline/code.ts +2 -1
  40. package/src/parser/inline/deletion.ts +3 -3
  41. package/src/parser/inline/emphasis.ts +3 -3
  42. package/src/parser/inline/emstrong.ts +3 -3
  43. package/src/parser/inline/extension/index.ts +22 -10
  44. package/src/parser/inline/extension/indexee.ts +2 -2
  45. package/src/parser/inline/extension/indexer.ts +2 -1
  46. package/src/parser/inline/extension/label.ts +2 -2
  47. package/src/parser/inline/extension/placeholder.ts +4 -4
  48. package/src/parser/inline/html.ts +2 -2
  49. package/src/parser/inline/htmlentity.ts +2 -1
  50. package/src/parser/inline/insertion.ts +3 -3
  51. package/src/parser/inline/link.test.ts +2 -2
  52. package/src/parser/inline/link.ts +12 -8
  53. package/src/parser/inline/mark.ts +3 -3
  54. package/src/parser/inline/math.test.ts +3 -3
  55. package/src/parser/inline/math.ts +6 -5
  56. package/src/parser/inline/media.test.ts +1 -1
  57. package/src/parser/inline/media.ts +16 -11
  58. package/src/parser/inline/reference.test.ts +3 -0
  59. package/src/parser/inline/reference.ts +7 -6
  60. package/src/parser/inline/remark.ts +2 -2
  61. package/src/parser/inline/ruby.ts +13 -12
  62. package/src/parser/inline/strong.ts +3 -3
  63. package/src/parser/inline/template.ts +11 -9
  64. package/src/parser/inline.test.ts +1 -2
  65. package/src/parser/inline.ts +1 -0
  66. package/src/parser/source/escapable.ts +2 -1
  67. package/src/parser/source/str.ts +3 -2
  68. package/src/parser/source/text.ts +2 -1
  69. package/src/parser/source/unescapable.ts +2 -1
  70. package/src/util/quote.ts +6 -8
  71. package/src/combinator/data/parser/context/memo.ts +0 -57
@@ -3,16 +3,16 @@ import { union, inits, some, syntax, creation, precedence, constraint, validate,
3
3
  import { inline } from '../../inline';
4
4
  import { indexee, identity } from './indexee';
5
5
  import { txt, str } from '../../source';
6
- import { Syntax, State } from '../../context';
6
+ import { State, Recursion, Backtrack } from '../../context';
7
7
  import { startTight, trimNodeEnd } from '../../visibility';
8
8
  import { html, define, defrag } from 'typed-dom/dom';
9
9
 
10
10
  import IndexParser = ExtensionParser.IndexParser;
11
11
 
12
- export const index: IndexParser = lazy(() => validate('[#', creation(fmap(indexee(surround(
12
+ export const index: IndexParser = lazy(() => validate('[#', creation(1, Recursion.ignore, fmap(indexee(surround(
13
13
  '[#',
14
14
  constraint(State.index, false,
15
- syntax(Syntax.index, 2, State.linkers | State.media,
15
+ syntax(2, State.linkers | State.media,
16
16
  startTight(
17
17
  some(inits([
18
18
  inline,
@@ -20,7 +20,8 @@ export const index: IndexParser = lazy(() => validate('[#', creation(fmap(indexe
20
20
  ]), ']', [[/^\\?\n/, 9], [']', 2]])))),
21
21
  ']',
22
22
  false,
23
- ([, ns], rest) => [[html('a', trimNodeEnd(defrag(ns)))], rest])),
23
+ ([, ns], rest) => [[html('a', { 'data-index': dataindex(ns) }, trimNodeEnd(defrag(ns)))], rest],
24
+ undefined, 1 | Backtrack.bracket)),
24
25
  ([el]: [HTMLAnchorElement]) => [
25
26
  define(el,
26
27
  {
@@ -30,16 +31,27 @@ export const index: IndexParser = lazy(() => validate('[#', creation(fmap(indexe
30
31
  }),
31
32
  ]))));
32
33
 
33
- export const signature: IndexParser.SignatureParser = lazy(() => validate('|', creation(fmap(open(
34
+ export const signature: IndexParser.SignatureParser = lazy(() => validate('|', creation(1, Recursion.ignore, fmap(open(
34
35
  '|',
35
36
  startTight(some(union([bracket, txt]), ']'))),
36
37
  ns => [
37
38
  html('span', { class: 'indexer', 'data-index': identity('index', undefined, ns.join(''))!.slice(7) }),
38
39
  ]))));
39
40
 
40
- const bracket: IndexParser.SignatureParser.BracketParser = lazy(() => creation(union([
41
- surround(str('('), some(union([bracket, txt]), ')'), str(')'), true),
42
- surround(str('['), some(union([bracket, txt]), ']'), str(']'), true),
43
- surround(str('{'), some(union([bracket, txt]), '}'), str('}'), true),
44
- surround(str('"'), precedence(3, some(txt, '"')), str('"'), true),
41
+ const bracket: IndexParser.SignatureParser.BracketParser = lazy(() => creation(0, Recursion.terminal, union([
42
+ surround(str('('), some(union([bracket, txt]), ')'), str(')'), true, undefined, undefined, 3 | Backtrack.index),
43
+ surround(str('['), some(union([bracket, txt]), ']'), str(']'), true, undefined, undefined, 3 | Backtrack.index),
44
+ surround(str('{'), some(union([bracket, txt]), '}'), str('}'), true, undefined, undefined, 3 | Backtrack.index),
45
+ surround(str('"'), precedence(3, some(txt, '"')), str('"'), true, undefined, undefined, 3 | Backtrack.index),
45
46
  ])));
47
+
48
+ export function dataindex(ns: readonly (string | HTMLElement)[]): string | undefined {
49
+ if (ns.length === 0) return;
50
+ for (let i = ns.length - 1; i >= 0; --i) {
51
+ const node = ns[i];
52
+ if (typeof node === 'string') return;
53
+ if (i === ns.length - 1 && ['UL', 'OL'].includes(node.tagName)) continue;
54
+ if (!node.classList.contains('indexer')) return;
55
+ return node.getAttribute('data-index') ?? undefined;
56
+ }
57
+ }
@@ -6,7 +6,7 @@ import { define } from 'typed-dom/dom';
6
6
 
7
7
  export function indexee<P extends Parser<unknown, MarkdownParser.Context>>(parser: P): P;
8
8
  export function indexee(parser: Parser<HTMLElement, MarkdownParser.Context>): Parser<HTMLElement> {
9
- return fmap(parser, ([el], _, { id }) => [define(el, { id: identity('index', id, el) })]);
9
+ return fmap(parser, ([el], _, { id }) => [define(el, { id: identity('index', id, el), 'data-index': null })]);
10
10
  }
11
11
 
12
12
  const MAX = 60;
@@ -27,7 +27,7 @@ export function identity(
27
27
  assert(id?.match(/^[0-9a-z/-]*$/i) ?? true);
28
28
  if (id === '') return undefined;
29
29
  if (typeof text !== 'string') {
30
- const index = text.querySelector(':scope > .indexer')?.getAttribute('data-index');
30
+ const index = text.getAttribute('data-index') ?? undefined;
31
31
  if (index === '' && text.tagName === 'LI') return undefined;
32
32
  return index
33
33
  ? `${type}:${id ?? ''}:${index}`
@@ -1,12 +1,13 @@
1
1
  import { ExtensionParser } from '../../inline';
2
2
  import { union, creation, focus, surround } from '../../../combinator';
3
3
  import { signature } from './index';
4
+ import { Recursion } from '../../context';
4
5
  import { html } from 'typed-dom/dom';
5
6
 
6
7
  export const indexer: ExtensionParser.IndexerParser = surround(
7
8
  /^\s+\[(?=\|\S)/,
8
9
  union([
9
10
  signature,
10
- creation(focus(/^\|(?=\])/, () => [[html('span', { class: 'indexer', 'data-index': '' })], ''])),
11
+ creation(1, Recursion.ignore, focus(/^\|(?=\])/, () => [[html('span', { class: 'indexer', 'data-index': '' })], ''])),
11
12
  ]),
12
13
  /^\]\s*$/);
@@ -1,7 +1,7 @@
1
1
  import { ExtensionParser } from '../../inline';
2
2
  import { union, constraint, creation, validate, surround, clear, fmap } from '../../../combinator';
3
3
  import { str } from '../../source';
4
- import { State } from '../../context';
4
+ import { State, Recursion } from '../../context';
5
5
  import { html } from 'typed-dom/dom';
6
6
 
7
7
  const body = str(/^\$[A-Za-z]*(?:(?:-[A-Za-z][0-9A-Za-z]*)+|-(?:(?:0|[1-9][0-9]*)\.)*(?:0|[1-9][0-9]*)(?![0-9A-Za-z]))/);
@@ -11,7 +11,7 @@ export const segment: ExtensionParser.LabelParser.SegmentParser = clear(validate
11
11
  body,
12
12
  ])));
13
13
 
14
- export const label: ExtensionParser.LabelParser = validate(['[$', '$'], creation(fmap(
14
+ export const label: ExtensionParser.LabelParser = validate(['[$', '$'], creation(1, Recursion.ignore, fmap(
15
15
  constraint(State.label, false,
16
16
  union([
17
17
  surround('[', body, ']'),
@@ -2,7 +2,7 @@ import { ExtensionParser } from '../../inline';
2
2
  import { union, some, syntax, creation, validate, surround, lazy } from '../../../combinator';
3
3
  import { inline } from '../../inline';
4
4
  import { str } from '../../source';
5
- import { Syntax, State } from '../../context';
5
+ import { State, Recursion, Backtrack } from '../../context';
6
6
  import { startTight } from '../../visibility';
7
7
  import { unshift } from 'spica/array';
8
8
  import { html, defrag } from 'typed-dom/dom';
@@ -11,9 +11,9 @@ import { html, defrag } from 'typed-dom/dom';
11
11
 
12
12
  // All syntax surrounded by square brackets shouldn't contain line breaks.
13
13
 
14
- export const placeholder: ExtensionParser.PlaceholderParser = lazy(() => validate('[', creation(surround(
14
+ export const placeholder: ExtensionParser.PlaceholderParser = lazy(() => validate('[', creation(1, Recursion.inline, surround(
15
15
  str(/^\[[:^|]/),
16
- syntax(Syntax.placeholder, 2, State.none,
16
+ syntax(2, State.none,
17
17
  startTight(some(union([inline]), ']', [[']', 2]]))),
18
18
  str(']'), false,
19
19
  ([, bs], rest) => [[
@@ -24,4 +24,4 @@ export const placeholder: ExtensionParser.PlaceholderParser = lazy(() => validat
24
24
  'data-invalid-message': `Invalid start symbol or linebreak`,
25
25
  }, defrag(bs)),
26
26
  ], rest],
27
- ([as, bs], rest) => [unshift(as, bs), rest]))));
27
+ ([as, bs], rest) => [unshift(as, bs), rest], 3 | Backtrack.bracket))));
@@ -2,7 +2,7 @@ import { HTMLParser } from '../inline';
2
2
  import { union, subsequence, some, syntax, creation, validate, focus, surround, open, match, lazy } from '../../combinator';
3
3
  import { inline } from '../inline';
4
4
  import { str } from '../source';
5
- import { Syntax, State } from '../context';
5
+ import { State, Recursion } from '../context';
6
6
  import { isStartLooseNodes, blankWith } from '../visibility';
7
7
  import { memoize } from 'spica/memoize';
8
8
  import { Clock } from 'spica/clock';
@@ -18,7 +18,7 @@ const attrspecs = {
18
18
  Object.setPrototypeOf(attrspecs, null);
19
19
  Object.values(attrspecs).forEach(o => Object.setPrototypeOf(o, null));
20
20
 
21
- export const html: HTMLParser = lazy(() => validate('<', validate(/^<[a-z]+(?=[^\S\n]|>)/i, creation(syntax(Syntax.none, 4, State.none, union([
21
+ export const html: HTMLParser = lazy(() => validate('<', validate(/^<[a-z]+(?=[^\S\n]|>)/i, creation(1, Recursion.inline, syntax(4, State.none, union([
22
22
  focus(
23
23
  /^<wbr[^\S\n]*>/i,
24
24
  () => [[h('wbr')], '']),
@@ -1,9 +1,10 @@
1
1
  import { HTMLEntityParser, UnsafeHTMLEntityParser } from '../inline';
2
2
  import { union, creation, validate, focus, fmap } from '../../combinator';
3
+ import { Recursion } from '../context';
3
4
  import { html } from 'typed-dom/dom';
4
5
  import { reduce } from 'spica/memoize';
5
6
 
6
- export const unsafehtmlentity: UnsafeHTMLEntityParser = creation(validate('&', focus(
7
+ export const unsafehtmlentity: UnsafeHTMLEntityParser = creation(1, Recursion.ignore, validate('&', focus(
7
8
  /^&[0-9A-Za-z]+;/,
8
9
  ({ source }) => [[parse(source) ?? `\x1B${source}`], ''])));
9
10
 
@@ -2,14 +2,14 @@ import { InsertionParser } from '../inline';
2
2
  import { union, some, syntax, creation, surround, open, lazy } from '../../combinator';
3
3
  import { inline } from '../inline';
4
4
  import { str } from '../source';
5
- import { Syntax, State } from '../context';
5
+ import { State, Recursion } from '../context';
6
6
  import { blankWith } from '../visibility';
7
7
  import { unshift } from 'spica/array';
8
8
  import { html, defrag } from 'typed-dom/dom';
9
9
 
10
- export const insertion: InsertionParser = lazy(() => creation(surround(
10
+ export const insertion: InsertionParser = lazy(() => creation(1, Recursion.inline, surround(
11
11
  str('++', '+'),
12
- syntax(Syntax.none, 1, State.none,
12
+ syntax(1, State.none,
13
13
  some(union([
14
14
  some(inline, blankWith('\n', '++')),
15
15
  open('\n', some(inline, '+'), true),
@@ -13,10 +13,10 @@ describe('Unit: parser/inline/link', () => {
13
13
  assert.deepStrictEqual(inspect(parser('[]{data:text/html;base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4K}')), [['<a class="invalid">data:text/html;base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4K</a>'], '']);
14
14
  assert.deepStrictEqual(inspect(parser('[]{any:alert}')), [['<a class="invalid">any:alert</a>'], '']);
15
15
  assert.deepStrictEqual(inspect(parser('[]{"}')), [['<a class="url" href="&quot;">"</a>'], '']);
16
- assert.deepStrictEqual(inspect(parser('[]{<}')), [['<a class="url" href="<">&lt;</a>'], '']);
16
+ assert.deepStrictEqual(inspect(parser('[]{<}')), [['<a class="url" href="&lt;">&lt;</a>'], '']);
17
17
  assert.deepStrictEqual(inspect(parser('[]{\\}')), [['<a class="url" href="\\">\\</a>'], '']);
18
18
  assert.deepStrictEqual(inspect(parser('[]{\\"}')), [['<a class="url" href="\\&quot;">\\"</a>'], '']);
19
- assert.deepStrictEqual(inspect(parser('[]{\\<}')), [['<a class="url" href="\\<">\\&lt;</a>'], '']);
19
+ assert.deepStrictEqual(inspect(parser('[]{\\<}')), [['<a class="url" href="\\&lt;">\\&lt;</a>'], '']);
20
20
  });
21
21
 
22
22
  it('fishing', () => {
@@ -4,7 +4,7 @@ import { union, inits, tails, sequence, some, constraint, syntax, creation, prec
4
4
  import { inline, media, shortmedia } from '../inline';
5
5
  import { attributes } from './html';
6
6
  import { linebreak, unescsource, str } from '../source';
7
- import { Syntax, State } from '../context';
7
+ import { State, Recursion, Backtrack } from '../context';
8
8
  import { trimBlankStart, trimNodeEnd } from '../visibility';
9
9
  import { stringify } from '../util';
10
10
  import { ReadonlyURL } from 'spica/url';
@@ -20,16 +20,20 @@ export const link: LinkParser = lazy(() => validate(['[', '{'], union([
20
20
  textlink,
21
21
  ])));
22
22
 
23
- export const textlink: LinkParser.TextLinkParser = lazy(() => creation(10,
23
+ export const textlink: LinkParser.TextLinkParser = lazy(() => creation(1, Recursion.ignore,
24
24
  constraint(State.link, false,
25
- syntax(Syntax.link, 2, State.linkers | State.media,
25
+ syntax(2, State.linkers | State.media,
26
26
  bind(reverse(tails([
27
27
  dup(surround(
28
28
  '[',
29
29
  trimBlankStart(some(union([inline]), ']', [[/^\\?\n/, 9], [']', 2]])),
30
30
  ']',
31
- true)),
32
- dup(surround(/^{(?![{}])/, inits([uri, some(option)]), /^[^\S\n]*}/)),
31
+ true, undefined, undefined, 1 | Backtrack.bracket)),
32
+ dup(surround(
33
+ /^{(?![{}])/,
34
+ inits([uri, some(option)]),
35
+ /^[^\S\n]*}/,
36
+ false, undefined, undefined, 3 | Backtrack.link)),
33
37
  ])),
34
38
  ([params, content = []]: [string[], (HTMLElement | string)[]], rest, context) => {
35
39
  assert(!html('div', content).querySelector('a, .media, .annotation, .reference'));
@@ -38,9 +42,9 @@ export const textlink: LinkParser.TextLinkParser = lazy(() => creation(10,
38
42
  return [[parse(content, params, context)], rest];
39
43
  })))));
40
44
 
41
- export const medialink: LinkParser.MediaLinkParser = lazy(() => creation(10,
45
+ export const medialink: LinkParser.MediaLinkParser = lazy(() => creation(1, Recursion.ignore,
42
46
  constraint(State.link | State.media, false,
43
- syntax(Syntax.link, 2, State.linkers,
47
+ syntax(2, State.linkers,
44
48
  bind(reverse(sequence([
45
49
  dup(surround(
46
50
  '[',
@@ -57,7 +61,7 @@ export const linemedialink: LinkParser.LineMediaLinkParser = surround(
57
61
  /^(?=[^\S\n]*(?:$|\n))/);
58
62
 
59
63
  export const unsafelink: LinkParser.UnsafeLinkParser = lazy(() =>
60
- creation(10, precedence(2,
64
+ creation(1, Recursion.ignore, precedence(2,
61
65
  bind(reverse(tails([
62
66
  dup(surround(
63
67
  '[',
@@ -4,14 +4,14 @@ import { inline } from '../inline';
4
4
  import { identity, signature } from './extension/indexee';
5
5
  import { str } from '../source';
6
6
  import { startTight, blankWith } from '../visibility';
7
- import { Syntax, State } from '../context';
7
+ import { State, Recursion } from '../context';
8
8
  import { unshift } from 'spica/array';
9
9
  import { html, define, defrag } from 'typed-dom/dom';
10
10
 
11
- export const mark: MarkParser = lazy(() => creation(surround(
11
+ export const mark: MarkParser = lazy(() => creation(1, Recursion.inline, surround(
12
12
  str('==', '='),
13
13
  constraint(State.mark, false,
14
- syntax(Syntax.none, 1, State.none,
14
+ syntax(1, State.none,
15
15
  startTight(some(union([
16
16
  some(inline, blankWith('=='), [[/^\\?\n/, 9]]),
17
17
  open(some(inline, '=', [[/^\\?\n/, 9]]), mark),
@@ -115,8 +115,8 @@ describe('Unit: parser/inline/math', () => {
115
115
  assert.deepStrictEqual(inspect(parser('$(0, 1]$)')), [['<span class="math" translate="no" data-src="$(0, 1]$">$(0, 1]$</span>'], ')']);
116
116
  assert.deepStrictEqual(inspect(parser('$\\{0,1\\}$')), [['<span class="math" translate="no" data-src="$\\{0,1\\}$">$\\{0,1\\}$</span>'], '']);
117
117
  assert.deepStrictEqual(inspect(parser('$n=1$')), [['<span class="math" translate="no" data-src="$n=1$">$n=1$</span>'], '']);
118
- assert.deepStrictEqual(inspect(parser('$n<m$')), [['<span class="math" translate="no" data-src="$n<m$">$n&lt;m$</span>'], '']);
119
- assert.deepStrictEqual(inspect(parser('$n>m$')), [['<span class="math" translate="no" data-src="$n>m$">$n&gt;m$</span>'], '']);
118
+ assert.deepStrictEqual(inspect(parser('$n<m$')), [['<span class="math" translate="no" data-src="$n&lt;m$">$n&lt;m$</span>'], '']);
119
+ assert.deepStrictEqual(inspect(parser('$n>m$')), [['<span class="math" translate="no" data-src="$n&gt;m$">$n&gt;m$</span>'], '']);
120
120
  assert.deepStrictEqual(inspect(parser('$E = mc^2$')), [['<span class="math" translate="no" data-src="$E = mc^2$">$E = mc^2$</span>'], '']);
121
121
  assert.deepStrictEqual(inspect(parser('$f(x)$')), [['<span class="math" translate="no" data-src="$f(x)$">$f(x)$</span>'], '']);
122
122
  assert.deepStrictEqual(inspect(parser('$f: x \\to y$')), [['<span class="math" translate="no" data-src="$f: x \\to y$">$f: x \\to y$</span>'], '']);
@@ -143,7 +143,7 @@ describe('Unit: parser/inline/math', () => {
143
143
 
144
144
  it('nest', () => {
145
145
  assert.deepStrictEqual(inspect(parser('${*a*}$')), [['<span class="math" translate="no" data-src="${*a*}$">${*a*}$</span>'], '']);
146
- assert.deepStrictEqual(inspect(parser('${<wbr>}$')), [['<span class="math" translate="no" data-src="${<wbr>}$">${&lt;wbr&gt;}$</span>'], '']);
146
+ assert.deepStrictEqual(inspect(parser('${<wbr>}$')), [['<span class="math" translate="no" data-src="${&lt;wbr&gt;}$">${&lt;wbr&gt;}$</span>'], '']);
147
147
  });
148
148
 
149
149
  });
@@ -1,11 +1,12 @@
1
1
  import { MathParser } from '../inline';
2
2
  import { union, some, creation, precedence, validate, focus, rewrite, surround, lazy } from '../../combinator';
3
- import { escsource, unescsource } from '../source';
3
+ import { escsource, unescsource, str } from '../source';
4
+ import { Recursion } from '../context';
4
5
  import { html } from 'typed-dom/dom';
5
6
 
6
7
  const forbiddenCommand = /\\(?:begin|tiny|huge|large)(?![a-z])/i;
7
8
 
8
- export const math: MathParser = lazy(() => validate('$', creation(rewrite(
9
+ export const math: MathParser = lazy(() => validate('$', creation(1, Recursion.ignore, rewrite(
9
10
  union([
10
11
  surround('$', precedence(6, bracket), '$'),
11
12
  surround(
@@ -31,11 +32,11 @@ export const math: MathParser = lazy(() => validate('$', creation(rewrite(
31
32
  source)
32
33
  ], '']))));
33
34
 
34
- const bracket: MathParser.BracketParser = lazy(() => creation(surround(
35
- '{',
35
+ const bracket: MathParser.BracketParser = lazy(() => creation(0, Recursion.terminal, surround(
36
+ str('{'),
36
37
  some(union([
37
38
  bracket,
38
39
  some(escsource, /^(?:[{}$]|\\?\n)/),
39
40
  ])),
40
- '}',
41
+ str('}'),
41
42
  true)));
@@ -91,7 +91,7 @@ describe('Unit: parser/inline/media', () => {
91
91
  assert.deepStrictEqual(inspect(parser('![\\[]{/}')), [['<a href="/" target="_blank"><img class="media" data-src="/" alt="["></a>'], '']);
92
92
  assert.deepStrictEqual(inspect(parser('![\\"]{"?"#"}')), [['<a href="&quot;?&quot;#&quot;" target="_blank"><img class="media" data-src="&quot;?&quot;#&quot;" alt="&quot;"></a>'], '']);
93
93
  assert.deepStrictEqual(inspect(parser('![*a*]{/}')), [['<a href="/" target="_blank"><img class="media" data-src="/" alt="*a*"></a>'], '']);
94
- assert.deepStrictEqual(inspect(parser('![<wbr>]{/}')), [['<a href="/" target="_blank"><img class="media" data-src="/" alt="<wbr>"></a>'], '']);
94
+ assert.deepStrictEqual(inspect(parser('![<wbr>]{/}')), [['<a href="/" target="_blank"><img class="media" data-src="/" alt="&lt;wbr&gt;"></a>'], '']);
95
95
  });
96
96
 
97
97
  it('external', () => {
@@ -4,7 +4,7 @@ import { unsafelink, uri, option as linkoption, resolve } from './link';
4
4
  import { attributes } from './html';
5
5
  import { unsafehtmlentity } from './htmlentity';
6
6
  import { txt, linebreak, str } from '../source';
7
- import { Syntax, State } from '../context';
7
+ import { State, Recursion, Backtrack } from '../context';
8
8
  import { markInvalid } from '../util';
9
9
  import { ReadonlyURL } from 'spica/url';
10
10
  import { unshift, push } from 'spica/array';
@@ -18,17 +18,21 @@ const optspec = {
18
18
  } as const;
19
19
  Object.setPrototypeOf(optspec, null);
20
20
 
21
- export const media: MediaParser = lazy(() => validate(['![', '!{'], creation(10, open(
21
+ export const media: MediaParser = lazy(() => validate(['![', '!{'], creation(1, Recursion.ignore, open(
22
22
  '!',
23
23
  constraint(State.media, false,
24
- syntax(Syntax.none, 2, ~State.link,
24
+ syntax(2, ~State.link,
25
25
  bind(verify(fmap(tails([
26
26
  dup(surround(
27
27
  '[',
28
28
  some(union([unsafehtmlentity, bracket, txt]), ']', [[/^\\?\n/, 9]]),
29
29
  ']',
30
- true)),
31
- dup(surround(/^{(?![{}])/, inits([uri, some(option)]), /^[^\S\n]*}/)),
30
+ true, undefined, undefined, 1 | Backtrack.media)),
31
+ dup(surround(
32
+ /^{(?![{}])/,
33
+ inits([uri, some(option)]),
34
+ /^[^\S\n]*}/,
35
+ false, undefined, undefined, 3 | Backtrack.link)),
32
36
  ]),
33
37
  ([as, bs]) => bs ? [[as.join('').trim() || as.join('')], bs] : [[''], as]),
34
38
  ([[text]]) => text === '' || text.trim() !== ''),
@@ -55,7 +59,7 @@ export const media: MediaParser = lazy(() => validate(['![', '!{'], creation(10,
55
59
  el.style.aspectRatio = el.getAttribute('aspect-ratio')!;
56
60
  }
57
61
  if (context.state! & State.link) return [[el], rest];
58
- if (cache && cache.tagName !== 'IMG') return creation(10, _ => [[el!], rest])({ source: '!', context });
62
+ if (cache && cache.tagName !== 'IMG') return [[el], rest];
59
63
  return fmap(
60
64
  unsafelink as MediaParser,
61
65
  ([link]) => [define(link, { class: null, target: '_blank' }, [el])])
@@ -67,14 +71,15 @@ export const linemedia: MediaParser.LineMediaParser = surround(
67
71
  union([media]),
68
72
  /^(?=[^\S\n]*(?:$|\n))/);
69
73
 
70
- const bracket: MediaParser.TextParser.BracketParser = lazy(() => creation(union([
74
+ const bracket: MediaParser.TextParser.BracketParser = lazy(() => creation(0, Recursion.terminal, union([
71
75
  surround(str('('), some(union([unsafehtmlentity, bracket, txt]), ')'), str(')'), true,
72
- undefined, ([as, bs = []], rest) => [unshift(as, bs), rest]),
76
+ undefined, ([as, bs = []], rest) => [unshift(as, bs), rest], 3 | Backtrack.media),
73
77
  surround(str('['), some(union([unsafehtmlentity, bracket, txt]), ']'), str(']'), true,
74
- undefined, ([as, bs = []], rest) => [unshift(as, bs), rest]),
78
+ undefined, ([as, bs = []], rest) => [unshift(as, bs), rest], 3 | Backtrack.media),
75
79
  surround(str('{'), some(union([unsafehtmlentity, bracket, txt]), '}'), str('}'), true,
76
- undefined, ([as, bs = []], rest) => [unshift(as, bs), rest]),
77
- surround(str('"'), precedence(3, some(union([unsafehtmlentity, txt]), '"')), str('"'), true),
80
+ undefined, ([as, bs = []], rest) => [unshift(as, bs), rest], 3 | Backtrack.media),
81
+ surround(str('"'), precedence(3, some(union([unsafehtmlentity, txt]), '"')), str('"'), true,
82
+ undefined, undefined, 3 | Backtrack.media),
78
83
  ])));
79
84
 
80
85
  const option: MediaParser.ParameterParser.OptionParser = lazy(() => union([
@@ -13,6 +13,9 @@ describe('Unit: parser/inline/reference', () => {
13
13
  assert.deepStrictEqual(inspect(parser('[[')), undefined);
14
14
  assert.deepStrictEqual(inspect(parser('[[]]')), undefined);
15
15
  assert.deepStrictEqual(inspect(parser('[[]]]')), undefined);
16
+ assert.deepStrictEqual(inspect(parser('[["]]')), undefined);
17
+ assert.deepStrictEqual(inspect(parser('[[(]]')), undefined);
18
+ assert.deepStrictEqual(inspect(parser('[[<bdi>]]')), undefined);
16
19
  assert.deepStrictEqual(inspect(parser('[[ ]]')), undefined);
17
20
  assert.deepStrictEqual(inspect(parser('[[ [a')), undefined);
18
21
  assert.deepStrictEqual(inspect(parser('[[\n]]')), undefined);
@@ -2,24 +2,25 @@ import { ReferenceParser } from '../inline';
2
2
  import { union, subsequence, some, syntax, creation, constraint, surround, lazy } from '../../combinator';
3
3
  import { inline } from '../inline';
4
4
  import { str } from '../source';
5
- import { Syntax, State } from '../context';
5
+ import { State, Recursion, Backtrack } from '../context';
6
6
  import { blank, trimBlankStart, trimNodeEnd } from '../visibility';
7
7
  import { html, defrag } from 'typed-dom/dom';
8
8
 
9
- export const reference: ReferenceParser = lazy(() => creation(surround(
9
+ export const reference: ReferenceParser = lazy(() => creation(1, Recursion.ignore, surround(
10
10
  '[[',
11
11
  constraint(State.reference, false,
12
- syntax(Syntax.reference, 6, State.annotation | State.reference | State.media,
12
+ syntax(6, State.annotation | State.reference | State.media,
13
13
  subsequence([
14
14
  abbr,
15
- trimBlankStart(some(inline, ']', [[/^\\?\n/, 9], [']', 2], [']]', 6]])),
15
+ trimBlankStart(some(inline, ']', [[/^\\?\n/, 9], [']', 2]])),
16
16
  ]))),
17
17
  ']]',
18
18
  false,
19
- ([, ns], rest) => [[html('sup', attributes(ns), [html('span', trimNodeEnd(defrag(ns)))])], rest])));
19
+ ([, ns], rest) => [[html('sup', attributes(ns), [html('span', trimNodeEnd(defrag(ns)))])], rest],
20
+ undefined, 1 | Backtrack.bracket)));
20
21
 
21
22
  // Chicago-Style
22
- const abbr: ReferenceParser.AbbrParser = creation(surround(
23
+ const abbr: ReferenceParser.AbbrParser = creation(1, Recursion.ignore, surround(
23
24
  '^',
24
25
  union([str(/^(?=[A-Z])(?:[0-9A-Za-z]'?|(?:[-.:]|\.?\??,? ?)(?!['\-.:?, ]))+/)]),
25
26
  /^\|?(?=]])|^\|[^\S\n]*/,
@@ -2,12 +2,12 @@ import { RemarkParser } from '../inline';
2
2
  import { union, some, syntax, creation, validate, surround, open, close, match, lazy } from '../../combinator';
3
3
  import { inline } from '../inline';
4
4
  import { text, str } from '../source';
5
- import { Syntax, State } from '../context';
5
+ import { State, Recursion } from '../context';
6
6
  import { memoize } from 'spica/memoize';
7
7
  import { unshift, push } from 'spica/array';
8
8
  import { html, defrag } from 'typed-dom/dom';
9
9
 
10
- export const remark: RemarkParser = lazy(() => validate('[%', creation(syntax(Syntax.none, 5, State.none, match(
10
+ export const remark: RemarkParser = lazy(() => validate('[%', creation(1, Recursion.inline, syntax(5, State.none, match(
11
11
  /^\[(%+)\s/,
12
12
  memoize(
13
13
  ([, fence]) =>
@@ -1,25 +1,26 @@
1
1
  import { RubyParser } from '../inline';
2
2
  import { eval, exec } from '../../combinator/data/parser';
3
- import { sequence, syntax, creation, validate, verify, surround, lazy, fmap } from '../../combinator';
3
+ import { sequence, syntax, creation, validate, surround, lazy, fmap, bind } from '../../combinator';
4
4
  import { unsafehtmlentity } from './htmlentity';
5
5
  import { text as txt, str } from '../source';
6
- import { Syntax, State } from '../context';
6
+ import { State, Recursion, Backtrack } from '../context';
7
7
  import { isStartTightNodes } from '../visibility';
8
8
  import { unshift, push } from 'spica/array';
9
9
  import { html, defrag } from 'typed-dom/dom';
10
10
 
11
- export const ruby: RubyParser = lazy(() => validate('[', creation(syntax(Syntax.ruby, 2, State.all, fmap(verify(fmap(
11
+ export const ruby: RubyParser = lazy(() => validate('[', creation(1, Recursion.ignore, syntax(2, State.all, fmap(
12
12
  sequence([
13
- surround('[', str(/^(?:\\[^\n]|[^\\[\](){}"\n])+/), ']'),
14
- surround('(', str(/^(?:\\[^\n]|[^\\[\](){}"\n])+/), ')'),
13
+ bind(surround('[', str(/^(?:\\[^\n]|[^\\[\](){}"\n])+/), ']', false, undefined, undefined, 3 | Backtrack.ruby), ([source], rest, context) => {
14
+ const ns = eval(text({ source, context }), [undefined])[0];
15
+ ns && ns[ns.length - 1] === '' && ns.pop();
16
+ return ns && isStartTightNodes(ns) ? [[ns], rest] : undefined;
17
+ }),
18
+ bind(surround('(', str(/^(?:\\[^\n]|[^\\[\](){}"\n])+/), ')', false, undefined, undefined, 3 | Backtrack.ruby), ([source], rest, context) => {
19
+ const ns = eval(text({ source, context }), [undefined])[0];
20
+ return ns && [[ns], rest];
21
+ }),
15
22
  ]),
16
- ([texts, rubies], _, context) => [
17
- eval(text({ source: texts, context }), [])[0] ?? '',
18
- eval(text({ source: rubies, context }), [])[0] ?? '',
19
- ]),
20
- ([texts, rubies]) => texts && rubies && isStartTightNodes(texts)),
21
23
  ([texts, rubies]) => {
22
- texts[texts.length - 1] === '' && texts.pop();
23
24
  switch (true) {
24
25
  case rubies.length <= texts.length:
25
26
  return [
@@ -51,7 +52,7 @@ export const ruby: RubyParser = lazy(() => validate('[', creation(syntax(Syntax.
51
52
  }
52
53
  })))));
53
54
 
54
- const text: RubyParser.TextParser = creation(1, false, ({ source, context }) => {
55
+ const text: RubyParser.TextParser = creation(1, Recursion.ignore, ({ source, context }) => {
55
56
  const acc = [''];
56
57
  while (source !== '') {
57
58
  assert(source[0] !== '\n');
@@ -3,14 +3,14 @@ import { union, some, syntax, creation, surround, open, lazy } from '../../combi
3
3
  import { inline } from '../inline';
4
4
  import { emstrong } from './emstrong';
5
5
  import { str } from '../source';
6
- import { Syntax, State } from '../context';
6
+ import { State, Recursion } from '../context';
7
7
  import { startTight, blankWith } from '../visibility';
8
8
  import { unshift } from 'spica/array';
9
9
  import { html, defrag } from 'typed-dom/dom';
10
10
 
11
- export const strong: StrongParser = lazy(() => creation(surround(
11
+ export const strong: StrongParser = lazy(() => creation(1, Recursion.inline, surround(
12
12
  str('**', '*'),
13
- syntax(Syntax.none, 1, State.none,
13
+ syntax(1, State.none,
14
14
  startTight(some(union([
15
15
  some(inline, blankWith('**'), [[/^\\?\n/, 9]]),
16
16
  open(some(inline, '*', [[/^\\?\n/, 9]]), union([
@@ -1,23 +1,25 @@
1
1
  import { TemplateParser } from '../inline';
2
2
  import { union, some, syntax, creation, precedence, surround, lazy } from '../../combinator';
3
3
  import { escsource, str } from '../source';
4
- import { Syntax, State } from '../context';
4
+ import { State, Recursion, Backtrack } from '../context';
5
5
  import { unshift } from 'spica/array';
6
6
  import { html } from 'typed-dom/dom';
7
7
 
8
- export const template: TemplateParser = lazy(() => creation(surround(
8
+ export const template: TemplateParser = lazy(() => creation(1, Recursion.ignore, surround(
9
9
  '{{',
10
- syntax(Syntax.none, 6, State.all, some(union([bracket, escsource]), '}', [['}}', 6]])),
10
+ syntax(6, State.all, some(union([bracket, escsource]), '}', [['}}', 6]])),
11
11
  '}}',
12
12
  true,
13
- ([, ns = []], rest) => [[html('span', { class: 'template' }, `{{${ns.join('').replace(/\x1B/g, '')}}}`)], rest])));
13
+ ([, ns = []], rest) => [[html('span', { class: 'template' }, `{{${ns.join('').replace(/\x1B/g, '')}}}`)], rest],
14
+ undefined, 3 | Backtrack.template)));
14
15
 
15
- const bracket: TemplateParser.BracketParser = lazy(() => creation(union([
16
+ const bracket: TemplateParser.BracketParser = lazy(() => creation(0, Recursion.terminal, union([
16
17
  surround(str('('), some(union([bracket, escsource]), ')'), str(')'), true,
17
- undefined, ([as, bs = []], rest) => [unshift(as, bs), rest]),
18
+ undefined, ([as, bs = []], rest) => [unshift(as, bs), rest], 3 | Backtrack.template),
18
19
  surround(str('['), some(union([bracket, escsource]), ']'), str(']'), true,
19
- undefined, ([as, bs = []], rest) => [unshift(as, bs), rest]),
20
+ undefined, ([as, bs = []], rest) => [unshift(as, bs), rest], 3 | Backtrack.template),
20
21
  surround(str('{'), some(union([bracket, escsource]), '}'), str('}'), true,
21
- undefined, ([as, bs = []], rest) => [unshift(as, bs), rest]),
22
- surround(str('"'), precedence(3, some(escsource, /^"|^\\?\n/)), str('"'), true),
22
+ undefined, ([as, bs = []], rest) => [unshift(as, bs), rest], 3 | Backtrack.template),
23
+ surround(str('"'), precedence(3, some(escsource, /^"|^\\?\n/)), str('"'), true,
24
+ undefined, undefined, 3 | Backtrack.template),
23
25
  ])));
@@ -148,8 +148,8 @@ describe('Unit: parser/inline', () => {
148
148
  assert.deepStrictEqual(inspect(parser('(((a))')), [['(', '<sup class="annotation"><span>a</span></sup>'], '']);
149
149
  assert.deepStrictEqual(inspect(parser('((((a))')), [['(', '(', '<sup class="annotation"><span>a</span></sup>'], '']);
150
150
  assert.deepStrictEqual(inspect(parser('((((a))))')), [['<sup class="annotation"><span><span class="paren">((a))</span></span></sup>'], '']);
151
- assert.deepStrictEqual(inspect(parser('((<bdi>))')), [['<sup class="annotation"><span><span class="invalid">&lt;bdi&gt;</span></span></sup>'], '']);
152
151
  assert.deepStrictEqual(inspect(parser('((${))}$')), [['(', '(', '<span class="math" translate="no" data-src="${))}$">${))}$</span>'], '']);
152
+ assert.deepStrictEqual(inspect(parser('((a\nb))')), [['<span class="paren">(<span class="paren">(a<br>b)</span>)</span>'], '']);
153
153
  assert.deepStrictEqual(inspect(parser('"((""))')), [['"', '<sup class="annotation"><span>""</span></sup>'], '']);
154
154
  assert.deepStrictEqual(inspect(parser('[[[a]]')), [['[', '<sup class="reference"><span>a</span></sup>'], '']);
155
155
  assert.deepStrictEqual(inspect(parser('[[[[a]]')), [['[', '[', '<sup class="reference"><span>a</span></sup>'], '']);
@@ -158,7 +158,6 @@ describe('Unit: parser/inline', () => {
158
158
  assert.deepStrictEqual(inspect(parser('[[[]{a}]]')), [['<sup class="reference"><span><a class="url" href="a">a</a></span></sup>'], '']);
159
159
  assert.deepStrictEqual(inspect(parser('[[[a]{b}]]')), [['<sup class="reference"><span><a class="link" href="b">a</a></span></sup>'], '']);
160
160
  assert.deepStrictEqual(inspect(parser('[(([a]{#}))]{#}')), [['<a class="link" href="#"><span class="paren">(<span class="paren">([a]{#})</span>)</span></a>'], '']);
161
- assert.deepStrictEqual(inspect(parser('[[<bdi>]]')), [['<sup class="reference"><span><span class="invalid">&lt;bdi&gt;</span></span></sup>'], '']);
162
161
  assert.deepStrictEqual(inspect(parser('[[${]]}$')), [['[', '[', '<span class="math" translate="no" data-src="${]]}$">${]]}$</span>'], '']);
163
162
  assert.deepStrictEqual(inspect(parser('"[[""]]')), [['"', '<sup class="reference"><span>""</span></sup>'], '']);
164
163
  assert.deepStrictEqual(inspect(parser('[==a==]{b}')), [['<a class="link" href="b">==a==</a>'], '']);
@@ -70,6 +70,7 @@ export const inline: InlineParser = lazy(() => union([
70
70
 
71
71
  export { indexee } from './inline/extension/indexee';
72
72
  export { indexer } from './inline/extension/indexer';
73
+ export { dataindex } from './inline/extension/index';
73
74
  export { medialink } from './inline/link';
74
75
  export { media } from './inline/media';
75
76
  export { shortmedia } from './inline/shortmedia';
@@ -1,10 +1,11 @@
1
1
  import { EscapableSourceParser } from '../source';
2
2
  import { creation } from '../../combinator';
3
3
  import { nonWhitespace } from './text';
4
+ import { Recursion } from '../context';
4
5
 
5
6
  const delimiter = /[\s\x00-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]/;
6
7
 
7
- export const escsource: EscapableSourceParser = creation(1, false, ({ source, context }) => {
8
+ export const escsource: EscapableSourceParser = creation(1, Recursion.ignore, ({ source, context }) => {
8
9
  if (source === '') return;
9
10
  const i = source.search(delimiter);
10
11
  switch (i) {