text-vide
Version:
An Open-Source JavaScript Implementation of Bionic Reading.
165 lines (164 loc) • 2.62 kB
JavaScript
const f = (t) => t == null || t === "", _ = (t, n) => Object.keys(t).reduce((e, s) => (n(e[s]) && delete e[s], e), t), A = (t, n) => ({
...n,
..._(t, f)
}), R = ["<b>", "</b>"];
const y = (t) => A(t, {
sep: R,
fixationPoint: 1,
ignoreHtmlTag: !0,
ignoreHtmlEntity: !0
}), E = [
[0, 4, 12, 17, 24, 29, 35, 42, 48],
[1, 2, 7, 10, 13, 14, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, 49],
[
1,
2,
5,
7,
9,
11,
13,
15,
17,
19,
21,
23,
25,
27,
29,
31,
33,
35,
37,
39,
41,
43,
45,
47,
49
],
[
0,
2,
4,
5,
6,
8,
9,
11,
14,
15,
17,
18,
20,
0,
21,
23,
24,
26,
27,
29,
30,
32,
33,
35,
36,
38,
39,
41,
42,
44,
45,
47,
48
],
[
0,
2,
3,
5,
6,
7,
8,
10,
11,
12,
14,
15,
17,
19,
20,
21,
23,
24,
25,
26,
28,
29,
30,
32,
33,
34,
35,
37,
38,
39,
41,
42,
43,
44,
46,
47,
48
]
], H = (t, n) => {
const { length: i } = t, e = E[n - 1] ?? E[0], s = e.findIndex(
(c) => i <= c
);
let o = i - s;
return s === -1 && (o = i - e.length), Math.max(o, 0);
}, N = (t, n) => typeof n == "string" ? `${n}${t}${n}` : `${n[0]}${t}${n[1]}`, m = (t) => Array.from(t).map((n) => {
const i = n.index, [e] = n, { length: s } = e;
return [i, i + s - 1];
}), x = /<!--[^]*?-->|<[^>]+>/g, O = (t) => {
const n = t.matchAll(x), e = m(n).reverse();
return (s) => {
const o = s.index, c = e.find(
([r]) => o > r
);
if (!c)
return !1;
const [, a] = c;
return o < a;
};
}, F = /&[\w#]+;/g, M = (t) => {
const n = t.matchAll(F), e = m(n).reverse();
return (s) => {
const o = s.index, c = e.find(
([r]) => o > r
);
if (!c)
return !1;
const [, a] = c;
return o < a;
};
}, G = /(\p{L}|\p{Nd})*\p{L}(\p{L}|\p{Nd})*/gu, U = (t, n = {}) => {
if (!(t != null && t.length))
return "";
const { fixationPoint: i, sep: e, ignoreHtmlTag: s, ignoreHtmlEntity: o } = y(n), c = Array.from(t.matchAll(G));
let a = "", l = 0, r;
s && (r = O(t));
let g;
o && (g = M(t));
for (const d of c) {
if ((r == null ? void 0 : r(d)) || (g == null ? void 0 : g(d)))
continue;
const [u] = d, T = d.index, L = T + H(u, i), I = t.slice(l, T);
a += I, T !== L && (a += N(t.slice(T, L), e)), l = L;
}
const h = t.slice(l);
return a + h;
};
export {
U as textVide
};