securemark 0.257.1 → 0.258.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +21 -8
- package/dist/index.js +1224 -611
- package/markdown.d.ts +1 -28
- package/package.json +9 -9
- package/src/combinator/control/manipulation/convert.ts +8 -4
- package/src/combinator/control/manipulation/scope.ts +10 -2
- package/src/combinator/data/parser/context/delimiter.ts +70 -0
- package/src/combinator/data/parser/context/memo.ts +30 -0
- package/src/combinator/{control/manipulation → data/parser}/context.test.ts +9 -9
- package/src/combinator/data/parser/context.ts +161 -0
- package/src/combinator/data/parser/inits.ts +1 -1
- package/src/combinator/data/parser/sequence.test.ts +1 -1
- package/src/combinator/data/parser/sequence.ts +1 -1
- package/src/combinator/data/parser/some.test.ts +1 -1
- package/src/combinator/data/parser/some.ts +14 -37
- package/src/combinator/data/parser/subsequence.test.ts +1 -1
- package/src/combinator/data/parser/union.test.ts +1 -1
- package/src/combinator/data/parser.ts +7 -47
- package/src/combinator.ts +1 -2
- package/src/parser/api/bind.ts +5 -5
- package/src/parser/api/parse.ts +3 -1
- package/src/parser/block/blockquote.ts +1 -1
- package/src/parser/block/dlist.ts +4 -10
- package/src/parser/block/extension/figure.ts +4 -3
- package/src/parser/block/extension/table.ts +2 -2
- package/src/parser/block/heading.ts +5 -13
- package/src/parser/block/ilist.ts +3 -2
- package/src/parser/block/olist.ts +10 -7
- package/src/parser/block/paragraph.ts +1 -1
- package/src/parser/block/reply/cite.ts +1 -1
- package/src/parser/block/reply/quote.ts +1 -1
- package/src/parser/block/reply.ts +1 -1
- package/src/parser/block/sidefence.ts +1 -1
- package/src/parser/block/table.test.ts +5 -0
- package/src/parser/block/table.ts +14 -13
- package/src/parser/block/ulist.ts +4 -3
- package/src/parser/block.ts +1 -1
- package/src/parser/context.ts +32 -0
- package/src/parser/header.ts +1 -1
- package/src/parser/inline/annotation.ts +9 -17
- package/src/parser/inline/autolink/email.ts +1 -1
- package/src/parser/inline/autolink/url.ts +2 -2
- package/src/parser/inline/autolink.ts +5 -3
- package/src/parser/inline/bracket.test.ts +2 -0
- package/src/parser/inline/bracket.ts +16 -15
- package/src/parser/inline/code.ts +1 -1
- package/src/parser/inline/comment.test.ts +1 -0
- package/src/parser/inline/comment.ts +4 -3
- package/src/parser/inline/deletion.ts +5 -4
- package/src/parser/inline/emphasis.ts +5 -4
- package/src/parser/inline/emstrong.ts +5 -4
- package/src/parser/inline/extension/index.ts +8 -15
- package/src/parser/inline/extension/indexee.ts +8 -10
- package/src/parser/inline/extension/indexer.ts +4 -3
- package/src/parser/inline/extension/label.ts +3 -2
- package/src/parser/inline/extension/placeholder.ts +6 -5
- package/src/parser/inline/html.ts +5 -4
- package/src/parser/inline/htmlentity.ts +1 -1
- package/src/parser/inline/insertion.ts +5 -4
- package/src/parser/inline/link.ts +12 -21
- package/src/parser/inline/mark.ts +5 -4
- package/src/parser/inline/math.ts +8 -9
- package/src/parser/inline/media.ts +8 -7
- package/src/parser/inline/reference.ts +12 -18
- package/src/parser/inline/ruby.ts +4 -3
- package/src/parser/inline/shortmedia.ts +3 -2
- package/src/parser/inline/strong.ts +5 -4
- package/src/parser/inline/template.test.ts +1 -1
- package/src/parser/inline/template.ts +9 -6
- package/src/parser/inline.test.ts +2 -0
- package/src/parser/locale.ts +6 -7
- package/src/parser/processor/footnote.ts +5 -3
- package/src/parser/source/text.ts +1 -1
- package/src/parser/util.ts +0 -220
- package/src/parser/visibility.ts +205 -0
- package/src/util/info.ts +4 -2
- package/src/util/quote.ts +12 -15
- package/src/util/toc.ts +14 -17
- package/webpack.config.js +1 -0
- package/src/combinator/control/manipulation/context.ts +0 -70
- package/src/combinator/control/manipulation/resource.ts +0 -54
|
@@ -10,7 +10,7 @@ describe('Unit: parser/inline/template', () => {
|
|
|
10
10
|
assert.deepStrictEqual(inspect(parser('')), undefined);
|
|
11
11
|
assert.deepStrictEqual(inspect(parser('{')), undefined);
|
|
12
12
|
assert.deepStrictEqual(inspect(parser('{}')), undefined);
|
|
13
|
-
assert.deepStrictEqual(inspect(parser('{{')),
|
|
13
|
+
assert.deepStrictEqual(inspect(parser('{{')), [['', '{{'], '']);
|
|
14
14
|
assert.deepStrictEqual(inspect(parser('{{\\}}')), undefined);
|
|
15
15
|
assert.deepStrictEqual(inspect(parser('{{a}b}')), undefined);
|
|
16
16
|
assert.deepStrictEqual(inspect(parser('{{{a}}')), undefined);
|
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
import { undefined } from 'spica/global';
|
|
2
2
|
import { TemplateParser } from '../inline';
|
|
3
|
-
import { union, some,
|
|
3
|
+
import { union, some, syntax, creator, precedence, surround, lazy } from '../../combinator';
|
|
4
|
+
import { optimize } from './link';
|
|
4
5
|
import { escsource, str } from '../source';
|
|
6
|
+
import { Rule } from '../context';
|
|
5
7
|
import { html } from 'typed-dom/dom';
|
|
6
8
|
import { unshift } from 'spica/array';
|
|
7
9
|
|
|
8
|
-
export const template: TemplateParser = lazy(() =>
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
export const template: TemplateParser = lazy(() => syntax(Rule.none, 2, surround(
|
|
11
|
+
'{{', some(union([bracket, escsource]), '}'), '}}', true,
|
|
12
|
+
([, ns = []], rest) => [[html('span', { class: 'template' }, `{{${ns.join('').replace(/\x1B/g, '')}}}`)], rest],
|
|
13
|
+
([, ns = [], rest], next) => next[0] === '}' ? undefined : optimize('{{', ns, rest))));
|
|
11
14
|
|
|
12
|
-
const bracket: TemplateParser.BracketParser = lazy(() => union([
|
|
15
|
+
const bracket: TemplateParser.BracketParser = lazy(() => creator(union([
|
|
13
16
|
surround(str('('), some(union([bracket, escsource]), ')'), str(')'), true, undefined, ([as, bs = []], rest) => [unshift(as, bs), rest]),
|
|
14
17
|
surround(str('['), some(union([bracket, escsource]), ']'), str(']'), true, undefined, ([as, bs = []], rest) => [unshift(as, bs), rest]),
|
|
15
18
|
surround(str('{'), some(union([bracket, escsource]), '}'), str('}'), true, undefined, ([as, bs = []], rest) => [unshift(as, bs), rest]),
|
|
16
19
|
surround(str('"'), precedence(8, some(escsource, /^"|^\\?\n/)), str('"'), true),
|
|
17
|
-
]));
|
|
20
|
+
])));
|
|
@@ -146,6 +146,7 @@ describe('Unit: parser/inline', () => {
|
|
|
146
146
|
assert.deepStrictEqual(inspect(parser('((((a))')), [['', '((', '<sup class="annotation"><span>a</span></sup>'], '']);
|
|
147
147
|
assert.deepStrictEqual(inspect(parser('((((a))))')), [['<sup class="annotation"><span><span class="paren">((a))</span></span></sup>'], '']);
|
|
148
148
|
assert.deepStrictEqual(inspect(parser('((<bdi>))')), [['<sup class="annotation"><span><span class="invalid"><bdi></span></span></sup>'], '']);
|
|
149
|
+
assert.deepStrictEqual(inspect(parser('((${))}$')), [['', '((', '<span class="math" translate="no" data-src="${))}$">${))}$</span>'], '']);
|
|
149
150
|
assert.deepStrictEqual(inspect(parser('"((""))')), [['"', '<sup class="annotation"><span>""</span></sup>'], '']);
|
|
150
151
|
assert.deepStrictEqual(inspect(parser('[[[a]]')), [['', '[', '<sup class="reference"><span>a</span></sup>'], '']);
|
|
151
152
|
assert.deepStrictEqual(inspect(parser('[[[[a]]')), [['', '[[', '<sup class="reference"><span>a</span></sup>'], '']);
|
|
@@ -155,6 +156,7 @@ describe('Unit: parser/inline', () => {
|
|
|
155
156
|
assert.deepStrictEqual(inspect(parser('[[[a]{b}]]')), [['<sup class="reference"><span><a href="b">a</a></span></sup>'], '']);
|
|
156
157
|
assert.deepStrictEqual(inspect(parser('[(([a]{#}))]{#}')), [['<a href="#"><span class="paren">(<span class="paren">([a]{#})</span>)</span></a>'], '']);
|
|
157
158
|
assert.deepStrictEqual(inspect(parser('[[<bdi>]]')), [['<sup class="reference"><span><span class="invalid"><bdi></span></span></sup>'], '']);
|
|
159
|
+
assert.deepStrictEqual(inspect(parser('[[${]]}$')), [['', '[[', '<span class="math" translate="no" data-src="${]]}$">${]]}$</span>'], '']);
|
|
158
160
|
assert.deepStrictEqual(inspect(parser('"[[""]]')), [['"', '<sup class="reference"><span>""</span></sup>'], '']);
|
|
159
161
|
assert.deepStrictEqual(inspect(parser('[[a](b)]{c}')), [['<a href="c"><ruby>a<rp>(</rp><rt>b</rt><rp>)</rp></ruby></a>'], '']);
|
|
160
162
|
assert.deepStrictEqual(inspect(parser('<http://host>')), [['<', '<a href="http://host" target="_blank">http://host</a>', '>'], '']);
|
package/src/parser/locale.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { Parser } from '../combinator/data/parser';
|
|
|
2
2
|
import { fmap } from '../combinator';
|
|
3
3
|
import { japanese } from './locale/ja';
|
|
4
4
|
import { html } from 'typed-dom/dom';
|
|
5
|
+
import { duffEach } from 'spica/duff';
|
|
5
6
|
|
|
6
7
|
export function localize<P extends Parser<HTMLElement | string>>(parser: P): P;
|
|
7
8
|
export function localize(parser: Parser<HTMLElement | string>): Parser<HTMLElement | string> {
|
|
@@ -10,13 +11,11 @@ export function localize(parser: Parser<HTMLElement | string>): Parser<HTMLEleme
|
|
|
10
11
|
const el = ns.length === 1 && typeof ns[0] === 'object'
|
|
11
12
|
? ns[0]
|
|
12
13
|
: html('div', ns);
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
sb.firstChild!.remove();
|
|
19
|
-
}
|
|
14
|
+
duffEach(el.querySelectorAll('.linebreak:not(:empty)'), el => {
|
|
15
|
+
assert(el.firstChild!.textContent === ' ');
|
|
16
|
+
if (!check(el)) return;
|
|
17
|
+
el.firstChild!.remove();
|
|
18
|
+
});
|
|
20
19
|
return ns;
|
|
21
20
|
});
|
|
22
21
|
}
|
|
@@ -2,6 +2,7 @@ import { undefined, Infinity, Map, Node } from 'spica/global';
|
|
|
2
2
|
import { text } from '../inline/extension/indexee';
|
|
3
3
|
import { frag, html, define } from 'typed-dom/dom';
|
|
4
4
|
import { MultiMap } from 'spica/multimap';
|
|
5
|
+
import { duffEach, duffReduce } from 'spica/duff';
|
|
5
6
|
import { push } from 'spica/array';
|
|
6
7
|
|
|
7
8
|
export function* footnote(
|
|
@@ -12,7 +13,7 @@ export function* footnote(
|
|
|
12
13
|
): Generator<HTMLAnchorElement | HTMLLIElement | undefined, undefined, undefined> {
|
|
13
14
|
// Bug: Firefox
|
|
14
15
|
//target.querySelectorAll(`:scope > .annotations`).forEach(el => el.remove());
|
|
15
|
-
target.querySelectorAll(`.annotations`)
|
|
16
|
+
duffEach(target.querySelectorAll(`.annotations`), el => el.parentNode === target && el.remove());
|
|
16
17
|
yield* reference(target, footnotes?.references, opts, bottom);
|
|
17
18
|
yield* annotation(target, footnotes?.annotations, opts, bottom);
|
|
18
19
|
return;
|
|
@@ -40,8 +41,9 @@ function build(
|
|
|
40
41
|
const titles = new Map<string, string>();
|
|
41
42
|
// Bug: Firefox
|
|
42
43
|
//const splitters = push([], target.querySelectorAll(`:scope > :is(${splitter ?? '_'})`));
|
|
43
|
-
const splitters =
|
|
44
|
-
|
|
44
|
+
const splitters = duffReduce(target.querySelectorAll(splitter ?? '_'), (acc, el) =>
|
|
45
|
+
el.parentNode === target ? push(acc, [el]) : acc
|
|
46
|
+
, [] as Element[]);
|
|
45
47
|
let count = 0;
|
|
46
48
|
let total = 0;
|
|
47
49
|
let style: 'count' | 'abbr';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { undefined } from 'spica/global';
|
|
2
2
|
import { TextParser, TxtParser, LinebreakParser } from '../source';
|
|
3
|
-
import { union,
|
|
3
|
+
import { union, creator, focus } from '../../combinator';
|
|
4
4
|
import { str } from './str';
|
|
5
5
|
import { html } from 'typed-dom/dom';
|
|
6
6
|
|
package/src/parser/util.ts
CHANGED
|
@@ -1,223 +1,3 @@
|
|
|
1
|
-
import { undefined } from 'spica/global';
|
|
2
|
-
import { MarkdownParser } from '../../markdown';
|
|
3
|
-
import { Parser, eval } from '../combinator/data/parser';
|
|
4
|
-
import { union, some, verify, convert, fmap } from '../combinator';
|
|
5
|
-
import { unsafehtmlentity } from './inline/htmlentity';
|
|
6
|
-
import { linebreak, unescsource } from './source';
|
|
7
|
-
import { invisibleHTMLEntityNames } from './api/normalize';
|
|
8
|
-
import { memoize, reduce } from 'spica/memoize';
|
|
9
|
-
import { push } from 'spica/array';
|
|
10
|
-
|
|
11
|
-
export function clean<P extends Parser<unknown>>(parser: P): P;
|
|
12
|
-
export function clean<T>(parser: Parser<T, MarkdownParser.Context>): Parser<T, MarkdownParser.Context> {
|
|
13
|
-
const clean = memoize<MarkdownParser.Context, MarkdownParser.Context>(context => ({
|
|
14
|
-
resources: context.resources,
|
|
15
|
-
precedence: context.precedence,
|
|
16
|
-
delimiters: context.delimiters,
|
|
17
|
-
host: context.host,
|
|
18
|
-
url: context.url,
|
|
19
|
-
id: context.id,
|
|
20
|
-
header: context.header,
|
|
21
|
-
cache: context.caches,
|
|
22
|
-
}), new WeakMap());
|
|
23
|
-
return (source, context) =>
|
|
24
|
-
parser(source, context.syntax ? clean(context) : context);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export const regBlankStart = new RegExp(
|
|
28
|
-
/^(?:\\?[^\S\n]|&IHN;|<wbr>)+/.source.replace('IHN', `(?:${invisibleHTMLEntityNames.join('|')})`));
|
|
29
|
-
|
|
30
|
-
export function blankWith(delimiter: string | RegExp): RegExp;
|
|
31
|
-
export function blankWith(starting: '' | '\n', delimiter: string | RegExp): RegExp;
|
|
32
|
-
export function blankWith(starting: '' | '\n', delimiter?: string | RegExp): RegExp {
|
|
33
|
-
if (delimiter === undefined) return blankWith('', starting);
|
|
34
|
-
return new RegExp(String.raw
|
|
35
|
-
`^(?:(?=${
|
|
36
|
-
starting
|
|
37
|
-
})(?:\\?\s|&(?:${invisibleHTMLEntityNames.join('|')});|<wbr>)${starting && '+'})?${
|
|
38
|
-
typeof delimiter === 'string' ? delimiter.replace(/[*+()\[\]]/g, '\\$&') : delimiter.source
|
|
39
|
-
}`);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export function visualize<P extends Parser<HTMLElement | string>>(parser: P): P;
|
|
43
|
-
export function visualize<T extends HTMLElement | string>(parser: Parser<T>): Parser<T> {
|
|
44
|
-
const blankline = new RegExp(
|
|
45
|
-
/^(?:\\$|\\?[^\S\n]|&IHN;|<wbr>)+$/.source.replace('IHN', `(?:${invisibleHTMLEntityNames.join('|')})`),
|
|
46
|
-
'gm');
|
|
47
|
-
return union([
|
|
48
|
-
convert(
|
|
49
|
-
source => source.replace(blankline, line => line.replace(/[\\&<]/g, '\x1B$&')),
|
|
50
|
-
verify(parser, (ns, rest, context) => !rest && hasVisible(ns, context))),
|
|
51
|
-
some(union([linebreak, unescsource])),
|
|
52
|
-
]);
|
|
53
|
-
}
|
|
54
|
-
function hasVisible(
|
|
55
|
-
nodes: readonly (HTMLElement | string)[],
|
|
56
|
-
{ syntax: { inline: { media = true } = {} } = {} }: MarkdownParser.Context = {},
|
|
57
|
-
): boolean {
|
|
58
|
-
for (let i = 0; i < nodes.length; ++i) {
|
|
59
|
-
const node = nodes[i];
|
|
60
|
-
if (typeof node === 'string') {
|
|
61
|
-
if (node && node.trimStart()) return true;
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
64
|
-
if (node.innerText.trimStart()) return true;
|
|
65
|
-
if (media && (node.classList.contains('media') || node.getElementsByClassName('media')[0])) return true;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
return false;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export function startLoose<P extends Parser<HTMLElement | string>>(parser: P, except?: string): P;
|
|
72
|
-
export function startLoose<T extends HTMLElement | string>(parser: Parser<T>, except?: string): Parser<T> {
|
|
73
|
-
return (source, context) =>
|
|
74
|
-
isStartLoose(source, context, except)
|
|
75
|
-
? parser(source, context)
|
|
76
|
-
: undefined;
|
|
77
|
-
}
|
|
78
|
-
const isStartLoose = reduce((source: string, context: MarkdownParser.Context, except?: string): boolean => {
|
|
79
|
-
return isStartTight(source.replace(regBlankStart, ''), context, except);
|
|
80
|
-
}, (source, _, except = '') => `${source}\x1E${except}`);
|
|
81
|
-
|
|
82
|
-
export function startTight<P extends Parser<unknown>>(parser: P, except?: string): P;
|
|
83
|
-
export function startTight<T>(parser: Parser<T>, except?: string): Parser<T> {
|
|
84
|
-
return (source, context) =>
|
|
85
|
-
isStartTight(source, context, except)
|
|
86
|
-
? parser(source, context)
|
|
87
|
-
: undefined;
|
|
88
|
-
}
|
|
89
|
-
const isStartTight = reduce((source: string, context: MarkdownParser.Context, except?: string): boolean => {
|
|
90
|
-
if (source === '') return true;
|
|
91
|
-
if (except && source.slice(0, except.length) === except) return false;
|
|
92
|
-
switch (source[0]) {
|
|
93
|
-
case ' ':
|
|
94
|
-
case ' ':
|
|
95
|
-
case '\t':
|
|
96
|
-
case '\n':
|
|
97
|
-
return false;
|
|
98
|
-
case '\\':
|
|
99
|
-
return source[1]?.trimStart() !== '';
|
|
100
|
-
case '&':
|
|
101
|
-
switch (true) {
|
|
102
|
-
case source.length > 2
|
|
103
|
-
&& source[1] !== ' '
|
|
104
|
-
&& eval(unsafehtmlentity(source, context))?.[0]?.trimStart() === '':
|
|
105
|
-
return false;
|
|
106
|
-
}
|
|
107
|
-
return true;
|
|
108
|
-
case '<':
|
|
109
|
-
switch (true) {
|
|
110
|
-
case source.length >= 5
|
|
111
|
-
&& source[1] === 'w'
|
|
112
|
-
&& source.slice(0, 5) === '<wbr>':
|
|
113
|
-
return false;
|
|
114
|
-
}
|
|
115
|
-
return true;
|
|
116
|
-
default:
|
|
117
|
-
return source[0].trimStart() !== '';
|
|
118
|
-
}
|
|
119
|
-
}, (source, _, except = '') => `${source}\x1E${except}`);
|
|
120
|
-
|
|
121
|
-
export function isStartLooseNodes(nodes: readonly (HTMLElement | string)[]): boolean {
|
|
122
|
-
if (nodes.length === 0) return true;
|
|
123
|
-
for (let i = 0; i < nodes.length; ++i) {
|
|
124
|
-
const node = nodes[i];
|
|
125
|
-
if (isVisible(node)) return true;
|
|
126
|
-
if (typeof node === 'object') {
|
|
127
|
-
if (node.tagName === 'BR') break;
|
|
128
|
-
if (node.className === 'linebreak') break;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
return false;
|
|
132
|
-
}
|
|
133
|
-
export function isStartTightNodes(nodes: readonly (HTMLElement | string)[]): boolean {
|
|
134
|
-
if (nodes.length === 0) return true;
|
|
135
|
-
return isVisible(nodes[0], 0);
|
|
136
|
-
}
|
|
137
|
-
//export function isEndTightNodes(nodes: readonly (HTMLElement | string)[]): boolean {
|
|
138
|
-
// if (nodes.length === 0) return true;
|
|
139
|
-
// return isVisible(nodes[nodes.length - 1], -1);
|
|
140
|
-
//}
|
|
141
|
-
function isVisible(node: HTMLElement | string, strpos?: number): boolean {
|
|
142
|
-
switch (typeof node) {
|
|
143
|
-
case 'string':
|
|
144
|
-
const char = node && strpos !== undefined
|
|
145
|
-
? node[strpos >= 0 ? strpos : node.length + strpos]
|
|
146
|
-
: node;
|
|
147
|
-
switch (char) {
|
|
148
|
-
case '':
|
|
149
|
-
case ' ':
|
|
150
|
-
case '\t':
|
|
151
|
-
case '\n':
|
|
152
|
-
return false;
|
|
153
|
-
default:
|
|
154
|
-
return char.trimStart() !== '';
|
|
155
|
-
}
|
|
156
|
-
default:
|
|
157
|
-
switch (node.tagName) {
|
|
158
|
-
case 'BR':
|
|
159
|
-
case 'WBR':
|
|
160
|
-
return false;
|
|
161
|
-
case 'SPAN':
|
|
162
|
-
return node.className !== 'linebreak';
|
|
163
|
-
default:
|
|
164
|
-
return true;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
export function trimBlank<P extends Parser<HTMLElement | string>>(parser: P): P;
|
|
170
|
-
export function trimBlank<T extends HTMLElement | string>(parser: Parser<T>): Parser<T> {
|
|
171
|
-
return trimBlankStart(trimBlankEnd(parser));
|
|
172
|
-
}
|
|
173
|
-
export function trimBlankStart<P extends Parser<unknown>>(parser: P): P;
|
|
174
|
-
export function trimBlankStart<T>(parser: Parser<T>): Parser<T> {
|
|
175
|
-
return convert(
|
|
176
|
-
reduce(source => source.replace(regBlankStart, '')),
|
|
177
|
-
parser);
|
|
178
|
-
}
|
|
179
|
-
export function trimBlankEnd<P extends Parser<HTMLElement | string>>(parser: P): P;
|
|
180
|
-
export function trimBlankEnd<T extends HTMLElement | string>(parser: Parser<T>): Parser<T> {
|
|
181
|
-
return fmap(
|
|
182
|
-
parser,
|
|
183
|
-
trimNodeEnd);
|
|
184
|
-
}
|
|
185
|
-
export function trimNode<T extends HTMLElement | string>(nodes: T[]): T[] {
|
|
186
|
-
return trimNodeStart(trimNodeEnd(nodes));
|
|
187
|
-
}
|
|
188
|
-
function trimNodeStart<T extends HTMLElement | string>(nodes: T[]): T[] {
|
|
189
|
-
for (let node = nodes[0]; nodes.length > 0 && !isVisible(node = nodes[0], 0);) {
|
|
190
|
-
if (nodes.length === 1 && typeof node === 'object' && node.className === 'indexer') break;
|
|
191
|
-
if (typeof node === 'string') {
|
|
192
|
-
const pos = node.trimStart().length;
|
|
193
|
-
if (pos > 0) {
|
|
194
|
-
nodes[0] = node.slice(pos) as T;
|
|
195
|
-
break;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
nodes.shift();
|
|
199
|
-
}
|
|
200
|
-
return nodes;
|
|
201
|
-
}
|
|
202
|
-
function trimNodeEnd<T extends HTMLElement | string>(nodes: T[]): T[] {
|
|
203
|
-
const skip = nodes.length > 0 &&
|
|
204
|
-
typeof nodes[nodes.length - 1] === 'object' &&
|
|
205
|
-
nodes[nodes.length - 1]['className'] === 'indexer'
|
|
206
|
-
? [nodes.pop()!]
|
|
207
|
-
: [];
|
|
208
|
-
for (let node = nodes[0]; nodes.length > 0 && !isVisible(node = nodes[nodes.length - 1], -1);) {
|
|
209
|
-
if (typeof node === 'string') {
|
|
210
|
-
const pos = node.trimEnd().length;
|
|
211
|
-
if (pos > 0) {
|
|
212
|
-
nodes[nodes.length - 1] = node.slice(0, pos) as T;
|
|
213
|
-
break;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
nodes.pop();
|
|
217
|
-
}
|
|
218
|
-
return push(nodes, skip);
|
|
219
|
-
}
|
|
220
|
-
|
|
221
1
|
export function stringify(nodes: readonly (HTMLElement | string)[]): string {
|
|
222
2
|
let acc = '';
|
|
223
3
|
for (let i = 0; i < nodes.length; ++i) {
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { undefined } from 'spica/global';
|
|
2
|
+
import { MarkdownParser } from '../../markdown';
|
|
3
|
+
import { Parser, eval } from '../combinator/data/parser';
|
|
4
|
+
import { union, some, verify, convert, fmap } from '../combinator';
|
|
5
|
+
import { unsafehtmlentity } from './inline/htmlentity';
|
|
6
|
+
import { linebreak, unescsource } from './source';
|
|
7
|
+
import { State } from './context';
|
|
8
|
+
import { invisibleHTMLEntityNames } from './api/normalize';
|
|
9
|
+
import { reduce } from 'spica/memoize';
|
|
10
|
+
import { push } from 'spica/array';
|
|
11
|
+
|
|
12
|
+
export function visualize<P extends Parser<HTMLElement | string>>(parser: P): P;
|
|
13
|
+
export function visualize<T extends HTMLElement | string>(parser: Parser<T>): Parser<T> {
|
|
14
|
+
const blankline = new RegExp(
|
|
15
|
+
/^(?:\\$|\\?[^\S\n]|&IHN;|<wbr>)+$/.source.replace('IHN', `(?:${invisibleHTMLEntityNames.join('|')})`),
|
|
16
|
+
'gm');
|
|
17
|
+
return union([
|
|
18
|
+
convert(
|
|
19
|
+
source => source.replace(blankline, line => line.replace(/[\\&<]/g, '\x1B$&')),
|
|
20
|
+
verify(parser, (ns, rest, context) => !rest && hasVisible(ns, context))),
|
|
21
|
+
some(union([linebreak, unescsource])),
|
|
22
|
+
]);
|
|
23
|
+
}
|
|
24
|
+
function hasVisible(
|
|
25
|
+
nodes: readonly (HTMLElement | string)[],
|
|
26
|
+
{ state = 0 }: MarkdownParser.Context = {},
|
|
27
|
+
): boolean {
|
|
28
|
+
for (let i = 0; i < nodes.length; ++i) {
|
|
29
|
+
const node = nodes[i];
|
|
30
|
+
if (typeof node === 'string') {
|
|
31
|
+
if (node && node.trimStart()) return true;
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
if (node.innerText.trimStart()) return true;
|
|
35
|
+
if (state & State.media ^ State.media &&
|
|
36
|
+
(node.classList.contains('media') || node.getElementsByClassName('media')[0])) return true;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const regBlankStart = new RegExp(
|
|
43
|
+
/^(?:\\?[^\S\n]|&IHN;|<wbr>)+/.source.replace('IHN', `(?:${invisibleHTMLEntityNames.join('|')})`));
|
|
44
|
+
|
|
45
|
+
export function blankWith(delimiter: string | RegExp): RegExp;
|
|
46
|
+
export function blankWith(starting: '' | '\n', delimiter: string | RegExp): RegExp;
|
|
47
|
+
export function blankWith(starting: '' | '\n', delimiter?: string | RegExp): RegExp {
|
|
48
|
+
if (delimiter === undefined) return blankWith('', starting);
|
|
49
|
+
return new RegExp(String.raw
|
|
50
|
+
`^(?:(?=${
|
|
51
|
+
starting
|
|
52
|
+
})(?:\\?\s|&(?:${invisibleHTMLEntityNames.join('|')});|<wbr>)${starting && '+'})?${
|
|
53
|
+
typeof delimiter === 'string' ? delimiter.replace(/[*+()\[\]]/g, '\\$&') : delimiter.source
|
|
54
|
+
}`);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function startLoose<P extends Parser<HTMLElement | string>>(parser: P, except?: string): P;
|
|
58
|
+
export function startLoose<T extends HTMLElement | string>(parser: Parser<T>, except?: string): Parser<T> {
|
|
59
|
+
return (source, context) =>
|
|
60
|
+
isStartLoose(source, context, except)
|
|
61
|
+
? parser(source, context)
|
|
62
|
+
: undefined;
|
|
63
|
+
}
|
|
64
|
+
const isStartLoose = reduce((source: string, context: MarkdownParser.Context, except?: string): boolean => {
|
|
65
|
+
return isStartTight(source.replace(regBlankStart, ''), context, except);
|
|
66
|
+
}, (source, _, except = '') => `${source}\x1E${except}`);
|
|
67
|
+
|
|
68
|
+
export function startTight<P extends Parser<unknown>>(parser: P, except?: string): P;
|
|
69
|
+
export function startTight<T>(parser: Parser<T>, except?: string): Parser<T> {
|
|
70
|
+
return (source, context) =>
|
|
71
|
+
isStartTight(source, context, except)
|
|
72
|
+
? parser(source, context)
|
|
73
|
+
: undefined;
|
|
74
|
+
}
|
|
75
|
+
const isStartTight = reduce((source: string, context: MarkdownParser.Context, except?: string): boolean => {
|
|
76
|
+
if (source === '') return true;
|
|
77
|
+
if (except && source.slice(0, except.length) === except) return false;
|
|
78
|
+
switch (source[0]) {
|
|
79
|
+
case ' ':
|
|
80
|
+
case ' ':
|
|
81
|
+
case '\t':
|
|
82
|
+
case '\n':
|
|
83
|
+
return false;
|
|
84
|
+
case '\\':
|
|
85
|
+
return source[1]?.trimStart() !== '';
|
|
86
|
+
case '&':
|
|
87
|
+
switch (true) {
|
|
88
|
+
case source.length > 2
|
|
89
|
+
&& source[1] !== ' '
|
|
90
|
+
&& eval(unsafehtmlentity(source, context))?.[0]?.trimStart() === '':
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
return true;
|
|
94
|
+
case '<':
|
|
95
|
+
switch (true) {
|
|
96
|
+
case source.length >= 5
|
|
97
|
+
&& source[1] === 'w'
|
|
98
|
+
&& source.slice(0, 5) === '<wbr>':
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
return true;
|
|
102
|
+
default:
|
|
103
|
+
return source[0].trimStart() !== '';
|
|
104
|
+
}
|
|
105
|
+
}, (source, _, except = '') => `${source}\x1E${except}`);
|
|
106
|
+
|
|
107
|
+
export function isStartLooseNodes(nodes: readonly (HTMLElement | string)[]): boolean {
|
|
108
|
+
if (nodes.length === 0) return true;
|
|
109
|
+
for (let i = 0; i < nodes.length; ++i) {
|
|
110
|
+
const node = nodes[i];
|
|
111
|
+
if (isVisible(node)) return true;
|
|
112
|
+
if (typeof node === 'object') {
|
|
113
|
+
if (node.tagName === 'BR') break;
|
|
114
|
+
if (node.className === 'linebreak') break;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
export function isStartTightNodes(nodes: readonly (HTMLElement | string)[]): boolean {
|
|
120
|
+
if (nodes.length === 0) return true;
|
|
121
|
+
return isVisible(nodes[0], 0);
|
|
122
|
+
}
|
|
123
|
+
//export function isEndTightNodes(nodes: readonly (HTMLElement | string)[]): boolean {
|
|
124
|
+
// if (nodes.length === 0) return true;
|
|
125
|
+
// return isVisible(nodes[nodes.length - 1], -1);
|
|
126
|
+
//}
|
|
127
|
+
function isVisible(node: HTMLElement | string, strpos?: number): boolean {
|
|
128
|
+
switch (typeof node) {
|
|
129
|
+
case 'string':
|
|
130
|
+
const char = node && strpos !== undefined
|
|
131
|
+
? node[strpos >= 0 ? strpos : node.length + strpos]
|
|
132
|
+
: node;
|
|
133
|
+
switch (char) {
|
|
134
|
+
case '':
|
|
135
|
+
case ' ':
|
|
136
|
+
case '\t':
|
|
137
|
+
case '\n':
|
|
138
|
+
return false;
|
|
139
|
+
default:
|
|
140
|
+
return char.trimStart() !== '';
|
|
141
|
+
}
|
|
142
|
+
default:
|
|
143
|
+
switch (node.tagName) {
|
|
144
|
+
case 'BR':
|
|
145
|
+
case 'WBR':
|
|
146
|
+
return false;
|
|
147
|
+
case 'SPAN':
|
|
148
|
+
return node.className !== 'linebreak';
|
|
149
|
+
default:
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function trimBlank<P extends Parser<HTMLElement | string>>(parser: P): P;
|
|
156
|
+
export function trimBlank<T extends HTMLElement | string>(parser: Parser<T>): Parser<T> {
|
|
157
|
+
return trimBlankStart(trimBlankEnd(parser));
|
|
158
|
+
}
|
|
159
|
+
export function trimBlankStart<P extends Parser<unknown>>(parser: P): P;
|
|
160
|
+
export function trimBlankStart<T>(parser: Parser<T>): Parser<T> {
|
|
161
|
+
return convert(
|
|
162
|
+
reduce(source => source.replace(regBlankStart, '')),
|
|
163
|
+
parser);
|
|
164
|
+
}
|
|
165
|
+
export function trimBlankEnd<P extends Parser<HTMLElement | string>>(parser: P): P;
|
|
166
|
+
export function trimBlankEnd<T extends HTMLElement | string>(parser: Parser<T>): Parser<T> {
|
|
167
|
+
return fmap(
|
|
168
|
+
parser,
|
|
169
|
+
trimNodeEnd);
|
|
170
|
+
}
|
|
171
|
+
export function trimNode<T extends HTMLElement | string>(nodes: T[]): T[] {
|
|
172
|
+
return trimNodeStart(trimNodeEnd(nodes));
|
|
173
|
+
}
|
|
174
|
+
function trimNodeStart<T extends HTMLElement | string>(nodes: T[]): T[] {
|
|
175
|
+
for (let node = nodes[0]; nodes.length > 0 && !isVisible(node = nodes[0], 0);) {
|
|
176
|
+
if (nodes.length === 1 && typeof node === 'object' && node.className === 'indexer') break;
|
|
177
|
+
if (typeof node === 'string') {
|
|
178
|
+
const pos = node.trimStart().length;
|
|
179
|
+
if (pos > 0) {
|
|
180
|
+
nodes[0] = node.slice(-pos) as T;
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
nodes.shift();
|
|
185
|
+
}
|
|
186
|
+
return nodes;
|
|
187
|
+
}
|
|
188
|
+
function trimNodeEnd<T extends HTMLElement | string>(nodes: T[]): T[] {
|
|
189
|
+
const skip = nodes.length > 0 &&
|
|
190
|
+
typeof nodes[nodes.length - 1] === 'object' &&
|
|
191
|
+
nodes[nodes.length - 1]['className'] === 'indexer'
|
|
192
|
+
? [nodes.pop()!]
|
|
193
|
+
: [];
|
|
194
|
+
for (let node = nodes[0]; nodes.length > 0 && !isVisible(node = nodes[nodes.length - 1], -1);) {
|
|
195
|
+
if (typeof node === 'string') {
|
|
196
|
+
const pos = node.trimEnd().length;
|
|
197
|
+
if (pos > 0) {
|
|
198
|
+
nodes[nodes.length - 1] = node.slice(0, pos) as T;
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
nodes.pop();
|
|
203
|
+
}
|
|
204
|
+
return push(nodes, skip);
|
|
205
|
+
}
|
package/src/util/info.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Info } from '../..';
|
|
2
2
|
import { scope } from './scope';
|
|
3
|
+
import { duffReduce } from 'spica/duff';
|
|
3
4
|
import { push } from 'spica/array';
|
|
4
5
|
|
|
5
6
|
export function info(source: DocumentFragment | HTMLElement | ShadowRoot): Info {
|
|
@@ -20,7 +21,8 @@ export function info(source: DocumentFragment | HTMLElement | ShadowRoot): Info
|
|
|
20
21
|
};
|
|
21
22
|
|
|
22
23
|
function find<T extends HTMLElement>(selector: string): T[] {
|
|
23
|
-
return
|
|
24
|
-
|
|
24
|
+
return duffReduce(source.querySelectorAll<T>(selector), (acc, el) =>
|
|
25
|
+
match(el) ? push(acc, [el]) : acc
|
|
26
|
+
, [] as T[]);
|
|
25
27
|
}
|
|
26
28
|
}
|
package/src/util/quote.ts
CHANGED
|
@@ -2,32 +2,30 @@ import { Element } from 'spica/global';
|
|
|
2
2
|
import { exec } from '../combinator/data/parser';
|
|
3
3
|
import { cite } from '../parser/block/reply/cite';
|
|
4
4
|
import { define } from 'typed-dom/dom';
|
|
5
|
+
import { duffEach } from 'spica/duff';
|
|
5
6
|
|
|
6
7
|
export function quote(anchor: string, range: Range): string {
|
|
7
8
|
if (exec(cite(`>>${anchor}`, {})) !== '') throw new Error(`Invalid anchor: ${anchor}`);
|
|
8
9
|
fit(range);
|
|
9
10
|
const node = trim(range.cloneContents());
|
|
10
11
|
if (!node.firstChild) return '';
|
|
11
|
-
|
|
12
|
-
let es = node.querySelectorAll('code[data-src], .math[data-src], .media[data-src], rt, rp'),
|
|
13
|
-
i = 0, len = es.length; i < len; ++i) {
|
|
14
|
-
const el = es[i];
|
|
12
|
+
duffEach(node.querySelectorAll('code[data-src], .math[data-src], .media[data-src], rt, rp'), el => {
|
|
15
13
|
switch (true) {
|
|
16
14
|
case el.matches('code'):
|
|
17
15
|
case el.matches('.math'):
|
|
18
16
|
define(el, el.getAttribute('data-src')!);
|
|
19
|
-
|
|
17
|
+
return;
|
|
20
18
|
case el.matches('.media'):
|
|
21
19
|
el.replaceWith(
|
|
22
20
|
/[\s{}]/.test(el.getAttribute('data-src')!)
|
|
23
21
|
? `!{ ${el.getAttribute('data-src')} }`
|
|
24
22
|
: `!{${el.getAttribute('data-src')}}`);
|
|
25
|
-
|
|
23
|
+
return;
|
|
26
24
|
case el.matches('rt, rp'):
|
|
27
25
|
el.remove();
|
|
28
|
-
|
|
26
|
+
return;
|
|
29
27
|
}
|
|
30
|
-
}
|
|
28
|
+
});
|
|
31
29
|
if (range.startOffset === 0 &&
|
|
32
30
|
range.startContainer.parentElement?.matches('.cite, .quote') &&
|
|
33
31
|
(!range.startContainer.previousSibling || range.startContainer.previousSibling.nodeName === 'BR')) {
|
|
@@ -37,26 +35,25 @@ export function quote(anchor: string, range: Range): string {
|
|
|
37
35
|
node.prepend(`>>${anchor}\n> `);
|
|
38
36
|
anchor = '';
|
|
39
37
|
}
|
|
40
|
-
|
|
41
|
-
const el = es[i];
|
|
38
|
+
duffEach(node.querySelectorAll('br'), el => {
|
|
42
39
|
if (anchor && el.nextSibling instanceof Element && el.nextSibling.matches('.cite, .quote')) {
|
|
43
40
|
el.replaceWith(`\n>${el.nextSibling.matches('.quote.invalid') ? ' ' : ''}`);
|
|
44
|
-
|
|
41
|
+
return;
|
|
45
42
|
}
|
|
46
43
|
if (anchor && el.parentElement?.closest('.cite, .quote')) {
|
|
47
44
|
el.replaceWith(`\n>${el.parentElement.closest('.quote.invalid') ? ' ' : ''}`);
|
|
48
|
-
|
|
45
|
+
return;
|
|
49
46
|
}
|
|
50
47
|
if (anchor) {
|
|
51
48
|
el.replaceWith(`\n>>${anchor}\n> `);
|
|
52
49
|
anchor = '';
|
|
53
|
-
|
|
50
|
+
return;
|
|
54
51
|
}
|
|
55
52
|
else {
|
|
56
53
|
el.replaceWith(`\n> `);
|
|
57
|
-
|
|
54
|
+
return;
|
|
58
55
|
}
|
|
59
|
-
}
|
|
56
|
+
});
|
|
60
57
|
anchor && node.append(`\n>>${anchor}`);
|
|
61
58
|
return node.textContent!;
|
|
62
59
|
}
|