securemark 0.291.1 → 0.292.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.
Files changed (164) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/design.md +13 -2
  3. package/dist/index.js +1093 -756
  4. package/markdown.d.ts +17 -17
  5. package/package.json +1 -1
  6. package/src/combinator/control/constraint/block.test.ts +8 -5
  7. package/src/combinator/control/constraint/block.ts +9 -9
  8. package/src/combinator/control/constraint/contract.ts +20 -28
  9. package/src/combinator/control/constraint/line.test.ts +9 -6
  10. package/src/combinator/control/constraint/line.ts +21 -22
  11. package/src/combinator/control/manipulation/convert.ts +29 -13
  12. package/src/combinator/control/manipulation/fence.ts +18 -15
  13. package/src/combinator/control/manipulation/indent.test.ts +17 -14
  14. package/src/combinator/control/manipulation/indent.ts +15 -8
  15. package/src/combinator/control/manipulation/match.ts +11 -10
  16. package/src/combinator/control/manipulation/recovery.ts +6 -2
  17. package/src/combinator/control/manipulation/scope.ts +37 -38
  18. package/src/combinator/control/manipulation/surround.ts +78 -60
  19. package/src/combinator/control/manipulation/trim.test.ts +12 -9
  20. package/src/combinator/control/monad/bind.ts +16 -16
  21. package/src/combinator/control/monad/fmap.ts +6 -6
  22. package/src/combinator/data/parser/context/delimiter.ts +8 -7
  23. package/src/combinator/data/parser/context.test.ts +19 -14
  24. package/src/combinator/data/parser/context.ts +20 -16
  25. package/src/combinator/data/parser/inits.ts +13 -14
  26. package/src/combinator/data/parser/sequence.test.ts +16 -15
  27. package/src/combinator/data/parser/sequence.ts +13 -14
  28. package/src/combinator/data/parser/some.test.ts +19 -18
  29. package/src/combinator/data/parser/some.ts +13 -15
  30. package/src/combinator/data/parser/subsequence.test.ts +22 -21
  31. package/src/combinator/data/parser/subsequence.ts +3 -3
  32. package/src/combinator/data/parser/tails.ts +3 -3
  33. package/src/combinator/data/parser/union.test.ts +16 -15
  34. package/src/combinator/data/parser/union.ts +2 -2
  35. package/src/combinator/data/parser.ts +66 -28
  36. package/src/debug.test.ts +3 -3
  37. package/src/parser/api/bind.ts +3 -3
  38. package/src/parser/api/header.ts +7 -6
  39. package/src/parser/api/normalize.ts +2 -2
  40. package/src/parser/api/parse.test.ts +14 -15
  41. package/src/parser/api/parse.ts +3 -3
  42. package/src/parser/autolink.test.ts +19 -17
  43. package/src/parser/block/blockquote.test.ts +86 -84
  44. package/src/parser/block/blockquote.ts +4 -2
  45. package/src/parser/block/codeblock.test.ts +58 -56
  46. package/src/parser/block/codeblock.ts +3 -3
  47. package/src/parser/block/dlist.test.ts +58 -56
  48. package/src/parser/block/extension/aside.test.ts +10 -8
  49. package/src/parser/block/extension/aside.ts +1 -1
  50. package/src/parser/block/extension/example.test.ts +20 -18
  51. package/src/parser/block/extension/example.ts +3 -3
  52. package/src/parser/block/extension/fig.test.ts +38 -36
  53. package/src/parser/block/extension/fig.ts +1 -1
  54. package/src/parser/block/extension/figbase.test.ts +17 -15
  55. package/src/parser/block/extension/figure.test.ts +64 -62
  56. package/src/parser/block/extension/figure.ts +3 -2
  57. package/src/parser/block/extension/message.test.ts +15 -13
  58. package/src/parser/block/extension/message.ts +3 -3
  59. package/src/parser/block/extension/placeholder.test.ts +3 -1
  60. package/src/parser/block/extension/table.test.ts +73 -71
  61. package/src/parser/block/extension/table.ts +5 -5
  62. package/src/parser/block/extension.test.ts +3 -1
  63. package/src/parser/block/heading.test.ts +65 -64
  64. package/src/parser/block/heading.ts +3 -3
  65. package/src/parser/block/ilist.test.ts +3 -1
  66. package/src/parser/block/ilist.ts +3 -3
  67. package/src/parser/block/mathblock.test.ts +33 -31
  68. package/src/parser/block/mathblock.ts +1 -1
  69. package/src/parser/block/mediablock.ts +2 -2
  70. package/src/parser/block/olist.test.ts +99 -97
  71. package/src/parser/block/olist.ts +2 -2
  72. package/src/parser/block/pagebreak.test.ts +17 -15
  73. package/src/parser/block/pagebreak.ts +1 -1
  74. package/src/parser/block/paragraph.test.ts +60 -57
  75. package/src/parser/block/reply/cite.test.ts +41 -39
  76. package/src/parser/block/reply/cite.ts +3 -3
  77. package/src/parser/block/reply/quote.test.ts +52 -50
  78. package/src/parser/block/reply.test.ts +21 -19
  79. package/src/parser/block/sidefence.test.ts +51 -49
  80. package/src/parser/block/table.test.ts +51 -50
  81. package/src/parser/block/table.ts +6 -6
  82. package/src/parser/block/ulist.test.ts +52 -50
  83. package/src/parser/block/ulist.ts +2 -2
  84. package/src/parser/block.ts +6 -5
  85. package/src/parser/context.ts +1 -0
  86. package/src/parser/header.test.ts +22 -21
  87. package/src/parser/header.ts +25 -13
  88. package/src/parser/inline/annotation.test.ts +44 -42
  89. package/src/parser/inline/annotation.ts +2 -2
  90. package/src/parser/inline/autolink/account.test.ts +32 -30
  91. package/src/parser/inline/autolink/account.ts +1 -1
  92. package/src/parser/inline/autolink/anchor.test.ts +23 -21
  93. package/src/parser/inline/autolink/anchor.ts +1 -1
  94. package/src/parser/inline/autolink/channel.test.ts +16 -14
  95. package/src/parser/inline/autolink/channel.ts +2 -2
  96. package/src/parser/inline/autolink/email.test.ts +38 -36
  97. package/src/parser/inline/autolink/email.ts +2 -2
  98. package/src/parser/inline/autolink/hashnum.test.ts +39 -37
  99. package/src/parser/inline/autolink/hashnum.ts +1 -1
  100. package/src/parser/inline/autolink/hashtag.test.ts +58 -56
  101. package/src/parser/inline/autolink/hashtag.ts +1 -1
  102. package/src/parser/inline/autolink/url.test.ts +76 -74
  103. package/src/parser/inline/autolink/url.ts +6 -6
  104. package/src/parser/inline/bracket.test.ts +69 -67
  105. package/src/parser/inline/bracket.ts +32 -32
  106. package/src/parser/inline/code.test.ts +32 -29
  107. package/src/parser/inline/code.ts +20 -13
  108. package/src/parser/inline/deletion.test.ts +29 -27
  109. package/src/parser/inline/deletion.ts +2 -2
  110. package/src/parser/inline/emphasis.test.ts +40 -36
  111. package/src/parser/inline/emphasis.ts +2 -2
  112. package/src/parser/inline/emstrong.test.ts +102 -96
  113. package/src/parser/inline/emstrong.ts +96 -36
  114. package/src/parser/inline/extension/index.test.ts +91 -89
  115. package/src/parser/inline/extension/index.ts +18 -30
  116. package/src/parser/inline/extension/indexee.ts +1 -1
  117. package/src/parser/inline/extension/indexer.test.ts +26 -24
  118. package/src/parser/inline/extension/indexer.ts +1 -1
  119. package/src/parser/inline/extension/label.test.ts +34 -32
  120. package/src/parser/inline/extension/placeholder.test.ts +44 -42
  121. package/src/parser/inline/extension/placeholder.ts +11 -8
  122. package/src/parser/inline/html.test.ts +108 -106
  123. package/src/parser/inline/html.ts +24 -23
  124. package/src/parser/inline/htmlentity.test.ts +39 -37
  125. package/src/parser/inline/htmlentity.ts +9 -3
  126. package/src/parser/inline/insertion.test.ts +29 -27
  127. package/src/parser/inline/insertion.ts +2 -2
  128. package/src/parser/inline/italic.test.ts +55 -53
  129. package/src/parser/inline/italic.ts +2 -2
  130. package/src/parser/inline/link.test.ts +187 -185
  131. package/src/parser/inline/link.ts +30 -12
  132. package/src/parser/inline/mark.test.ts +31 -29
  133. package/src/parser/inline/mark.ts +3 -3
  134. package/src/parser/inline/math.test.ts +133 -131
  135. package/src/parser/inline/math.ts +2 -2
  136. package/src/parser/inline/media.test.ts +93 -91
  137. package/src/parser/inline/media.ts +41 -17
  138. package/src/parser/inline/reference.test.ts +110 -108
  139. package/src/parser/inline/reference.ts +48 -39
  140. package/src/parser/inline/remark.test.ts +53 -51
  141. package/src/parser/inline/remark.ts +3 -3
  142. package/src/parser/inline/ruby.test.ts +46 -44
  143. package/src/parser/inline/ruby.ts +24 -27
  144. package/src/parser/inline/shortmedia.test.ts +11 -9
  145. package/src/parser/inline/strong.test.ts +37 -33
  146. package/src/parser/inline/strong.ts +6 -3
  147. package/src/parser/inline/template.test.ts +24 -22
  148. package/src/parser/inline/template.ts +20 -11
  149. package/src/parser/inline.test.ts +221 -220
  150. package/src/parser/inline.ts +13 -8
  151. package/src/parser/segment.ts +13 -8
  152. package/src/parser/source/escapable.test.ts +24 -22
  153. package/src/parser/source/escapable.ts +26 -41
  154. package/src/parser/source/line.test.ts +19 -17
  155. package/src/parser/source/line.ts +3 -3
  156. package/src/parser/source/str.ts +28 -11
  157. package/src/parser/source/text.test.ts +85 -83
  158. package/src/parser/source/text.ts +26 -54
  159. package/src/parser/source/unescapable.test.ts +24 -22
  160. package/src/parser/source/unescapable.ts +18 -33
  161. package/src/parser/source.ts +1 -1
  162. package/src/parser/util.ts +36 -33
  163. package/src/parser/visibility.ts +19 -15
  164. package/src/util/quote.ts +4 -2
