@podlite/highlight 0.0.4 → 0.0.6
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.podlite
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
=head1 Upcoming
|
|
4
4
|
|
|
5
|
+
=head1 0.0.5
|
|
6
|
+
|
|
7
|
+
=item hidden text in a code block reaches the highlighter masked, so it stays hidden once the block is highlighted, and a highlight made for draft is dropped when the page switches to production; the caption of a hidden block is masked too
|
|
8
|
+
|
|
5
9
|
=head1 0.0.4
|
|
6
10
|
|
|
7
11
|
=item the package can be imported from Node directly. The emitted ESM referenced its own files without an extension, which the ESM loader refuses
|
package/esm/HighlightedCode.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ type Decoration = {
|
|
|
7
7
|
class?: string;
|
|
8
8
|
};
|
|
9
9
|
};
|
|
10
|
-
export declare const extractPlainAndDecorations: (nodes: unknown) => {
|
|
10
|
+
export declare const extractPlainAndDecorations: (nodes: unknown, hides?: (node: unknown) => boolean) => {
|
|
11
11
|
plain: string;
|
|
12
12
|
decorations: Decoration[];
|
|
13
13
|
};
|
|
@@ -18,7 +18,11 @@ export type HighlightedCodeProps = {
|
|
|
18
18
|
};
|
|
19
19
|
children: React.ReactNode;
|
|
20
20
|
keyProp: string | number;
|
|
21
|
-
ctx:
|
|
21
|
+
ctx: {
|
|
22
|
+
config?: unknown;
|
|
23
|
+
maskMode?: boolean;
|
|
24
|
+
renderMode?: string;
|
|
25
|
+
} | undefined;
|
|
22
26
|
id?: string;
|
|
23
27
|
wrap?: 'pre-code' | 'block';
|
|
24
28
|
};
|
package/esm/HighlightedCode.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React, { useState, useEffect } from 'react';
|
|
2
|
-
import { makeAttrs } from '@podlite/schema';
|
|
2
|
+
import { isCovered, makeAttrs, maskText } from '@podlite/schema';
|
|
3
3
|
import { codeToThemedHtml } from './shiki.js';
|
|
4
4
|
const fcodeToTag = {
|
|
5
5
|
B: 'strong',
|
|
@@ -8,34 +8,33 @@ const fcodeToTag = {
|
|
|
8
8
|
U: 'u',
|
|
9
9
|
K: 'kbd',
|
|
10
10
|
};
|
|
11
|
-
|
|
11
|
+
// Text a node hides reaches the highlighter masked, one mask character per character,
|
|
12
|
+
// so the decorations keep their places.
|
|
13
|
+
export const extractPlainAndDecorations = (nodes, hides = () => false) => {
|
|
12
14
|
const decorations = [];
|
|
13
15
|
let plain = '';
|
|
14
|
-
const walk = (input) => {
|
|
16
|
+
const walk = (input, covered = false) => {
|
|
15
17
|
if (input == null)
|
|
16
18
|
return;
|
|
17
19
|
if (typeof input === 'string') {
|
|
18
|
-
plain += input;
|
|
20
|
+
plain += covered ? maskText(input) : input;
|
|
19
21
|
return;
|
|
20
22
|
}
|
|
21
23
|
if (Array.isArray(input)) {
|
|
22
24
|
for (const child of input)
|
|
23
|
-
walk(child);
|
|
25
|
+
walk(child, covered);
|
|
24
26
|
return;
|
|
25
27
|
}
|
|
28
|
+
covered = covered || hides(input);
|
|
26
29
|
const node = input;
|
|
27
|
-
if (node.type === 'text' && typeof node.value === 'string') {
|
|
28
|
-
plain += node.value;
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
if (node.type === 'verbatim' && typeof node.value === 'string') {
|
|
32
|
-
plain += node.value;
|
|
30
|
+
if ((node.type === 'text' || node.type === 'verbatim') && typeof node.value === 'string') {
|
|
31
|
+
plain += covered ? maskText(node.value) : node.value;
|
|
33
32
|
return;
|
|
34
33
|
}
|
|
35
34
|
if (node.type === 'fcode' && typeof node.name === 'string') {
|
|
36
35
|
const tagName = fcodeToTag[node.name] || 'span';
|
|
37
36
|
const start = plain.length;
|
|
38
|
-
walk(node.content);
|
|
37
|
+
walk(node.content, covered);
|
|
39
38
|
const end = plain.length;
|
|
40
39
|
if (end > start) {
|
|
41
40
|
decorations.push({
|
|
@@ -48,10 +47,10 @@ export const extractPlainAndDecorations = (nodes) => {
|
|
|
48
47
|
return;
|
|
49
48
|
}
|
|
50
49
|
if (node.content !== undefined) {
|
|
51
|
-
walk(node.content);
|
|
50
|
+
walk(node.content, covered);
|
|
52
51
|
}
|
|
53
52
|
else if (typeof node.text === 'string') {
|
|
54
|
-
plain += node.text;
|
|
53
|
+
plain += covered ? maskText(node.text) : node.text;
|
|
55
54
|
}
|
|
56
55
|
};
|
|
57
56
|
walk(nodes);
|
|
@@ -61,25 +60,30 @@ export const extractPlainAndDecorations = (nodes) => {
|
|
|
61
60
|
// a caller that says nothing gets the caption rather than silently losing it.
|
|
62
61
|
const HighlightedCode = React.memo(({ node, children, keyProp, ctx, id, wrap = 'block' }) => {
|
|
63
62
|
const conf = makeAttrs(node, ctx);
|
|
64
|
-
const
|
|
63
|
+
const written = conf.exists('caption') ? String(conf.getFirstValue('caption')) : null;
|
|
64
|
+
const caption = written !== null && isCovered(node, ctx) ? maskText(written) : written;
|
|
65
65
|
const lang = conf.getFirstValue('lang');
|
|
66
|
-
const
|
|
66
|
+
const { plain, decorations } = extractPlainAndDecorations(node.content, part => isCovered(part, { maskMode: ctx?.maskMode, renderMode: ctx?.renderMode }));
|
|
67
|
+
const isDark = typeof document !== 'undefined' && !!document.body && document.body.className.toLowerCase().includes('dark');
|
|
68
|
+
// What is asked of the highlighter, with hidden text already masked. A result is
|
|
69
|
+
// shown only for the request it answers: one made for another mode or theme never
|
|
70
|
+
// reaches the page, and the same text drawn again keeps its highlight.
|
|
71
|
+
const request = JSON.stringify([lang, isDark, plain, decorations]);
|
|
72
|
+
const [result, setResult] = useState(null);
|
|
67
73
|
useEffect(() => {
|
|
68
74
|
if (!lang)
|
|
69
75
|
return;
|
|
70
76
|
let cancelled = false;
|
|
71
77
|
const highlight = async () => {
|
|
72
78
|
try {
|
|
73
|
-
const
|
|
74
|
-
const { plain, decorations } = extractPlainAndDecorations(node.content);
|
|
75
|
-
const result = await codeToThemedHtml({
|
|
79
|
+
const html = await codeToThemedHtml({
|
|
76
80
|
code: plain,
|
|
77
81
|
language: lang,
|
|
78
82
|
theme: isDark ? 'dark' : 'light',
|
|
79
83
|
decorations,
|
|
80
84
|
});
|
|
81
85
|
if (!cancelled)
|
|
82
|
-
|
|
86
|
+
setResult({ request, html });
|
|
83
87
|
}
|
|
84
88
|
catch (e) {
|
|
85
89
|
console.error('[podlite] shiki highlight error:', e);
|
|
@@ -89,7 +93,8 @@ const HighlightedCode = React.memo(({ node, children, keyProp, ctx, id, wrap = '
|
|
|
89
93
|
return () => {
|
|
90
94
|
cancelled = true;
|
|
91
95
|
};
|
|
92
|
-
}, [
|
|
96
|
+
}, [request]);
|
|
97
|
+
const html = result && result.request === request ? result.html : null;
|
|
93
98
|
// The id goes on whichever element the branch puts outermost, so a link to
|
|
94
99
|
// the block keeps working once highlighting replaces the plain output.
|
|
95
100
|
const codeBody = html ? (React.createElement("code", { key: keyProp, id: id, className: "shiki", dangerouslySetInnerHTML: { __html: html } })) : (React.createElement("pre", { key: `${keyProp}-pre`, id: id },
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"HighlightedCode.js","sourceRoot":"","sources":["../src/HighlightedCode.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;
|
|
1
|
+
{"version":3,"file":"HighlightedCode.js","sourceRoot":"","sources":["../src/HighlightedCode.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAClD,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAA;AAChE,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAS1C,MAAM,UAAU,GAA2B;IACzC,CAAC,EAAE,QAAQ;IACX,CAAC,EAAE,IAAI;IACP,CAAC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ACP,CAAA;IACH,CAAC;IAED,OAAO,QAAQ,CAAA;AACjB,CAAC,CACF,CAAA;AAED,eAAe,CAAC,WAAW,GAAG,iBAAiB,CAAA;AAE/C,eAAe,eAAe,CAAA"}
|
package/lib/HighlightedCode.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ type Decoration = {
|
|
|
7
7
|
class?: string;
|
|
8
8
|
};
|
|
9
9
|
};
|
|
10
|
-
export declare const extractPlainAndDecorations: (nodes: unknown) => {
|
|
10
|
+
export declare const extractPlainAndDecorations: (nodes: unknown, hides?: (node: unknown) => boolean) => {
|
|
11
11
|
plain: string;
|
|
12
12
|
decorations: Decoration[];
|
|
13
13
|
};
|
|
@@ -18,7 +18,11 @@ export type HighlightedCodeProps = {
|
|
|
18
18
|
};
|
|
19
19
|
children: React.ReactNode;
|
|
20
20
|
keyProp: string | number;
|
|
21
|
-
ctx:
|
|
21
|
+
ctx: {
|
|
22
|
+
config?: unknown;
|
|
23
|
+
maskMode?: boolean;
|
|
24
|
+
renderMode?: string;
|
|
25
|
+
} | undefined;
|
|
22
26
|
id?: string;
|
|
23
27
|
wrap?: 'pre-code' | 'block';
|
|
24
28
|
};
|
package/lib/HighlightedCode.js
CHANGED
|
@@ -44,34 +44,33 @@ const fcodeToTag = {
|
|
|
44
44
|
U: 'u',
|
|
45
45
|
K: 'kbd',
|
|
46
46
|
};
|
|
47
|
-
|
|
47
|
+
// Text a node hides reaches the highlighter masked, one mask character per character,
|
|
48
|
+
// so the decorations keep their places.
|
|
49
|
+
const extractPlainAndDecorations = (nodes, hides = () => false) => {
|
|
48
50
|
const decorations = [];
|
|
49
51
|
let plain = '';
|
|
50
|
-
const walk = (input) => {
|
|
52
|
+
const walk = (input, covered = false) => {
|
|
51
53
|
if (input == null)
|
|
52
54
|
return;
|
|
53
55
|
if (typeof input === 'string') {
|
|
54
|
-
plain += input;
|
|
56
|
+
plain += covered ? (0, schema_1.maskText)(input) : input;
|
|
55
57
|
return;
|
|
56
58
|
}
|
|
57
59
|
if (Array.isArray(input)) {
|
|
58
60
|
for (const child of input)
|
|
59
|
-
walk(child);
|
|
61
|
+
walk(child, covered);
|
|
60
62
|
return;
|
|
61
63
|
}
|
|
64
|
+
covered = covered || hides(input);
|
|
62
65
|
const node = input;
|
|
63
|
-
if (node.type === 'text' && typeof node.value === 'string') {
|
|
64
|
-
plain += node.value;
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
if (node.type === 'verbatim' && typeof node.value === 'string') {
|
|
68
|
-
plain += node.value;
|
|
66
|
+
if ((node.type === 'text' || node.type === 'verbatim') && typeof node.value === 'string') {
|
|
67
|
+
plain += covered ? (0, schema_1.maskText)(node.value) : node.value;
|
|
69
68
|
return;
|
|
70
69
|
}
|
|
71
70
|
if (node.type === 'fcode' && typeof node.name === 'string') {
|
|
72
71
|
const tagName = fcodeToTag[node.name] || 'span';
|
|
73
72
|
const start = plain.length;
|
|
74
|
-
walk(node.content);
|
|
73
|
+
walk(node.content, covered);
|
|
75
74
|
const end = plain.length;
|
|
76
75
|
if (end > start) {
|
|
77
76
|
decorations.push({
|
|
@@ -84,10 +83,10 @@ const extractPlainAndDecorations = (nodes) => {
|
|
|
84
83
|
return;
|
|
85
84
|
}
|
|
86
85
|
if (node.content !== undefined) {
|
|
87
|
-
walk(node.content);
|
|
86
|
+
walk(node.content, covered);
|
|
88
87
|
}
|
|
89
88
|
else if (typeof node.text === 'string') {
|
|
90
|
-
plain += node.text;
|
|
89
|
+
plain += covered ? (0, schema_1.maskText)(node.text) : node.text;
|
|
91
90
|
}
|
|
92
91
|
};
|
|
93
92
|
walk(nodes);
|
|
@@ -98,25 +97,30 @@ exports.extractPlainAndDecorations = extractPlainAndDecorations;
|
|
|
98
97
|
// a caller that says nothing gets the caption rather than silently losing it.
|
|
99
98
|
const HighlightedCode = react_1.default.memo(({ node, children, keyProp, ctx, id, wrap = 'block' }) => {
|
|
100
99
|
const conf = (0, schema_1.makeAttrs)(node, ctx);
|
|
101
|
-
const
|
|
100
|
+
const written = conf.exists('caption') ? String(conf.getFirstValue('caption')) : null;
|
|
101
|
+
const caption = written !== null && (0, schema_1.isCovered)(node, ctx) ? (0, schema_1.maskText)(written) : written;
|
|
102
102
|
const lang = conf.getFirstValue('lang');
|
|
103
|
-
const
|
|
103
|
+
const { plain, decorations } = (0, exports.extractPlainAndDecorations)(node.content, part => (0, schema_1.isCovered)(part, { maskMode: ctx?.maskMode, renderMode: ctx?.renderMode }));
|
|
104
|
+
const isDark = typeof document !== 'undefined' && !!document.body && document.body.className.toLowerCase().includes('dark');
|
|
105
|
+
// What is asked of the highlighter, with hidden text already masked. A result is
|
|
106
|
+
// shown only for the request it answers: one made for another mode or theme never
|
|
107
|
+
// reaches the page, and the same text drawn again keeps its highlight.
|
|
108
|
+
const request = JSON.stringify([lang, isDark, plain, decorations]);
|
|
109
|
+
const [result, setResult] = (0, react_1.useState)(null);
|
|
104
110
|
(0, react_1.useEffect)(() => {
|
|
105
111
|
if (!lang)
|
|
106
112
|
return;
|
|
107
113
|
let cancelled = false;
|
|
108
114
|
const highlight = async () => {
|
|
109
115
|
try {
|
|
110
|
-
const
|
|
111
|
-
const { plain, decorations } = (0, exports.extractPlainAndDecorations)(node.content);
|
|
112
|
-
const result = await (0, shiki_1.codeToThemedHtml)({
|
|
116
|
+
const html = await (0, shiki_1.codeToThemedHtml)({
|
|
113
117
|
code: plain,
|
|
114
118
|
language: lang,
|
|
115
119
|
theme: isDark ? 'dark' : 'light',
|
|
116
120
|
decorations,
|
|
117
121
|
});
|
|
118
122
|
if (!cancelled)
|
|
119
|
-
|
|
123
|
+
setResult({ request, html });
|
|
120
124
|
}
|
|
121
125
|
catch (e) {
|
|
122
126
|
console.error('[podlite] shiki highlight error:', e);
|
|
@@ -126,7 +130,8 @@ const HighlightedCode = react_1.default.memo(({ node, children, keyProp, ctx, id
|
|
|
126
130
|
return () => {
|
|
127
131
|
cancelled = true;
|
|
128
132
|
};
|
|
129
|
-
}, [
|
|
133
|
+
}, [request]);
|
|
134
|
+
const html = result && result.request === request ? result.html : null;
|
|
130
135
|
// The id goes on whichever element the branch puts outermost, so a link to
|
|
131
136
|
// the block keeps working once highlighting replaces the plain output.
|
|
132
137
|
const codeBody = html ? (react_1.default.createElement("code", { key: keyProp, id: id, className: "shiki", dangerouslySetInnerHTML: { __html: html } })) : (react_1.default.createElement("pre", { key: `${keyProp}-pre`, id: id },
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"HighlightedCode.js","sourceRoot":"","sources":["../src/HighlightedCode.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+CAAkD;AAClD,
|
|
1
|
+
{"version":3,"file":"HighlightedCode.js","sourceRoot":"","sources":["../src/HighlightedCode.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+CAAkD;AAClD,4CAAgE;AAChE,mCAA0C;AAS1C,MAAM,UAAU,GAA2B;IACzC,CAAC,EAAE,QAAQ;IACX,CAAC,EAAE,IAAI;IACP,CAAC,EAAE,MAAM;IACT,CAAC,EAAE,GAAG;IACN,CAAC,EAAE,KAAK;CACT,CAAA;AAED,sFAAsF;AACtF,wCAAwC;AACjC,MAAM,0BAA0B,GAAG,CACxC,KAAc,EACd,QAAoC,GAAG,EAAE,CAAC,KAAK,EACD,EAAE;IAChD,MAAM,WAAW,GAAiB,EAAE,CAAA;IACpC,IAAI,KAAK,GAAG,EAAE,CAAA;IAEd,MAAM,IAAI,GAAG,CAAC,KAAc,EAAE,OAAO,GAAG,KAAK,EAAQ,EAAE;QACrD,IAAI,KAAK,IAAI,IAAI;YAAE,OAAM;QACzB,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;YAC9B,KAAK,IAAI,OAAO,CAAC,CAAC,CAAC,IAAA,iBAAQ,EAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAA;YAC1C,OAAM;QACR,CAAC;QACD,IAAI,KAA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,IAAI,MAAM,CAAC,OAAO,KAAK,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAA;IAEtE,2EAA2E;IAC3E,uEAAuE;IACvE,MAAM,QAAQ,GAAG,IAAI,CAAC,CAAC,CAAC,CACtB,wCAAM,GAAG,EAAE,OAAO,EAAE,EAAE,EAAE,EAAE,EAAE,SAAS,EAAC,OAAO,EAAC,uBAAuB,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,GAAI,CAC5F,CAAC,CAAC,CAAC,CACF,uCAAK,GAAG,EAAE,GAAG,OAAO,MAAM,EAAE,EAAE,EAAE,EAAE;QAChC,wCAAM,GAAG,EAAE,OAAO,IAAG,QAAQ,CAAQ,CACjC,CACP,CAAA;IAED,IAAI,IAAI,KAAK,OAAO,EAAE,CAAC;QACrB,OAAO,CACL,uCAAK,SAAS,EAAC,YAAY,EAAC,GAAG,EAAE,GAAG,OAAO,WAAW;YACnD,QAAQ;YACR,OAAO,CAAC,CAAC,CAAC,CACT,uCAAK,GAAG,EAAE,GAAG,OAAO,UAAU,EAAE,SAAS,EAAC,SAAS,IAChD,OAAO,CACJ,CACP,CAAC,CAAC,CAAC,IAAI,CACJ,CACP,CAAA;IACH,CAAC;IAED,OAAO,QAAQ,CAAA;AACjB,CAAC,CACF,CAAA;AAED,eAAe,CAAC,WAAW,GAAG,iBAAiB,CAAA;AAE/C,kBAAe,eAAe,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@podlite/highlight",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"description": "Syntax highlighting for Podlite code blocks, shared by the renderer and the editor",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"test": "yarn g:jest --passWithNoTests"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@podlite/schema": "0.0.
|
|
26
|
+
"@podlite/schema": "0.0.72"
|
|
27
27
|
},
|
|
28
28
|
"exports": {
|
|
29
29
|
".": {
|