securemark 0.294.1 → 0.294.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/design.md +27 -39
- package/dist/index.js +49 -66
- package/package.json +1 -1
- package/src/combinator/control/manipulation/clear.ts +2 -2
- package/src/combinator/control/manipulation/lazy.ts +1 -3
- package/src/combinator/data/data.ts +38 -32
- package/src/combinator/data/parser/inits.ts +3 -3
- package/src/combinator/data/parser/sequence.test.ts +2 -2
- package/src/combinator/data/parser/sequence.ts +2 -2
- package/src/combinator/data/parser/some.test.ts +2 -2
- package/src/combinator/data/parser/subsequence.test.ts +2 -2
- package/src/combinator/data/parser/subsequence.ts +2 -2
- package/src/combinator/data/parser/tails.ts +2 -2
- package/src/combinator/data/parser/union.test.ts +2 -2
- package/src/combinator/data/parser/union.ts +2 -2
- package/src/combinator/data/parser.ts +33 -18
- package/src/parser/api/bind.ts +1 -1
- package/src/parser/api/parse.ts +9 -9
- package/src/parser/block.ts +1 -1
- package/src/parser/inline/autolink/url.test.ts +71 -72
- package/src/parser/inline/autolink/url.ts +4 -4
- package/src/parser/segment.ts +4 -10
- package/src/parser/util.ts +2 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Parser, Ctx } from './parser';
|
|
2
2
|
|
|
3
|
-
export class List<N extends List.Node = List.Node, C extends
|
|
3
|
+
export class List<N extends List.Node = List.Node, C extends Ctx = Ctx, D extends Parser<unknown, C>[] = any> {
|
|
4
4
|
constructor(nodes?: ArrayLike<N>) {
|
|
5
5
|
if (nodes === undefined) return;
|
|
6
6
|
for (let i = 0; i < nodes.length; ++i) {
|
|
@@ -9,47 +9,55 @@ export class List<N extends List.Node = List.Node, C extends CtxOptions = CtxOpt
|
|
|
9
9
|
}
|
|
10
10
|
public length = 0;
|
|
11
11
|
public head?: N = undefined;
|
|
12
|
+
public last?: N = undefined;
|
|
12
13
|
public get tail(): N | undefined {
|
|
13
14
|
return this.head?.next;
|
|
14
15
|
}
|
|
15
|
-
public get last(): N | undefined {
|
|
16
|
-
return this.head?.prev;
|
|
17
|
-
}
|
|
18
16
|
public insert(node: N, before?: N): N {
|
|
19
|
-
assert(!node.next);
|
|
17
|
+
assert(!node.next && !node.prev);
|
|
18
|
+
if (before === undefined) return this.push(node);
|
|
19
|
+
if (before === this.head) return this.unshift(node);
|
|
20
20
|
if (++this.length === 1) {
|
|
21
|
-
return this.head =
|
|
21
|
+
return this.head = this.last = node;
|
|
22
22
|
}
|
|
23
23
|
assert(node !== before);
|
|
24
|
-
const next = node.next = before
|
|
24
|
+
const next = node.next = before;
|
|
25
25
|
const prev = node.prev = next.prev!;
|
|
26
26
|
return next.prev = prev.next = node;
|
|
27
27
|
}
|
|
28
28
|
public delete(node: N): N {
|
|
29
|
-
assert(node.next);
|
|
29
|
+
assert(node.next || node.prev || this.head === this.last);
|
|
30
30
|
assert(this.length > 0);
|
|
31
31
|
if (--this.length === 0) {
|
|
32
|
-
this.head = undefined;
|
|
32
|
+
this.head = this.last = undefined;
|
|
33
33
|
}
|
|
34
34
|
else {
|
|
35
35
|
const { next, prev } = node;
|
|
36
|
-
|
|
37
|
-
this.head = next
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
36
|
+
prev === undefined
|
|
37
|
+
? this.head = next
|
|
38
|
+
: prev.next = next;
|
|
39
|
+
next === undefined
|
|
40
|
+
? this.last = prev
|
|
41
|
+
: next.prev = prev;
|
|
42
42
|
}
|
|
43
43
|
node.next = node.prev = undefined;
|
|
44
44
|
return node;
|
|
45
45
|
}
|
|
46
46
|
public unshift(node: N): N {
|
|
47
|
-
assert(!node.next);
|
|
48
|
-
|
|
47
|
+
assert(!node.next && !node.prev);
|
|
48
|
+
if (++this.length === 1) {
|
|
49
|
+
return this.head = this.last = node;
|
|
50
|
+
}
|
|
51
|
+
node.next = this.head;
|
|
52
|
+
return this.head = this.head!.prev = node;
|
|
49
53
|
}
|
|
50
54
|
public push(node: N): N {
|
|
51
|
-
assert(!node.next);
|
|
52
|
-
|
|
55
|
+
assert(!node.next && !node.prev);
|
|
56
|
+
if (++this.length === 1) {
|
|
57
|
+
return this.head = this.last = node;
|
|
58
|
+
}
|
|
59
|
+
node.prev = this.last;
|
|
60
|
+
return this.last = this.last!.next = node;
|
|
53
61
|
}
|
|
54
62
|
public shift(): N | undefined {
|
|
55
63
|
if (this.length === 0) return;
|
|
@@ -57,22 +65,25 @@ export class List<N extends List.Node = List.Node, C extends CtxOptions = CtxOpt
|
|
|
57
65
|
}
|
|
58
66
|
public pop(): N | undefined {
|
|
59
67
|
if (this.length === 0) return;
|
|
60
|
-
return this.delete(this.
|
|
68
|
+
return this.delete(this.last!);
|
|
61
69
|
}
|
|
62
70
|
public import(list: List<N>, before?: N): this {
|
|
63
71
|
assert(list !== this);
|
|
64
72
|
if (list.length === 0) return this;
|
|
65
73
|
if (this.length === 0) {
|
|
66
74
|
this.head = list.head;
|
|
67
|
-
this.
|
|
75
|
+
this.last = list.last;
|
|
76
|
+
this.length = list.length;
|
|
68
77
|
list.clear();
|
|
69
78
|
return this;
|
|
70
79
|
}
|
|
71
80
|
const head = list.head!;
|
|
72
81
|
const last = list.last!;
|
|
73
|
-
const next = last.next = before
|
|
74
|
-
const prev = head.prev =
|
|
75
|
-
next
|
|
82
|
+
const next = last.next = before;
|
|
83
|
+
const prev = head.prev = before?.prev ?? this.last!;
|
|
84
|
+
next === undefined
|
|
85
|
+
? this.last = last
|
|
86
|
+
: next.prev = last;
|
|
76
87
|
prev.next = head;
|
|
77
88
|
this.length += list.length;
|
|
78
89
|
list.clear();
|
|
@@ -80,14 +91,13 @@ export class List<N extends List.Node = List.Node, C extends CtxOptions = CtxOpt
|
|
|
80
91
|
}
|
|
81
92
|
public clear(): void {
|
|
82
93
|
this.length = 0;
|
|
83
|
-
this.head = undefined;
|
|
94
|
+
this.head = this.last = undefined;
|
|
84
95
|
}
|
|
85
96
|
public *[Symbol.iterator](): Iterator<N, undefined, undefined> {
|
|
86
97
|
for (let node = this.head; node && this.head;) {
|
|
87
98
|
const next = node.next;
|
|
88
99
|
yield node;
|
|
89
100
|
node = next;
|
|
90
|
-
if (node === this.head) break;
|
|
91
101
|
}
|
|
92
102
|
}
|
|
93
103
|
public flatMap<T extends List.Node>(f: (node: N) => List<T>): List<T> {
|
|
@@ -96,7 +106,6 @@ export class List<N extends List.Node = List.Node, C extends CtxOptions = CtxOpt
|
|
|
96
106
|
const next = node.next;
|
|
97
107
|
acc.import(f(node));
|
|
98
108
|
node = next;
|
|
99
|
-
if (node === this.head) break;
|
|
100
109
|
}
|
|
101
110
|
return acc;
|
|
102
111
|
}
|
|
@@ -105,15 +114,13 @@ export class List<N extends List.Node = List.Node, C extends CtxOptions = CtxOpt
|
|
|
105
114
|
const next = node.next;
|
|
106
115
|
acc = f(acc, node);
|
|
107
116
|
node = next;
|
|
108
|
-
if (node === this.head) break;
|
|
109
117
|
}
|
|
110
118
|
return acc;
|
|
111
119
|
}
|
|
112
120
|
public foldr<T>(f: (node: N, acc: T) => T, acc: T): T {
|
|
113
|
-
for (let node = this.
|
|
121
|
+
for (let node = this.last; node && this.head;) {
|
|
114
122
|
const prev = node.prev;
|
|
115
123
|
acc = f(node, acc);
|
|
116
|
-
if (node === this.head) break;
|
|
117
124
|
node = prev;
|
|
118
125
|
}
|
|
119
126
|
return acc;
|
|
@@ -123,7 +130,6 @@ export class List<N extends List.Node = List.Node, C extends CtxOptions = CtxOpt
|
|
|
123
130
|
const next = node.next;
|
|
124
131
|
if (f(node)) return node;
|
|
125
132
|
node = next;
|
|
126
|
-
if (node === this.head) break;
|
|
127
133
|
}
|
|
128
134
|
}
|
|
129
135
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Parser, List, Data,
|
|
1
|
+
import { Parser, List, Data, Ctx, Node, Context, SubParsers, SubNode } from '../parser';
|
|
2
2
|
|
|
3
|
-
export function inits<P extends Parser<unknown
|
|
4
|
-
export function inits<N, D extends Parser<N>[]>(parsers: D, resume?: (nodes: List<Data<N>>) => boolean): Parser<N,
|
|
3
|
+
export function inits<P extends Parser<unknown>>(parsers: SubParsers<P>, resume?: (nodes: List<Data<SubNode<P>>>) => boolean): SubNode<P> extends Node<P> ? P : Parser<SubNode<P>, Context<P>, SubParsers<P>>;
|
|
4
|
+
export function inits<N, D extends Parser<N>[]>(parsers: D, resume?: (nodes: List<Data<N>>) => boolean): Parser<N, Ctx, D> {
|
|
5
5
|
assert(parsers.every(f => f));
|
|
6
6
|
if (parsers.length === 1) return parsers[0];
|
|
7
7
|
return input => {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Parser, List, Data, input } from '../parser';
|
|
1
|
+
import { Parser, List, Data, Ctx, input } from '../parser';
|
|
2
2
|
import { sequence } from './sequence';
|
|
3
3
|
import { inspect } from '../../../debug.test';
|
|
4
4
|
|
|
@@ -14,7 +14,7 @@ describe('Unit: combinator/data/parser/sequence', () => {
|
|
|
14
14
|
? void ++context.position || new List([new Data('B')])
|
|
15
15
|
: undefined;
|
|
16
16
|
};
|
|
17
|
-
const ab = sequence<Parser<string,
|
|
17
|
+
const ab = sequence<Parser<string, Ctx, [typeof a, typeof b]>>([a, b]);
|
|
18
18
|
const { context: ctx } = input('', {});
|
|
19
19
|
|
|
20
20
|
it('basic', () => {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Parser, List, Data,
|
|
1
|
+
import { Parser, List, Data, Ctx, Node, Context, SubParsers, SubNode } from '../parser';
|
|
2
2
|
|
|
3
3
|
export function sequence<P extends Parser<unknown>>(parsers: SubParsers<P>, resume?: (nodes: List<Data<SubNode<P>>>) => boolean): SubNode<P> extends Node<P> ? P : Parser<SubNode<P>, Context<P>, SubParsers<P>>;
|
|
4
|
-
export function sequence<N, D extends Parser<N>[]>(parsers: D, resume?: (nodes: List<Data<N>>) => boolean): Parser<N,
|
|
4
|
+
export function sequence<N, D extends Parser<N>[]>(parsers: D, resume?: (nodes: List<Data<N>>) => boolean): Parser<N, Ctx, D> {
|
|
5
5
|
assert(parsers.every(f => f));
|
|
6
6
|
if (parsers.length === 1) return parsers[0];
|
|
7
7
|
return input => {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Parser, List, Data, input } from '../parser';
|
|
1
|
+
import { Parser, List, Data, Ctx, input } from '../parser';
|
|
2
2
|
import { union } from './union';
|
|
3
3
|
import { some } from './some';
|
|
4
4
|
import { inspect } from '../../../debug.test';
|
|
@@ -15,7 +15,7 @@ describe('Unit: combinator/data/parser/some', () => {
|
|
|
15
15
|
? void ++context.position || new List([new Data('B')])
|
|
16
16
|
: undefined;
|
|
17
17
|
};
|
|
18
|
-
const ab = union<Parser<string,
|
|
18
|
+
const ab = union<Parser<string, Ctx, [typeof a, typeof b]>>([a, b]);
|
|
19
19
|
const { context: ctx } = input('', {});
|
|
20
20
|
|
|
21
21
|
it('basic', () => {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Parser, List, Data, input } from '../parser';
|
|
1
|
+
import { Parser, List, Data, Ctx, input } from '../parser';
|
|
2
2
|
import { subsequence } from './subsequence';
|
|
3
3
|
import { inspect } from '../../../debug.test';
|
|
4
4
|
|
|
@@ -19,7 +19,7 @@ describe('Unit: combinator/data/parser/subsequence', () => {
|
|
|
19
19
|
? void ++context.position || new List([new Data('C')])
|
|
20
20
|
: undefined;
|
|
21
21
|
};
|
|
22
|
-
const abc = subsequence<Parser<string,
|
|
22
|
+
const abc = subsequence<Parser<string, Ctx, [typeof a, typeof b, typeof c]>>([a, b, c]);
|
|
23
23
|
const { context: ctx } = input('', {});
|
|
24
24
|
|
|
25
25
|
it('basic', () => {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { Parser, List, Data,
|
|
1
|
+
import { Parser, List, Data, Ctx, Node, Context, SubParsers, SubNode } from '../parser';
|
|
2
2
|
import { union } from './union';
|
|
3
3
|
import { inits } from './inits';
|
|
4
4
|
|
|
5
5
|
export function subsequence<P extends Parser<unknown>>(parsers: SubParsers<P>, resume?: (nodes: List<Data<SubNode<P>>>) => boolean): SubNode<P> extends Node<P> ? P : Parser<SubNode<P>, Context<P>, SubParsers<P>>;
|
|
6
|
-
export function subsequence<N, D extends Parser<N>[]>(parsers: D, resume?: (nodes: List<Data<N>>) => boolean): Parser<N,
|
|
6
|
+
export function subsequence<N, D extends Parser<N>[]>(parsers: D, resume?: (nodes: List<Data<N>>) => boolean): Parser<N, Ctx, D> {
|
|
7
7
|
assert(parsers.every(f => f));
|
|
8
8
|
return union(
|
|
9
9
|
parsers.map((_, i) =>
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Parser, List, Data,
|
|
1
|
+
import { Parser, List, Data, Ctx, Node, Context, SubParsers, SubNode } from '../parser';
|
|
2
2
|
import { union } from './union';
|
|
3
3
|
import { sequence } from './sequence';
|
|
4
4
|
|
|
5
5
|
export function tails<P extends Parser<unknown>>(parsers: SubParsers<P>, resume?: (nodes: List<Data<SubNode<P>>>) => boolean): SubNode<P> extends Node<P> ? P : Parser<SubNode<P>, Context<P>, SubParsers<P>>;
|
|
6
|
-
export function tails<N, D extends Parser<N>[]>(parsers: D, resume?: (nodes: List<Data<N>>) => boolean): Parser<N,
|
|
6
|
+
export function tails<N, D extends Parser<N>[]>(parsers: D, resume?: (nodes: List<Data<N>>) => boolean): Parser<N, Ctx, D> {
|
|
7
7
|
return union(parsers.map((_, i) => sequence(parsers.slice(i), resume)) as D);
|
|
8
8
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Parser, List, Data, input } from '../parser';
|
|
1
|
+
import { Parser, List, Data, Ctx, input } from '../parser';
|
|
2
2
|
import { union } from './union';
|
|
3
3
|
import { inspect } from '../../../debug.test';
|
|
4
4
|
|
|
@@ -14,7 +14,7 @@ describe('Unit: combinator/data/parser/union', () => {
|
|
|
14
14
|
? void ++context.position || new List([new Data('B')])
|
|
15
15
|
: undefined;
|
|
16
16
|
};
|
|
17
|
-
const ab = union<Parser<string,
|
|
17
|
+
const ab = union<Parser<string, Ctx, [typeof a, typeof b]>>([a, b]);
|
|
18
18
|
const { context: ctx } = input('', {});
|
|
19
19
|
|
|
20
20
|
it('basic', () => {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Parser,
|
|
1
|
+
import { Parser, Ctx, Node, Context, SubParsers, SubNode } from '../parser';
|
|
2
2
|
|
|
3
3
|
export function union<P extends Parser<unknown>>(parsers: SubParsers<P>): SubNode<P> extends Node<P> ? P : Parser<SubNode<P>, Context<P>, SubParsers<P>>;
|
|
4
|
-
export function union<N, D extends Parser<N>[]>(parsers: D): Parser<N,
|
|
4
|
+
export function union<N, D extends Parser<N>[]>(parsers: D): Parser<N, Ctx, D> {
|
|
5
5
|
assert(parsers.every(f => f));
|
|
6
6
|
switch (parsers.length) {
|
|
7
7
|
case 0:
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { List } from './data';
|
|
2
2
|
import { Delimiters } from './parser/context/delimiter';
|
|
3
3
|
|
|
4
|
-
export type Parser<N, C extends
|
|
5
|
-
= (input: Input<C
|
|
6
|
-
export interface Input<C extends
|
|
7
|
-
readonly context: C
|
|
4
|
+
export type Parser<N, C extends Ctx = Ctx, D extends Parser<unknown, C>[] = any>
|
|
5
|
+
= (input: Input<C>) => Result<N, C, D>;
|
|
6
|
+
export interface Input<C extends Ctx = Ctx> {
|
|
7
|
+
readonly context: C;
|
|
8
8
|
}
|
|
9
|
-
export type Result<N, C extends
|
|
9
|
+
export type Result<N, C extends Ctx = Ctx, D extends Parser<unknown, C>[] = any>
|
|
10
10
|
= List<Data<N>, C, D>
|
|
11
11
|
| undefined;
|
|
12
12
|
export { List };
|
|
@@ -32,19 +32,44 @@ export interface CtxOptions {
|
|
|
32
32
|
// Objectの内部実装を利用する。
|
|
33
33
|
// 探索木を直接使用する場合は探索速度が重要で挿入は相対的に少なく削除は不要かつ不確実であるため
|
|
34
34
|
// AVL木が適当と思われる。
|
|
35
|
+
// 最大セグメントサイズ10KB内で探索コストが平均実行性能を圧迫するほど大きくなるとは考えにくいが
|
|
36
|
+
// 探索コストを減らすにはバックトラック位置数が規定数を超えた場合一定区間ごとに探索木を分割する方法が考えられる。
|
|
37
|
+
// 10KBの入力すべてを保持する探索木を1024文字ごとに分割するために必要なテーブルサイズは64bit*98=784byteとなる。
|
|
38
|
+
// 探索木のポインタによるオーバーヘッドを考慮すれば一定サイズ以上ではテーブルのほうが効率的となる。
|
|
39
|
+
// 区間別テーブルは固定サイズであるためプールして再使用できる。
|
|
40
|
+
// 従って分割時のデータ構造は区間ごとに探索木を動的に生成しデータ数に応じてテーブルに移行するのが最も効率的である。
|
|
41
|
+
// これにより最悪時間計算量線形化に要する最悪空間計算量が+1nに局限される。
|
|
42
|
+
// 木とテーブルいずれにおいてもバックトラックデータとオーバーヘッドを合わせた追加データサイズの最大値は
|
|
43
|
+
// セグメントサイズに制約されるため入力サイズに対する最大追加データサイズの平均比率はかなり小さくなる。
|
|
44
|
+
//
|
|
45
|
+
// 1. データ数が規定数を超えたら区間テーブルを生成しデータを振り分ける。
|
|
46
|
+
// - 子ノードのポインタだけ保持するとしても1ノード複数データ保持で圧縮できるかは微妙。
|
|
47
|
+
// - 1ノードに2データ保持すれば2連続データを1/2の確率で捕捉し1バックトラックあたりの平均追加データサイズは
|
|
48
|
+
// -7byte(((16+1)*2-(16+2))*2+((16+1)*2-(16+2)*2)*2)/4=(32-4)/4=7の10byteに減少する。
|
|
49
|
+
// 2連続データの発生確率が1/5なら-3.2byteの13.8byte、1/10なら+0.4byteの17.4byteに増加する。
|
|
50
|
+
// - 1ノードに4データ保持すれば2連続データを3/4の確率で捕捉し1バックトラックあたりの平均追加データサイズは
|
|
51
|
+
// -9byte(((16+1)*2-(16+4))*3+((16+1)*2-(16+4)*2))/4=(42-6)/4=9の8byteに減少する。
|
|
52
|
+
// 2連続データの発生確率が1/5なら-3.6byteの13.4byte、1/10なら+1.2byteの18.2byteに増加する。
|
|
53
|
+
// 2. 区間内のデータ構造は探索木から開始しデータ数が規定数を超えたらテーブルに変換する。
|
|
54
|
+
// - 1ノード1データ1区間1024文字ならば1024<(64/8*2+1)*61から1区間61データ以上でテーブルのほうが小さくなる。
|
|
55
|
+
// - 64/8*2+1=17文字に1か所以下のバックトラックでテーブル以上の効率となる。
|
|
56
|
+
// - 通常の入力でバックトラックが17文字に平均1か所以上となることは考えられず
|
|
57
|
+
// 1段落数百文字あたり平均2、3か所以下が妥当な頻度でありこの場合の最大追加データサイズは
|
|
58
|
+
// 入力内の最大セグメントサイズの10%前後である。
|
|
59
|
+
//
|
|
35
60
|
backtracks?: Record<number, number>;
|
|
36
61
|
linebreak?: number;
|
|
37
62
|
range?: number;
|
|
38
63
|
}
|
|
39
64
|
export type Node<P extends Parser<unknown>> = P extends Parser<infer N> ? N : never;
|
|
40
|
-
export type SubParsers<P extends Parser<unknown>> = P extends Parser<unknown,
|
|
41
|
-
export type Context<P extends Parser<unknown>> = P extends Parser<unknown, infer C> ? C
|
|
65
|
+
export type SubParsers<P extends Parser<unknown>> = P extends Parser<unknown, Ctx, infer D> ? D : never;
|
|
66
|
+
export type Context<P extends Parser<unknown>> = P extends Parser<unknown, infer C> ? C : never;
|
|
42
67
|
export type SubNode<P extends Parser<unknown>> = ExtractSubNode<SubParsers<P>>;
|
|
43
68
|
export type IntermediateParser<P extends Parser<unknown>> = Parser<SubNode<P>, Context<P>, SubParsers<P>>;
|
|
44
69
|
type ExtractSubNode<D extends Parser<unknown>[]> = ExtractSubParser<D> extends infer N ? N extends Parser<infer U> ? U : never : never;
|
|
45
70
|
type ExtractSubParser<D extends Parser<unknown>[]> = D extends (infer P)[] ? P extends Parser<unknown> ? P : never : never;
|
|
46
71
|
|
|
47
|
-
export function input(source: string, context:
|
|
72
|
+
export function input<C extends CtxOptions>(source: string, context: C): Input<C & Ctx> {
|
|
48
73
|
// @ts-expect-error
|
|
49
74
|
context.source = source;
|
|
50
75
|
// @ts-expect-error
|
|
@@ -67,16 +92,6 @@ export function subinput<C extends Ctx>(source: string, context: C): Input<C> {
|
|
|
67
92
|
};
|
|
68
93
|
}
|
|
69
94
|
|
|
70
|
-
export function clean<C extends Ctx>(context: C): C {
|
|
71
|
-
const { source, position } = context;
|
|
72
|
-
for (const p of Object.keys(context)) {
|
|
73
|
-
context[p] = undefined;
|
|
74
|
-
}
|
|
75
|
-
context.source = source;
|
|
76
|
-
context.position = position;
|
|
77
|
-
return context;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
95
|
export function failsafe<P extends Parser<unknown>>(parser: P): P;
|
|
81
96
|
export function failsafe<N>(parser: Parser<N>): Parser<N> {
|
|
82
97
|
assert(parser);
|
package/src/parser/api/bind.ts
CHANGED
|
@@ -19,7 +19,7 @@ export function bind(target: DocumentFragment | HTMLElement | ShadowRoot, settin
|
|
|
19
19
|
nearest: (position: number) => HTMLElement | undefined;
|
|
20
20
|
index: (block: HTMLElement) => number;
|
|
21
21
|
} {
|
|
22
|
-
|
|
22
|
+
const context: MarkdownParser.Options = {
|
|
23
23
|
...settings,
|
|
24
24
|
host: settings.host ?? new ReadonlyURL(location.pathname, location.origin),
|
|
25
25
|
};
|
package/src/parser/api/parse.ts
CHANGED
|
@@ -15,13 +15,13 @@ interface Options extends ParserOptions {
|
|
|
15
15
|
readonly test?: boolean;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
export function parse(source: string,
|
|
18
|
+
export function parse(source: string, options: Options = {}, context?: MarkdownParser.Options): DocumentFragment {
|
|
19
19
|
const url = headers(source).find(field => field.toLowerCase().startsWith('url:'))?.slice(4).trim() ?? '';
|
|
20
20
|
source = !context ? normalize(source) : source;
|
|
21
21
|
context = {
|
|
22
|
-
host:
|
|
22
|
+
host: options.host ?? context?.host ?? new ReadonlyURL(location.pathname, location.origin),
|
|
23
23
|
url: url ? new ReadonlyURL(url as ':') : context?.url,
|
|
24
|
-
id:
|
|
24
|
+
id: options.id ?? context?.id,
|
|
25
25
|
caches: context?.caches,
|
|
26
26
|
resources: context?.resources,
|
|
27
27
|
};
|
|
@@ -38,11 +38,11 @@ export function parse(source: string, opts: Options = {}, context?: MarkdownPars
|
|
|
38
38
|
...(header(input(seg, { header: index++ === 0 } as MarkdownParser.Context)) || block(input(seg, context)))
|
|
39
39
|
?.foldl<HTMLElement[]>((acc, { value }) => void acc.push(value) || acc, []) ?? []);
|
|
40
40
|
}
|
|
41
|
-
assert(
|
|
42
|
-
if (
|
|
43
|
-
for (const _ of figure(node,
|
|
44
|
-
for (const _ of note(node,
|
|
45
|
-
assert(
|
|
46
|
-
assert(
|
|
41
|
+
assert(options.id !== '' || !node.querySelector('[id], .index[href], .label[href], .annotation > a[href], .reference > a[href]'));
|
|
42
|
+
if (options.test) return node;
|
|
43
|
+
for (const _ of figure(node, options.notes, context));
|
|
44
|
+
for (const _ of note(node, options.notes, context));
|
|
45
|
+
assert(options.id !== '' || !node.querySelector('[id], .index[href], .label[href], .annotation > a[href], .reference > a[href]'));
|
|
46
|
+
assert(options.id !== '' || !options.notes?.references.querySelector('[id], .index[href], .label[href]'));
|
|
47
47
|
return node;
|
|
48
48
|
}
|
package/src/parser/block.ts
CHANGED
|
@@ -56,6 +56,7 @@ export const block: BlockParser = reset(
|
|
|
56
56
|
backtracks: {},
|
|
57
57
|
},
|
|
58
58
|
error(union([
|
|
59
|
+
emptyline,
|
|
59
60
|
input => {
|
|
60
61
|
const { context: { source, position } } = input;
|
|
61
62
|
if (position === source.length) return;
|
|
@@ -109,7 +110,6 @@ export const block: BlockParser = reset(
|
|
|
109
110
|
if ('0' <= fst && fst <= '9') return olist(input);
|
|
110
111
|
}
|
|
111
112
|
},
|
|
112
|
-
emptyline,
|
|
113
113
|
paragraph
|
|
114
114
|
]) as any));
|
|
115
115
|
|