@@ -1,66 +1,38 @@
1
1
  import { TextParser, TxtParser, LinebreakParser } from '../source';
2
2
  import { Command } from '../context';
3
3
  import { union, consume, focus } from '../../combinator';
4
- import { str } from './str';
5
4
  import { html } from 'typed-dom/dom';
6
5
 
7
- export const delimiter = /[\s\x00-\x7F()[]{}]|\S#|[0-9A-Za-z]>/u;
8
- export const nonWhitespace = /[\S\n]|$/u;
9
- export const nonAlphanumeric = /[^0-9A-Za-z]|\S#|[0-9A-Za-z]>|$/u;
10
- const repeat = str(/^(.)\1*/);
11
-
12
- export const text: TextParser = ({ source, context }) => {
13
- if (source === '') return;
14
- const i = source.search(delimiter);
15
- switch (i) {
16
- case -1:
17
- consume(source.length, context);
18
- return [[source], ''];
19
- case 0:
20
- consume(1, context);
21
- switch (source[0]) {
22
- case '\r':
23
- assert(!source.includes('\r', 1));
24
- consume(-1, context);
25
- return [[], source.slice(1)];
26
- case Command.Escape:
27
- case '\\':
28
- switch (source[1]) {
29
- case undefined:
30
- return [[], ''];
31
- case '\n':
32
- assert(source[0] !== Command.Escape);
33
- return [[], source.slice(1)];
34
- default:
35
- consume(1, context);
36
- return [[source.slice(1, 2)], source.slice(2)];
37
- }
6
+ export const text: TextParser = input => {
7
+ const { context } = input;
8
+ const { source, position } = context;
9
+ if (position === source.length) return;
10
+ consume(1, context);
11
+ context.position += 1;
12
+ switch (source[position]) {
13
+ case '\r':
14
+ assert(!source.includes('\r', position + 1));
15
+ consume(-1, context);
16
+ return [[]];
17
+ case Command.Escape:
18
+ case '\\':
19
+ switch (source[position + 1]) {
20
+ case undefined:
21
+ return [[]];
38
22
  case '\n':
39
- context.linebreak ||= source.length;
40
- return [[html('br')], source.slice(1)];
41
- case '*':
42
- case '`':
43
- return source[1] === source[0]
44
- ? repeat({ source, context })
45
- : [[source[0]], source.slice(1)];
23
+ assert(source[0] !== Command.Escape);
24
+ return [[]];
46
25
  default:
47
- assert(source[0] !== '\n');
48
- const b = source[0].trimStart() === '';
49
- const i = b || isAlphanumeric(source[0])
50
- ? source.search(b ? nonWhitespace : nonAlphanumeric) || 1
51
- : 1;
52
- assert(i > 0);
53
- assert(!['\\', '\n'].includes(source[0]));
54
- consume(i - 1, context);
55
- return b && i === source.length
56
- || b && source[i] === '\n'
57
- || b && source[i] === '\\' && source[i + 1] === '\n'
58
- ? [[], source.slice(i)]
59
- : [[source.slice(0, i - +b || 1)], source.slice(i - +b || 1)];
26
+ consume(1, context);
27
+ context.position += 1;
28
+ return [[source.slice(position + 1, position + 2)]];
60
29
  }
30
+ case '\n':
31
+ context.linebreak ||= source.length - position;
32
+ return [[html('br')]];
61
33
  default:
62
- consume(i, context);
63
- return [[source.slice(0, i)], source.slice(i)];
34
+ assert(source[position] !== '\n');
35
+ return [[source[position]]];
64
36
  }
65
37
  };
66
38
 
@@ -1,45 +1,47 @@
1
1
  import { unescsource } from './unescapable';
2
2
  import { some } from '../../combinator';
3
+ import { input } from '../../combinator/data/parser';
3
4
  import { inspect } from '../../debug.test';
4
5
 
5
6
  describe('Unit: parser/source/unescapable', () => {
6
7
  describe('unescsource', () => {
7
- const parser = (source: string) => some(unescsource)({ source, context: {} });
8
+ const parser = (source: string) => some(unescsource)(input(source, ctx));
9
+ const { context: ctx } = input('', {});
8
10
 
9
11
  it('invalid', () => {
10
- assert.deepStrictEqual(inspect(parser('')), undefined);
12
+ assert.deepStrictEqual(inspect(parser(''), ctx), undefined);
11
13
  });
12
14
 
13
15
  it('basic', () => {
14
- assert.deepStrictEqual(inspect(parser('a')), [['a'], '']);
15
- assert.deepStrictEqual(inspect(parser('ab')), [['ab'], '']);
16
- assert.deepStrictEqual(inspect(parser('09あいAZaz')), [['09', 'あい', 'AZaz'], '']);
16
+ assert.deepStrictEqual(inspect(parser('a'), ctx), [['a'], '']);
17
+ assert.deepStrictEqual(inspect(parser('ab'), ctx), [['a', 'b'], '']);
18
+ assert.deepStrictEqual(inspect(parser('09あいAZaz'), ctx), [['0', '9', '', 'い', 'A', 'Z', 'a', 'z'], '']);
17
19
  });
18
20
 
19
21
  it('space', () => {
20
- assert.deepStrictEqual(inspect(parser(' ')), [[' '], '']);
21
- assert.deepStrictEqual(inspect(parser(' ')), [[' ', ' '], '']);
22
- assert.deepStrictEqual(inspect(parser(' ')), [[' ', ' '], '']);
23
- assert.deepStrictEqual(inspect(parser(' \n')), [[' ', '<br>'], '']);
24
- assert.deepStrictEqual(inspect(parser(' \n')), [[' ', ' ', '<br>'], '']);
25
- assert.deepStrictEqual(inspect(parser(' \n')), [[' ', ' ', '<br>'], '']);
22
+ assert.deepStrictEqual(inspect(parser(' '), ctx), [[' '], '']);
23
+ assert.deepStrictEqual(inspect(parser(' '), ctx), [[' ', ' '], '']);
24
+ assert.deepStrictEqual(inspect(parser(' '), ctx), [[' ', ' ', ' '], '']);
25
+ assert.deepStrictEqual(inspect(parser(' \n'), ctx), [[' ', '<br>'], '']);
26
+ assert.deepStrictEqual(inspect(parser(' \n'), ctx), [[' ', ' ', '<br>'], '']);
27
+ assert.deepStrictEqual(inspect(parser(' \n'), ctx), [[' ', ' ', ' ', '<br>'], '']);
26
28
  });
27
29
 
28
30
  it('linebreak', () => {
29
- assert.deepStrictEqual(inspect(parser('\n\n')), [['<br>', '<br>'], '']);
31
+ assert.deepStrictEqual(inspect(parser('\n\n'), ctx), [['<br>', '<br>'], '']);
30
32
  });
31
33
 
32
34
  it('\\', () => {
33
- assert.deepStrictEqual(inspect(parser('\\')), [['\\'], '']);
34
- assert.deepStrictEqual(inspect(parser('\\\\')), [['\\', '\\'], '']);
35
- assert.deepStrictEqual(inspect(parser('\\\\\\')), [['\\', '\\', '\\'], '']);
36
- assert.deepStrictEqual(inspect(parser('\\ ')), [['\\', ' '], '']);
37
- assert.deepStrictEqual(inspect(parser('\\_')), [['\\', '_'], '']);
38
- assert.deepStrictEqual(inspect(parser('\\0')), [['\\', '0'], '']);
39
- assert.deepStrictEqual(inspect(parser('\\a')), [['\\', 'a'], '']);
40
- assert.deepStrictEqual(inspect(parser('\\`')), [['\\', '`'], '']);
41
- assert.deepStrictEqual(inspect(parser('\\ ')), [['\\', ' '], '']);
42
- assert.deepStrictEqual(inspect(parser('\\\n')), [['\\', '<br>'], '']);
35
+ assert.deepStrictEqual(inspect(parser('\\'), ctx), [['\\'], '']);
36
+ assert.deepStrictEqual(inspect(parser('\\\\'), ctx), [['\\', '\\'], '']);
37
+ assert.deepStrictEqual(inspect(parser('\\\\\\'), ctx), [['\\', '\\', '\\'], '']);
38
+ assert.deepStrictEqual(inspect(parser('\\ '), ctx), [['\\', ' '], '']);
39
+ assert.deepStrictEqual(inspect(parser('\\_'), ctx), [['\\', '_'], '']);
40
+ assert.deepStrictEqual(inspect(parser('\\0'), ctx), [['\\', '0'], '']);
41
+ assert.deepStrictEqual(inspect(parser('\\a'), ctx), [['\\', 'a'], '']);
42
+ assert.deepStrictEqual(inspect(parser('\\`'), ctx), [['\\', '`'], '']);
43
+ assert.deepStrictEqual(inspect(parser('\\ '), ctx), [['\\', ' '], '']);
44
+ assert.deepStrictEqual(inspect(parser('\\\n'), ctx), [['\\', '<br>'], '']);
43
45
  });
44
46
 
45
47
  });
@@ -1,42 +1,27 @@
1
1
  import { UnescapableSourceParser } from '../source';
2
2
  import { Command } from '../context';
3
3
  import { consume } from '../../combinator';
4
- import { delimiter, nonWhitespace, nonAlphanumeric, isAlphanumeric } from './text';
5
4
  import { html } from 'typed-dom/dom';
6
5
 
7
- export const unescsource: UnescapableSourceParser = ({ source, context }) => {
8
- if (source === '') return;
9
- const i = source.search(delimiter);
10
- switch (i) {
11
- case -1:
12
- consume(source.length, context);
13
- return [[source], ''];
14
- case 0: {
6
+ export const unescsource: UnescapableSourceParser = ({ context }) => {
7
+ const { source, position } = context;
8
+ if (position === source.length) return;
9
+ consume(1, context);
10
+ context.position += 1;
11
+ switch (source[position]) {
12
+ case '\r':
13
+ assert(!source.includes('\r', position + 1));
14
+ consume(-1, context);
15
+ return [[]];
16
+ case Command.Escape:
15
17
  consume(1, context);
16
- switch (source[0]) {
17
- case '\r':
18
- assert(!source.includes('\r', 1));
19
- consume(-1, context);
20
- return [[], source.slice(1)];
21
- case Command.Escape:
22
- consume(1, context);
23
- return [[source.slice(1, 2)], source.slice(2)];
24
- case '\n':
25
- context.linebreak ||= source.length;
26
- return [[html('br')], source.slice(1)];
27
- default:
28
- assert(source[0] !== '\n');
29
- const b = source[0].trimStart() === '';
30
- const i = b || isAlphanumeric(source[0])
31
- ? source.search(b ? nonWhitespace : nonAlphanumeric) || 1
32
- : 1;
33
- assert(i > 0);
34
- consume(i - 1, context);
35
- return [[source.slice(0, i - +b || 1)], source.slice(i - +b || 1)];
36
- }
37
- }
18
+ context.position += 1;
19
+ return [[source.slice(position + 1, position + 2)]];
20
+ case '\n':
21
+ context.linebreak ||= source.length - position;
22
+ return [[html('br')]];
38
23
  default:
39
- consume(i, context);
40
- return [[source.slice(0, i)], source.slice(i)];
24
+ assert(source[position] !== '\n');
25
+ return [[source[position]]];
41
26
  }
42
27
  };
@@ -14,5 +14,5 @@ export import AnyLineParser = SourceParser.AnyLineParser;
14
14
  export { text, txt, linebreak } from './source/text';
15
15
  export { escsource } from './source/escapable';
16
16
  export { unescsource } from './source/unescapable';
17
- export { str } from './source/str';
17
+ export { str, strs } from './source/str';
18
18
  export { contentline, emptyline, anyline } from './source/line';
@@ -1,6 +1,7 @@
1
1
  import { min } from 'spica/alias';
2
+ import { MarkdownParser } from '../../markdown';
2
3
  import { Command } from './context';
3
- import { Parser, Result, Ctx, Node, Context, eval, exec } from '../combinator/data/parser';
4
+ import { Parser, Result, Ctx, Node, Context, eval, failsafe } from '../combinator/data/parser';
4
5
  import { convert } from '../combinator';
5
6
  import { define } from 'typed-dom/dom';
6
7
 
@@ -22,67 +23,69 @@ export function lineable<N extends HTMLElement | string>(parser: Parser<N>, trim
22
23
  trim === 0);
23
24
  }
24
25
 
25
- export function repeat<P extends Parser<HTMLElement | string>>(symbol: string, parser: P, cons: (nodes: Node<P>[], context: Context<P>) => Node<P>[], termination?: (acc: Node<P>[][], rest: string, prefix: number, postfix: number, state: boolean) => Result<string | Node<P>>): P;
26
- export function repeat<N extends HTMLElement | string>(symbol: string, parser: Parser<N>, cons: (nodes: N[], context: Ctx) => N[], termination: (acc: N[][], rest: string, prefix: number, postfix: number, state: boolean) => Result<string | N> = (acc, rest, prefix, postfix) => {
27
- const nodes = [];
26
+ export function repeat<P extends Parser<HTMLElement | string, MarkdownParser.Context>>(symbol: string, parser: P, cons: (nodes: Node<P>[], context: Context<P>) => Node<P>[], termination?: (acc: Node<P>[], context: Ctx, prefix: number, postfix: number, state: boolean) => Result<string | Node<P>>): P;
27
+ export function repeat<N extends HTMLElement | string>(symbol: string, parser: Parser<N>, cons: (nodes: N[], context: MarkdownParser.Context) => N[], termination: (acc: N[], context: Ctx, prefix: number, postfix: number, state: boolean) => Result<string | N, MarkdownParser.Context> = (nodes, context, prefix, postfix) => {
28
+ const acc = [];
28
29
  if (prefix > 0) {
29
- nodes.push(symbol[0].repeat(prefix));
30
- }
31
- for (let i = 0; i < acc.length; ++i) {
32
- nodes.push(...acc[i]);
30
+ acc.push(symbol[0].repeat(prefix));
33
31
  }
32
+ acc.push(...nodes);
34
33
  if (postfix > 0) {
35
- nodes.push(rest.slice(0, postfix));
36
- rest = rest.slice(postfix);
34
+ const { source, position } = context;
35
+ acc.push(source.slice(position, position + postfix));
36
+ context.position += postfix;
37
37
  }
38
- return [nodes, rest];
39
- }): Parser<string | N> {
40
- return input => {
41
- const { source, context } = input;
42
- assert(source.startsWith(symbol));
43
- let acc: N[][] = [];
38
+ return [acc];
39
+ }): Parser<string | N, MarkdownParser.Context> {
40
+ return failsafe(input => {
41
+ const { context } = input;
42
+ const { source, position } = context;
43
+ assert(source.startsWith(symbol, context.position));
44
+ let nodes: N[] = [];
44
45
  let i = symbol.length;
45
- while (source[i] === source[0]) ++i;
46
- let rest = source.slice(i);
46
+ while (source[context.position + i] === source[context.position]) ++i;
47
+ context.position += i;
47
48
  let state = false;
48
49
  for (; i >= symbol.length; i -= symbol.length) {
49
- if (acc.length > 0 && rest.startsWith(symbol)) {
50
- acc = [cons(acc.flat(), context)];
51
- rest = rest.slice(symbol.length);
50
+ if (nodes.length > 0 && source.startsWith(symbol, context.position)) {
51
+ nodes = cons(nodes, context);
52
+ context.position += symbol.length;
52
53
  continue;
53
54
  }
54
- const result = parser({ source: rest, context });
55
+ const buf = context.buffer;
56
+ context.buffer = nodes;
57
+ const result = parser(input);
58
+ context.buffer = buf;
55
59
  if (result === undefined) break;
56
- const nodes = eval(result);
57
- rest = exec(result);
58
- acc.push(nodes);
60
+ nodes = eval(result);
59
61
  switch (nodes.at(-1)) {
60
- case Command.Escape:
61
- assert(!rest.startsWith(symbol));
62
+ case Command.Cancel:
63
+ assert(!source.startsWith(symbol, context.position));
62
64
  nodes.pop();
63
65
  state = false;
64
66
  break;
65
67
  case Command.Separator:
66
- assert(!rest.startsWith(symbol));
68
+ assert(!source.startsWith(symbol, context.position));
67
69
  nodes.pop();
68
70
  state = true;
69
71
  continue;
70
72
  default:
71
- acc = [cons(acc.flat(), context)];
73
+ nodes = cons(nodes, context);
72
74
  state = true;
73
75
  continue;
74
76
  }
75
77
  break;
76
78
  }
77
- if (acc.length === 0) return;
79
+ if (nodes.length === 0) return;
78
80
  const prefix = i;
79
81
  i = 0;
80
- for (let len = min(prefix, rest.length); i < len && rest[i] === symbol[0];) {
82
+ for (let len = min(prefix, source.length - context.position); i < len && source[context.position + i] === symbol[0];) {
81
83
  ++i;
82
84
  }
83
85
  const postfix = i;
84
- return termination(acc, rest, prefix, postfix, state);
85
- };
86
+ context.range = context.position - position;
87
+ return termination(nodes, context, prefix, postfix, state);
88
+ });
86
89
  }
87
90
 
88
91
  export function invalid(
@@ -22,7 +22,7 @@ export function visualize<N extends HTMLElement | string>(parser: Parser<N>): Pa
22
22
  return union([
23
23
  convert(
24
24
  source => source.replace(blank.line, line => line.replace(/[\\&<]/g, `${Command.Escape}$&`)),
25
- verify(parser, (ns, rest) => !rest && hasVisible(ns)),
25
+ verify(parser, (ns, { source, position }) => position === source.length && hasVisible(ns)),
26
26
  false),
27
27
  some(union([linebreak, unescsource])),
28
28
  ]);
@@ -37,6 +37,7 @@ function hasVisible(
37
37
  }
38
38
  else {
39
39
  if (node.innerText.trimStart()) return true;
40
+ if (node.classList.contains('reference')) return true;
40
41
  //if (state & State.media ^ State.media &&
41
42
  // (node.classList.contains('media') || node.getElementsByClassName('media')[0])) return true;
42
43
  }
@@ -79,35 +80,38 @@ export function tightStart<N>(parser: Parser<N>, except?: string): Parser<N> {
79
80
  : undefined;
80
81
  }
81
82
  function isTightStart(input: Input<MarkdownParser.Context>, except?: string): boolean {
82
- const { source } = input;
83
- if (source === '') return true;
84
- if (except && source.slice(0, except.length) === except) return false;
85
- switch (source[0]) {
83
+ const { context } = input;
84
+ const { source, position } = context;
85
+ if (position === source.length) return true;
86
+ if (except && source.startsWith(except, position)) return false;
87
+ switch (source[position]) {
86
88
  case ' ':
87
89
  case ' ':
88
90
  case '\t':
89
91
  case '\n':
90
92
  return false;
91
93
  case '\\':
92
- return source[1]?.trimStart() !== '';
94
+ return source[position + 1]?.trimStart() !== '';
93
95
  case '&':
94
96
  switch (true) {
95
- case source.length > 2
96
- && source[1] !== ' '
97
+ case source.length - position > 2
98
+ && source[position + 1] !== ' '
97
99
  && eval(unsafehtmlentity(input))?.[0]?.trimStart() === '':
100
+ context.position = position;
98
101
  return false;
99
102
  }
103
+ context.position = position;
100
104
  return true;
101
105
  case '<':
102
106
  switch (true) {
103
- case source.length >= 5
104
- && source.slice(0, 4) === '<wbr'
105
- && (source[5] === '>' || /^<wbr[^\S\n]*>/.test(source)):
107
+ case source.length - position >= 5
108
+ && source.startsWith('<wbr', position)
109
+ && (source[position + 5] === '>' || /^<wbr[^\S\n]*>/.test(source.slice(position))):
106
110
  return false;
107
111
  }
108
112
  return true;
109
113
  default:
110
- return source[0].trimStart() !== '';
114
+ return source[position].trimStart() !== '';
111
115
  }
112
116
  }
113
117
 
@@ -199,9 +203,9 @@ export function trimBlankNodeEnd<N extends HTMLElement | string>(nodes: N[]): N[
199
203
  : [];
200
204
  for (let node = nodes[0]; nodes.length > 0 && !isVisible(node = nodes.at(-1)!, -1);) {
201
205
  if (typeof node === 'string') {
202
- const len = node.trimEnd().length;
203
- if (len > 0) {
204
- nodes[nodes.length - 1] = node.slice(0, len) as N;
206
+ const str = node.trimEnd();
207
+ if (str.length > 0) {
208
+ nodes[nodes.length - 1] = str as N;
205
209
  break;
206
210
  }
207
211
  }
package/src/util/quote.ts CHANGED
@@ -1,8 +1,10 @@
1
- import { exec } from '../combinator/data/parser';
1
+ import { input } from '../combinator/data/parser';
2
2
  import { cite } from '../parser/block/reply/cite';
3
3
 
4
4
  export function quote(anchor: string, range: Range): string {
5
- if (exec(cite({ source: `>>${anchor}`, context: {} })) !== '') throw new Error(`Invalid anchor: ${anchor}`);
5
+ const { context } = input('', {});
6
+ cite(input(`>>${anchor}`, context));
7
+ if (context.position !== context.source.length) throw new Error(`Invalid anchor: ${anchor}`);
6
8
  fit(range);
7
9
  const node = trim(range.cloneContents());
8
10
  if (!node.firstChild) return '';