@dr-ishaan/rehype-perfect-code-blocks 1.2.2 → 1.3.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 +98 -0
- package/LICENSE +0 -0
- package/README.md +225 -13
- package/dist/astro.d.ts +0 -0
- package/dist/astro.d.ts.map +1 -1
- package/dist/astro.js +9 -2
- package/dist/astro.js.map +1 -1
- package/dist/color-utils.d.ts +77 -0
- package/dist/color-utils.d.ts.map +1 -0
- package/dist/color-utils.js +189 -0
- package/dist/color-utils.js.map +1 -0
- package/dist/copy-script.d.ts +10 -3
- package/dist/copy-script.d.ts.map +1 -1
- package/dist/copy-script.js +108 -16
- package/dist/copy-script.js.map +1 -1
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/meta.d.ts +0 -0
- package/dist/meta.d.ts.map +0 -0
- package/dist/meta.js +0 -0
- package/dist/meta.js.map +0 -0
- package/dist/remark.d.ts +0 -0
- package/dist/remark.d.ts.map +0 -0
- package/dist/remark.js +0 -0
- package/dist/remark.js.map +0 -0
- package/dist/shiki.d.ts +20 -0
- package/dist/shiki.d.ts.map +1 -1
- package/dist/shiki.js +116 -4
- package/dist/shiki.js.map +1 -1
- package/dist/styles.css +0 -0
- package/dist/transformer.d.ts +0 -0
- package/dist/transformer.d.ts.map +1 -1
- package/dist/transformer.js +108 -1
- package/dist/transformer.js.map +1 -1
- package/dist/types.d.ts +12 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +0 -0
- package/dist/types.js.map +0 -0
- package/dist/word-diff.d.ts +47 -0
- package/dist/word-diff.d.ts.map +1 -0
- package/dist/word-diff.js +138 -0
- package/dist/word-diff.js.map +1 -0
- package/package.json +2 -2
- package/src/astro.ts +9 -2
- package/src/color-utils.ts +214 -0
- package/src/copy-script.ts +108 -16
- package/src/index.ts +7 -1
- package/src/meta.ts +0 -0
- package/src/remark.ts +0 -0
- package/src/shiki.ts +157 -10
- package/src/styles.css +0 -0
- package/src/transformer.ts +109 -1
- package/src/types.ts +12 -0
- package/src/vite-raw.d.ts +0 -0
- package/src/word-diff.ts +143 -0
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Color manipulation utilities for theme-aware CSS variable defaults.
|
|
3
|
+
*
|
|
4
|
+
* Pattern 2 (adopted from expressive-code's `helpers/color-transforms.ts`):
|
|
5
|
+
* CSS variable defaults are derived from the loaded Shiki theme and adjusted
|
|
6
|
+
* to meet WCAG contrast ratios, so code blocks look good with ANY Shiki theme
|
|
7
|
+
* out of the box — line numbers, diff backgrounds, and focus highlights are
|
|
8
|
+
* automatically legible against the theme's background color.
|
|
9
|
+
*
|
|
10
|
+
* The functions here are intentionally minimal — we only implement what we
|
|
11
|
+
* need to compute a few default `--pcb-*` values. For full color manipulation
|
|
12
|
+
* (lighten/darken/mix), see expressive-code's implementation.
|
|
13
|
+
*/
|
|
14
|
+
/** Relative luminance per WCAG 2.1: https://www.w3.org/TR/WCAG21/#dfn-relative-luminance */
|
|
15
|
+
function relativeLuminance({ r, g, b }) {
|
|
16
|
+
const channel = (c) => {
|
|
17
|
+
const s = c / 255;
|
|
18
|
+
return s <= 0.03928 ? s / 12.92 : Math.pow((s + 0.055) / 1.055, 2.4);
|
|
19
|
+
};
|
|
20
|
+
return 0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b);
|
|
21
|
+
}
|
|
22
|
+
/** Contrast ratio per WCAG 2.1: https://www.w3.org/TR/WCAG21/#dfn-contrast-ratio (1–21). */
|
|
23
|
+
export function contrastRatio(fg, bg) {
|
|
24
|
+
const l1 = relativeLuminance(fg);
|
|
25
|
+
const l2 = relativeLuminance(bg);
|
|
26
|
+
const lighter = Math.max(l1, l2);
|
|
27
|
+
const darker = Math.min(l1, l2);
|
|
28
|
+
return (lighter + 0.05) / (darker + 0.05);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Parse a hex color (#RGB, #RGBA, #RRGGBB, #RRGGBBAA) or rgb()/rgba() string
|
|
32
|
+
* into an RGB object. Returns null if the input can't be parsed.
|
|
33
|
+
*/
|
|
34
|
+
export function parseColor(input) {
|
|
35
|
+
if (!input)
|
|
36
|
+
return null;
|
|
37
|
+
const s = input.trim().toLowerCase();
|
|
38
|
+
// Hex: #RGB, #RGBA, #RRGGBB, #RRGGBBAA
|
|
39
|
+
const hexMatch = s.match(/^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/);
|
|
40
|
+
if (hexMatch) {
|
|
41
|
+
let hex = hexMatch[1];
|
|
42
|
+
if (hex.length === 3 || hex.length === 4) {
|
|
43
|
+
hex = hex
|
|
44
|
+
.split('')
|
|
45
|
+
.map((c) => c + c)
|
|
46
|
+
.join('');
|
|
47
|
+
}
|
|
48
|
+
const r = parseInt(hex.slice(0, 2), 16);
|
|
49
|
+
const g = parseInt(hex.slice(2, 4), 16);
|
|
50
|
+
const b = parseInt(hex.slice(4, 6), 16);
|
|
51
|
+
const a = hex.length === 8 ? parseInt(hex.slice(6, 8), 16) / 255 : undefined;
|
|
52
|
+
return { r, g, b, a };
|
|
53
|
+
}
|
|
54
|
+
// rgb() / rgba()
|
|
55
|
+
const rgbMatch = s.match(/^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*([\d.]+)\s*)?\)$/);
|
|
56
|
+
if (rgbMatch) {
|
|
57
|
+
return {
|
|
58
|
+
r: parseInt(rgbMatch[1], 10),
|
|
59
|
+
g: parseInt(rgbMatch[2], 10),
|
|
60
|
+
b: parseInt(rgbMatch[3], 10),
|
|
61
|
+
a: rgbMatch[4] !== undefined ? parseFloat(rgbMatch[4]) : undefined,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
/** Convert an RGB color back to a hex string (#RRGGBB or #RRGGBBAA). */
|
|
67
|
+
export function toHex({ r, g, b, a }) {
|
|
68
|
+
const toHex2 = (n) => Math.max(0, Math.min(255, Math.round(n))).toString(16).padStart(2, '0');
|
|
69
|
+
const base = `#${toHex2(r)}${toHex2(g)}${toHex2(b)}`;
|
|
70
|
+
if (a !== undefined && a < 1)
|
|
71
|
+
return base + toHex2(a * 255);
|
|
72
|
+
return base;
|
|
73
|
+
}
|
|
74
|
+
/** Mix two colors by a ratio (0 = pure a, 1 = pure b). */
|
|
75
|
+
export function mix(a, b, ratio) {
|
|
76
|
+
const t = Math.max(0, Math.min(1, ratio));
|
|
77
|
+
return {
|
|
78
|
+
r: a.r * (1 - t) + b.r * t,
|
|
79
|
+
g: a.g * (1 - t) + b.g * t,
|
|
80
|
+
b: a.b * (1 - t) + b.b * t,
|
|
81
|
+
a: a.a !== undefined || b.a !== undefined ? (a.a ?? 1) * (1 - t) + (b.a ?? 1) * t : undefined,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/** Lighten a color toward white by a ratio (0–1). */
|
|
85
|
+
export function lighten(color, ratio) {
|
|
86
|
+
return mix(color, { r: 255, g: 255, b: 255 }, ratio);
|
|
87
|
+
}
|
|
88
|
+
/** Darken a color toward black by a ratio (0–1). */
|
|
89
|
+
export function darken(color, ratio) {
|
|
90
|
+
return mix(color, { r: 0, g: 0, b: 0 }, ratio);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Adjust a foreground color to meet a target contrast ratio against a
|
|
94
|
+
* background. If the contrast is already sufficient, returns the foreground
|
|
95
|
+
* unchanged. Otherwise, lightens (if bg is dark) or darkens (if bg is light)
|
|
96
|
+
* the foreground until the target ratio is met.
|
|
97
|
+
*
|
|
98
|
+
* @param fg Foreground color to adjust
|
|
99
|
+
* @param bg Background color to adjust against
|
|
100
|
+
* @param minRatio Minimum WCAG contrast ratio (default 4.5 = AA for normal text)
|
|
101
|
+
* @param maxRatio Maximum ratio to aim for if adjusting (default 7.0 = AAA)
|
|
102
|
+
* @returns The adjusted foreground color (or the original if already sufficient)
|
|
103
|
+
*/
|
|
104
|
+
export function ensureColorContrastOnBackground(fg, bg, minRatio = 4.5, maxRatio = 7.0) {
|
|
105
|
+
const current = contrastRatio(fg, bg);
|
|
106
|
+
if (current >= minRatio)
|
|
107
|
+
return fg;
|
|
108
|
+
// Decide direction: if bg is dark (luminance < 0.5), lighten fg; else darken.
|
|
109
|
+
const bgLum = relativeLuminance(bg);
|
|
110
|
+
const direction = bgLum < 0.5 ? 'lighten' : 'darken';
|
|
111
|
+
// Binary search between current and pure white/black for the target ratio.
|
|
112
|
+
let lo = 0;
|
|
113
|
+
let hi = 1;
|
|
114
|
+
let best = fg;
|
|
115
|
+
for (let i = 0; i < 16; i++) {
|
|
116
|
+
const mid = (lo + hi) / 2;
|
|
117
|
+
const candidate = direction === 'lighten' ? lighten(fg, mid) : darken(fg, mid);
|
|
118
|
+
const ratio = contrastRatio(candidate, bg);
|
|
119
|
+
if (ratio >= minRatio && ratio <= maxRatio) {
|
|
120
|
+
return candidate;
|
|
121
|
+
}
|
|
122
|
+
if (ratio < minRatio) {
|
|
123
|
+
lo = mid;
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
best = candidate;
|
|
127
|
+
hi = mid;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return best;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Extract the background and foreground colors from a Shiki theme object.
|
|
134
|
+
* Shiki themes have a `bg` and `fg` property at the top level (hex strings).
|
|
135
|
+
* Returns nulls if the theme shape doesn't match.
|
|
136
|
+
*/
|
|
137
|
+
export function extractThemeColors(theme) {
|
|
138
|
+
const t = theme;
|
|
139
|
+
return {
|
|
140
|
+
bg: parseColor(t?.bg),
|
|
141
|
+
fg: parseColor(t?.fg),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Compute theme-aware `--pcb-*` defaults for a code block based on the
|
|
146
|
+
* loaded Shiki theme. These are applied as inline styles on the `<figure>`
|
|
147
|
+
* element, so the static `dist/styles.css` can ship its own defaults while
|
|
148
|
+
* the runtime overrides them with theme-aware values.
|
|
149
|
+
*
|
|
150
|
+
* Currently computes:
|
|
151
|
+
* - --pcb-bg: theme background (or 'inherit' if unknown)
|
|
152
|
+
* - --pcb-fg: theme foreground (or 'inherit' if unknown)
|
|
153
|
+
* - --pcb-line-numbers-fg: theme fg, contrast-adjusted against theme bg
|
|
154
|
+
* - --pcb-line-highlight-bg: theme fg at ~12% alpha (subtle highlight)
|
|
155
|
+
* - --pcb-line-diff-add-bg: green at ~18% alpha
|
|
156
|
+
* - --pcb-line-diff-del-bg: red at ~18% alpha
|
|
157
|
+
* - --pcb-line-focus-bg: theme fg at ~6% alpha (subtle dim)
|
|
158
|
+
*
|
|
159
|
+
* @param theme The Shiki theme object (must have `bg` and `fg` hex strings)
|
|
160
|
+
* @returns A CSS style string (e.g. `--pcb-bg:#fff;--pcb-fg:#000;...`) or empty string
|
|
161
|
+
*/
|
|
162
|
+
export function computeThemeAwareDefaults(theme) {
|
|
163
|
+
const { bg, fg } = extractThemeColors(theme);
|
|
164
|
+
if (!bg || !fg)
|
|
165
|
+
return '';
|
|
166
|
+
const parts = [];
|
|
167
|
+
parts.push(`--pcb-bg:${toHex(bg)}`);
|
|
168
|
+
parts.push(`--pcb-fg:${toHex(fg)}`);
|
|
169
|
+
// Line numbers: use fg, but adjust contrast to >= 3.0 (WCAG AA for large text)
|
|
170
|
+
// against the background. Shiki themes often have low-contrast line-number
|
|
171
|
+
// colors baked in; we override them with a guaranteed-legible value.
|
|
172
|
+
const lineNumFg = ensureColorContrastOnBackground(fg, bg, 3.0, 4.5);
|
|
173
|
+
parts.push(`--pcb-ln-fg:${toHex(lineNumFg)}`);
|
|
174
|
+
// Line highlight background: subtle tint of the foreground at ~12% alpha.
|
|
175
|
+
// Use mix() with the background to get a slightly-lighter/darker shade.
|
|
176
|
+
const hlBg = mix(bg, fg, 0.12);
|
|
177
|
+
parts.push(`--pcb-line-highlight-bg:${toHex(hlBg)}`);
|
|
178
|
+
// Diff add: green (#22863a in github) at 18% alpha over bg.
|
|
179
|
+
const diffAdd = mix(bg, { r: 34, g: 134, b: 58 }, 0.18);
|
|
180
|
+
parts.push(`--pcb-line-add-bg:${toHex(diffAdd)}`);
|
|
181
|
+
// Diff del: red (#cb2431 in github) at 18% alpha over bg.
|
|
182
|
+
const diffDel = mix(bg, { r: 203, g: 36, b: 49 }, 0.18);
|
|
183
|
+
parts.push(`--pcb-line-del-bg:${toHex(diffDel)}`);
|
|
184
|
+
// Focus background: dim the non-focused lines by mixing bg with fg at low alpha.
|
|
185
|
+
const focusBg = mix(bg, fg, 0.04);
|
|
186
|
+
parts.push(`--pcb-line-focus-bg:${toHex(focusBg)}`);
|
|
187
|
+
return parts.join(';');
|
|
188
|
+
}
|
|
189
|
+
//# sourceMappingURL=color-utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"color-utils.js","sourceRoot":"","sources":["../src/color-utils.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAUH,4FAA4F;AAC5F,SAAS,iBAAiB,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAO;IACzC,MAAM,OAAO,GAAG,CAAC,CAAS,EAAU,EAAE;QACpC,MAAM,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC;QAClB,OAAO,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,GAAG,KAAK,EAAE,GAAG,CAAC,CAAC;IACvE,CAAC,CAAC;IACF,OAAO,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,GAAG,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,GAAG,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AACzE,CAAC;AAED,4FAA4F;AAC5F,MAAM,UAAU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EAA0E;IAC1E,wEAAwE;IACxE,MAAM,IAAI,GAAG,GAAG,CAAC,EAAE,EAAE,EAAE,EAAE,IAAI,CAAC,CAAC;IAC/B,KAAK,CAAC,IAAI,CAAC,2BAA2B,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAErD,4DAA4D;IAC5D,MAAM,OAAO,GAAG,GAAG,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,IAAI,CAAC,CAAC;IACxD,KAAK,CAAC,IAAI,CAAC,qBAAqB,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;IAElD,0DAA0D;IAC1D,MAAM,OAAO,GAAG,GAAG,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,IAAI,CAAC,CAAC;IACxD,KAAK,CAAC,IAAI,CAAC,qBAAqB,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;IAElD,iFAAiF;IACjF,MAAM,OAAO,GAAG,GAAG,CAAC,EAAE,EAAE,EAAE,EAAE,IAAI,CAAC,CAAC;IAClC,KAAK,CAAC,IAAI,CAAC,uBAAuB,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;IAEpD,OAAO,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACzB,CAAC"}
|
package/dist/copy-script.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The copy-button client script. Inlined once per page (deduped by the
|
|
3
|
-
* Astro integration via `injectScript`). ~
|
|
3
|
+
* Astro integration via `injectScript`). ~1.2 KB gzipped.
|
|
4
4
|
*
|
|
5
5
|
* Behavior:
|
|
6
|
-
* - Listens for clicks on `.pcb__copy`
|
|
6
|
+
* - Listens for clicks on `.pcb__copy` (event delegation on document)
|
|
7
7
|
* - Copies the textContent of the nearest `pre code`
|
|
8
8
|
* - Toggles `.pcb__copy--done` and swaps the icon + label
|
|
9
9
|
* - Resets after `data-feedback-duration` ms (default 1600)
|
|
@@ -13,6 +13,13 @@
|
|
|
13
13
|
* - Strips leading `#` comment lines when `data-strip-comments` is set (terminal preset)
|
|
14
14
|
* - Announces "Copied" to screen readers via an aria-live region (WCAG 4.1.2)
|
|
15
15
|
* - Hides copy button when JS is disabled (via .no-js class on <html>)
|
|
16
|
+
*
|
|
17
|
+
* Pattern 4 (adopted from VitePress + expressive-code):
|
|
18
|
+
* - Event delegation via `document.addEventListener('click', ...)` — works
|
|
19
|
+
* regardless of how buttons were rendered (SSR, CSR, view transitions).
|
|
20
|
+
* - MutationObserver re-initializes the aria-live region and the .no-js → .js
|
|
21
|
+
* class swap when new code blocks are added to the DOM (SPA support).
|
|
22
|
+
* - `astro:page-load` event listener for Astro view transitions.
|
|
16
23
|
*/
|
|
17
|
-
export declare const COPY_SCRIPT = "\n(function () {\n if (window.__pcbCopyReady) return;\n window.__pcbCopyReady = true;\n\n // Remove the .no-js class so the copy buttons become visible (graceful degradation).\n if (document.documentElement.classList.contains('no-js')) {\n
|
|
24
|
+
export declare const COPY_SCRIPT = "\n(function () {\n if (window.__pcbCopyReady) return;\n window.__pcbCopyReady = true;\n\n // Remove the .no-js class so the copy buttons become visible (graceful degradation).\n function swapNoJs() {\n if (document.documentElement.classList.contains('no-js')) {\n document.documentElement.classList.remove('no-js');\n document.documentElement.classList.add('js');\n }\n }\n swapNoJs();\n\n // Reuse a single aria-live region for all copy announcements.\n var liveRegion = null;\n function ensureLiveRegion() {\n if (liveRegion && document.body.contains(liveRegion)) return liveRegion;\n liveRegion = document.querySelector('.pcb__sr-live');\n if (!liveRegion) {\n liveRegion = document.createElement('span');\n liveRegion.className = 'pcb__sr-live';\n liveRegion.setAttribute('aria-live', 'polite');\n liveRegion.setAttribute('aria-atomic', 'true');\n liveRegion.setAttribute('role', 'status');\n document.body.appendChild(liveRegion);\n }\n return liveRegion;\n }\n ensureLiveRegion();\n\n function announce(msg) {\n var lr = ensureLiveRegion();\n if (!lr) return;\n lr.textContent = '';\n // Force re-announcement by clearing then setting on next tick.\n setTimeout(function () { lr.textContent = msg; }, 50);\n }\n\n function findLabel(btn) {\n return btn.querySelector('.pcb__copy-label');\n }\n\n function findIcon(btn) {\n return btn.querySelector('svg');\n }\n\n // Strip leading comment lines (e.g. shell prompts like \"# comment\") from\n // the text before copying. Used for terminal-preset blocks where the\n // displayed code may include comments the user doesn't want on the clipboard.\n function stripComments(text) {\n // Strip lines that start with optional whitespace followed by # (shell),\n // // (C-style), or REM (Windows batch). Keep everything else.\n return text.replace(/^[ \\t]*(?:#|\\/\\/|REM\\b).*$/gm, '').replace(/\\n{3,}/g, '\\n\\n').trim();\n }\n\n // Event-delegated click handler. Works for buttons added after initial\n // render (e.g. via React/Vue re-render or Astro view transitions) because\n // the listener is on document, not on each button.\n document.addEventListener('click', function (e) {\n var btn = e.target && e.target.closest && e.target.closest('.pcb__copy');\n if (!btn) return;\n var figure = btn.closest('.pcb');\n var code = figure && figure.querySelector('pre code');\n if (!code) return;\n\n var done = btn.getAttribute('data-done-label') || 'copied!';\n var duration = parseInt(btn.getAttribute('data-feedback-duration') || '1600', 10);\n var successIconHtml = btn.getAttribute('data-success-icon');\n var stripCommentsFlag = btn.hasAttribute('data-strip-comments');\n\n var label = findLabel(btn);\n var icon = findIcon(btn);\n var originalLabel = label ? label.textContent : null;\n var originalIconHtml = icon ? icon.outerHTML : null;\n\n var rawText = code.innerText;\n var textToCopy = stripCommentsFlag ? stripComments(rawText) : rawText;\n\n var finish = function () {\n btn.classList.add('pcb__copy--done');\n if (label) label.textContent = done;\n if (successIconHtml && icon) {\n var tmp = document.createElement('span');\n tmp.innerHTML = successIconHtml;\n var newIcon = tmp.firstChild;\n if (newIcon) {\n icon.replaceWith(newIcon);\n icon = newIcon;\n }\n }\n announce(done);\n setTimeout(function () {\n btn.classList.remove('pcb__copy--done');\n if (label && originalLabel != null) label.textContent = originalLabel;\n if (originalIconHtml && icon) {\n var tmp2 = document.createElement('span');\n tmp2.innerHTML = originalIconHtml;\n var oldIcon = icon;\n icon = tmp2.firstChild;\n if (icon) oldIcon.replaceWith(icon);\n }\n }, duration);\n };\n\n if (navigator.clipboard && navigator.clipboard.writeText) {\n navigator.clipboard.writeText(textToCopy).then(finish).catch(fallback);\n } else {\n fallback();\n }\n\n function fallback() {\n var ta = document.createElement('textarea');\n ta.value = textToCopy;\n ta.style.position = 'fixed';\n ta.style.opacity = '0';\n document.body.appendChild(ta);\n ta.select();\n try { document.execCommand('copy'); finish(); } catch (_) {}\n document.body.removeChild(ta);\n }\n });\n\n // Pattern 4: MutationObserver for SPA support + .no-js race fix.\n // Watches for:\n // - New code blocks added to the DOM (React/Vue re-render, Astro view\n // transitions, Turbolinks) \u2192 re-apply .no-js \u2192 .js swap + ensure\n // aria-live region.\n // - Attribute changes on <html> (specifically the class attribute) \u2192\n // catches the case where a later script adds .no-js AFTER this script\n // ran (a race condition that previously left copy buttons hidden).\n if (typeof MutationObserver !== 'undefined') {\n var pendingObserve = false;\n var observer = new MutationObserver(function (mutations) {\n // Batch checks with microtask to avoid layout thrash.\n if (pendingObserve) return;\n pendingObserve = true;\n Promise.resolve().then(function () {\n pendingObserve = false;\n var needsSwap = false;\n for (var i = 0; i < mutations.length; i++) {\n var m = mutations[i];\n // childList: new nodes added (SPA navigation, view transitions)\n if (m.type === 'childList' && m.addedNodes && m.addedNodes.length) {\n needsSwap = true;\n }\n // attributes: class changed on <html> (the .no-js race fix)\n if (m.type === 'attributes' && m.attributeName === 'class') {\n needsSwap = true;\n }\n }\n if (needsSwap) {\n swapNoJs();\n ensureLiveRegion();\n }\n });\n });\n // Observe documentElement for BOTH childList (new nodes) AND attribute\n // changes (class attribute \u2014 catches .no-js being added by a later script).\n observer.observe(document.documentElement, {\n childList: true,\n subtree: true,\n attributes: true,\n attributeFilter: ['class'],\n });\n }\n\n // Defensive: re-run swapNoJs() on DOMContentLoaded and window.load.\n // Belt + suspenders for the .no-js race \u2014 if a framework (Astro, Next.js,\n // etc.) adds .no-js in a way the MutationObserver doesn't catch (e.g.,\n // before the observer is set up, or in a different document context),\n // these event handlers will catch it.\n if (typeof document.addEventListener === 'function') {\n document.addEventListener('DOMContentLoaded', function () {\n swapNoJs();\n ensureLiveRegion();\n });\n }\n if (typeof window.addEventListener === 'function') {\n window.addEventListener('load', function () {\n swapNoJs();\n ensureLiveRegion();\n });\n }\n\n // Pattern 4: astro:page-load event listener for Astro view transitions.\n // Astro emits this event after a view transition completes; the new page's\n // DOM may have replaced the old, so re-apply the .no-js \u2192 .js swap.\n if (typeof document.addEventListener === 'function') {\n document.addEventListener('astro:page-load', function () {\n swapNoJs();\n ensureLiveRegion();\n });\n }\n})();\n";
|
|
18
25
|
//# sourceMappingURL=copy-script.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"copy-script.d.ts","sourceRoot":"","sources":["../src/copy-script.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"copy-script.d.ts","sourceRoot":"","sources":["../src/copy-script.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,eAAO,MAAM,WAAW,kxOAmMvB,CAAC"}
|
package/dist/copy-script.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The copy-button client script. Inlined once per page (deduped by the
|
|
3
|
-
* Astro integration via `injectScript`). ~
|
|
3
|
+
* Astro integration via `injectScript`). ~1.2 KB gzipped.
|
|
4
4
|
*
|
|
5
5
|
* Behavior:
|
|
6
|
-
* - Listens for clicks on `.pcb__copy`
|
|
6
|
+
* - Listens for clicks on `.pcb__copy` (event delegation on document)
|
|
7
7
|
* - Copies the textContent of the nearest `pre code`
|
|
8
8
|
* - Toggles `.pcb__copy--done` and swaps the icon + label
|
|
9
9
|
* - Resets after `data-feedback-duration` ms (default 1600)
|
|
@@ -13,6 +13,13 @@
|
|
|
13
13
|
* - Strips leading `#` comment lines when `data-strip-comments` is set (terminal preset)
|
|
14
14
|
* - Announces "Copied" to screen readers via an aria-live region (WCAG 4.1.2)
|
|
15
15
|
* - Hides copy button when JS is disabled (via .no-js class on <html>)
|
|
16
|
+
*
|
|
17
|
+
* Pattern 4 (adopted from VitePress + expressive-code):
|
|
18
|
+
* - Event delegation via `document.addEventListener('click', ...)` — works
|
|
19
|
+
* regardless of how buttons were rendered (SSR, CSR, view transitions).
|
|
20
|
+
* - MutationObserver re-initializes the aria-live region and the .no-js → .js
|
|
21
|
+
* class swap when new code blocks are added to the DOM (SPA support).
|
|
22
|
+
* - `astro:page-load` event listener for Astro view transitions.
|
|
16
23
|
*/
|
|
17
24
|
export const COPY_SCRIPT = `
|
|
18
25
|
(function () {
|
|
@@ -20,27 +27,37 @@ export const COPY_SCRIPT = `
|
|
|
20
27
|
window.__pcbCopyReady = true;
|
|
21
28
|
|
|
22
29
|
// Remove the .no-js class so the copy buttons become visible (graceful degradation).
|
|
23
|
-
|
|
24
|
-
document.documentElement.classList.
|
|
25
|
-
|
|
30
|
+
function swapNoJs() {
|
|
31
|
+
if (document.documentElement.classList.contains('no-js')) {
|
|
32
|
+
document.documentElement.classList.remove('no-js');
|
|
33
|
+
document.documentElement.classList.add('js');
|
|
34
|
+
}
|
|
26
35
|
}
|
|
36
|
+
swapNoJs();
|
|
27
37
|
|
|
28
38
|
// Reuse a single aria-live region for all copy announcements.
|
|
29
|
-
var liveRegion =
|
|
30
|
-
|
|
31
|
-
liveRegion
|
|
32
|
-
liveRegion
|
|
33
|
-
liveRegion
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
39
|
+
var liveRegion = null;
|
|
40
|
+
function ensureLiveRegion() {
|
|
41
|
+
if (liveRegion && document.body.contains(liveRegion)) return liveRegion;
|
|
42
|
+
liveRegion = document.querySelector('.pcb__sr-live');
|
|
43
|
+
if (!liveRegion) {
|
|
44
|
+
liveRegion = document.createElement('span');
|
|
45
|
+
liveRegion.className = 'pcb__sr-live';
|
|
46
|
+
liveRegion.setAttribute('aria-live', 'polite');
|
|
47
|
+
liveRegion.setAttribute('aria-atomic', 'true');
|
|
48
|
+
liveRegion.setAttribute('role', 'status');
|
|
49
|
+
document.body.appendChild(liveRegion);
|
|
50
|
+
}
|
|
51
|
+
return liveRegion;
|
|
37
52
|
}
|
|
53
|
+
ensureLiveRegion();
|
|
38
54
|
|
|
39
55
|
function announce(msg) {
|
|
40
|
-
|
|
41
|
-
|
|
56
|
+
var lr = ensureLiveRegion();
|
|
57
|
+
if (!lr) return;
|
|
58
|
+
lr.textContent = '';
|
|
42
59
|
// Force re-announcement by clearing then setting on next tick.
|
|
43
|
-
setTimeout(function () {
|
|
60
|
+
setTimeout(function () { lr.textContent = msg; }, 50);
|
|
44
61
|
}
|
|
45
62
|
|
|
46
63
|
function findLabel(btn) {
|
|
@@ -60,6 +77,9 @@ export const COPY_SCRIPT = `
|
|
|
60
77
|
return text.replace(/^[ \\t]*(?:#|\\/\\/|REM\\b).*$/gm, '').replace(/\\n{3,}/g, '\\n\\n').trim();
|
|
61
78
|
}
|
|
62
79
|
|
|
80
|
+
// Event-delegated click handler. Works for buttons added after initial
|
|
81
|
+
// render (e.g. via React/Vue re-render or Astro view transitions) because
|
|
82
|
+
// the listener is on document, not on each button.
|
|
63
83
|
document.addEventListener('click', function (e) {
|
|
64
84
|
var btn = e.target && e.target.closest && e.target.closest('.pcb__copy');
|
|
65
85
|
if (!btn) return;
|
|
@@ -123,6 +143,78 @@ export const COPY_SCRIPT = `
|
|
|
123
143
|
document.body.removeChild(ta);
|
|
124
144
|
}
|
|
125
145
|
});
|
|
146
|
+
|
|
147
|
+
// Pattern 4: MutationObserver for SPA support + .no-js race fix.
|
|
148
|
+
// Watches for:
|
|
149
|
+
// - New code blocks added to the DOM (React/Vue re-render, Astro view
|
|
150
|
+
// transitions, Turbolinks) → re-apply .no-js → .js swap + ensure
|
|
151
|
+
// aria-live region.
|
|
152
|
+
// - Attribute changes on <html> (specifically the class attribute) →
|
|
153
|
+
// catches the case where a later script adds .no-js AFTER this script
|
|
154
|
+
// ran (a race condition that previously left copy buttons hidden).
|
|
155
|
+
if (typeof MutationObserver !== 'undefined') {
|
|
156
|
+
var pendingObserve = false;
|
|
157
|
+
var observer = new MutationObserver(function (mutations) {
|
|
158
|
+
// Batch checks with microtask to avoid layout thrash.
|
|
159
|
+
if (pendingObserve) return;
|
|
160
|
+
pendingObserve = true;
|
|
161
|
+
Promise.resolve().then(function () {
|
|
162
|
+
pendingObserve = false;
|
|
163
|
+
var needsSwap = false;
|
|
164
|
+
for (var i = 0; i < mutations.length; i++) {
|
|
165
|
+
var m = mutations[i];
|
|
166
|
+
// childList: new nodes added (SPA navigation, view transitions)
|
|
167
|
+
if (m.type === 'childList' && m.addedNodes && m.addedNodes.length) {
|
|
168
|
+
needsSwap = true;
|
|
169
|
+
}
|
|
170
|
+
// attributes: class changed on <html> (the .no-js race fix)
|
|
171
|
+
if (m.type === 'attributes' && m.attributeName === 'class') {
|
|
172
|
+
needsSwap = true;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
if (needsSwap) {
|
|
176
|
+
swapNoJs();
|
|
177
|
+
ensureLiveRegion();
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
// Observe documentElement for BOTH childList (new nodes) AND attribute
|
|
182
|
+
// changes (class attribute — catches .no-js being added by a later script).
|
|
183
|
+
observer.observe(document.documentElement, {
|
|
184
|
+
childList: true,
|
|
185
|
+
subtree: true,
|
|
186
|
+
attributes: true,
|
|
187
|
+
attributeFilter: ['class'],
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Defensive: re-run swapNoJs() on DOMContentLoaded and window.load.
|
|
192
|
+
// Belt + suspenders for the .no-js race — if a framework (Astro, Next.js,
|
|
193
|
+
// etc.) adds .no-js in a way the MutationObserver doesn't catch (e.g.,
|
|
194
|
+
// before the observer is set up, or in a different document context),
|
|
195
|
+
// these event handlers will catch it.
|
|
196
|
+
if (typeof document.addEventListener === 'function') {
|
|
197
|
+
document.addEventListener('DOMContentLoaded', function () {
|
|
198
|
+
swapNoJs();
|
|
199
|
+
ensureLiveRegion();
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
if (typeof window.addEventListener === 'function') {
|
|
203
|
+
window.addEventListener('load', function () {
|
|
204
|
+
swapNoJs();
|
|
205
|
+
ensureLiveRegion();
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Pattern 4: astro:page-load event listener for Astro view transitions.
|
|
210
|
+
// Astro emits this event after a view transition completes; the new page's
|
|
211
|
+
// DOM may have replaced the old, so re-apply the .no-js → .js swap.
|
|
212
|
+
if (typeof document.addEventListener === 'function') {
|
|
213
|
+
document.addEventListener('astro:page-load', function () {
|
|
214
|
+
swapNoJs();
|
|
215
|
+
ensureLiveRegion();
|
|
216
|
+
});
|
|
217
|
+
}
|
|
126
218
|
})();
|
|
127
219
|
`;
|
|
128
220
|
//# sourceMappingURL=copy-script.js.map
|
package/dist/copy-script.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"copy-script.js","sourceRoot":"","sources":["../src/copy-script.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"copy-script.js","sourceRoot":"","sources":["../src/copy-script.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAmM1B,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -18,9 +18,15 @@
|
|
|
18
18
|
*/
|
|
19
19
|
import type { Plugin } from 'unified';
|
|
20
20
|
import type { Root } from 'hast';
|
|
21
|
+
import { disposeHighlighter, runHighlighterTask } from './shiki.js';
|
|
21
22
|
import { remarkPreserveCodeMeta } from './remark.js';
|
|
23
|
+
import { wordDiff, hasChanges } from './word-diff.js';
|
|
24
|
+
import type { DiffToken } from './word-diff.js';
|
|
22
25
|
import type { PerfectCodeOptions } from './types.js';
|
|
23
26
|
export { remarkPreserveCodeMeta };
|
|
27
|
+
export { disposeHighlighter, runHighlighterTask };
|
|
28
|
+
export { wordDiff, hasChanges };
|
|
29
|
+
export type { DiffToken };
|
|
24
30
|
export declare const rehypePerfectCodeBlocks: Plugin<[PerfectCodeOptions?], Root>;
|
|
25
31
|
export default rehypePerfectCodeBlocks;
|
|
26
32
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACtC,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACtC,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAEjC,OAAO,EAAuB,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AACzF,OAAO,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAErD,OAAO,EAAE,sBAAsB,EAAE,CAAC;AAClC,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,CAAC;AAClD,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,CAAC;AAChC,YAAY,EAAE,SAAS,EAAE,CAAC;AAE1B,eAAO,MAAM,uBAAuB,EAAE,MAAM,CAAC,CAAC,kBAAkB,CAAC,CAAC,EAAE,IAAI,CAmBrE,CAAC;AAEJ,eAAe,uBAAuB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -17,9 +17,12 @@
|
|
|
17
17
|
* plugin with style/script injection and automatic Shiki integration.
|
|
18
18
|
*/
|
|
19
19
|
import { rehypePerfectCodeBlocks as transformer } from './transformer.js';
|
|
20
|
-
import { runShikiOnRawBlocks } from './shiki.js';
|
|
20
|
+
import { runShikiOnRawBlocks, disposeHighlighter, runHighlighterTask } from './shiki.js';
|
|
21
21
|
import { remarkPreserveCodeMeta } from './remark.js';
|
|
22
|
+
import { wordDiff, hasChanges } from './word-diff.js';
|
|
22
23
|
export { remarkPreserveCodeMeta };
|
|
24
|
+
export { disposeHighlighter, runHighlighterTask };
|
|
25
|
+
export { wordDiff, hasChanges };
|
|
23
26
|
export const rehypePerfectCodeBlocks = (options = {}) => {
|
|
24
27
|
const engine = options.engine ?? 'auto';
|
|
25
28
|
const opts = options;
|
|
@@ -78,6 +81,7 @@ function resolveDefaults(opts) {
|
|
|
78
81
|
lineNumbersStart: opts.lineNumbersStart ?? 1,
|
|
79
82
|
highlight: opts.highlight ?? true,
|
|
80
83
|
diff: opts.diff ?? true,
|
|
84
|
+
wordDiff: opts.wordDiff ?? false,
|
|
81
85
|
focus: opts.focus ?? true,
|
|
82
86
|
errorLevels: opts.errorLevels ?? true,
|
|
83
87
|
wrap: opts.wrap ?? false,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAIH,OAAO,EAAE,uBAAuB,IAAI,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC1E,OAAO,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAIH,OAAO,EAAE,uBAAuB,IAAI,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC1E,OAAO,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AACzF,OAAO,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAItD,OAAO,EAAE,sBAAsB,EAAE,CAAC;AAClC,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,CAAC;AAClD,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,CAAC;AAGhC,MAAM,CAAC,MAAM,uBAAuB,GAClC,CAAC,OAAO,GAAG,EAAE,EAAE,EAAE;IACf,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,MAAM,CAAC;IACxC,MAAM,IAAI,GAAG,OAAO,CAAC;IAErB,OAAO,KAAK,EAAE,IAAI,EAAE,EAAE;QACpB,8EAA8E;QAC9E,2EAA2E;QAC3E,0EAA0E;QAC1E,0BAA0B;QAC1B,IAAI,MAAM,KAAK,OAAO,IAAI,MAAM,KAAK,MAAM,EAAE,CAAC;YAC5C,MAAM,mBAAmB,CAAC,IAAI,EAAE,eAAe,CAAC,IAAI,CAAC,CAAC,CAAC;QACzD,CAAC;QACD,2DAA2D;QAE3D,2CAA2C;QAC3C,MAAM,SAAS,GAAG,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IAAI;QACrD,aAAa,EAAE,IAAI,CAAC,aAAa,IAAI,EAAE;QACvC,gBAAgB,EAAE,IAAI,CAAC,gBAAgB,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;QACrD,WAAW,EAAE,IAAI,CAAC,WAAW,IAAI,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC;QAC3C,sBAAsB,EAAE,IAAI,CAAC,sBAAsB,IAAI,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC;QACjE,uBAAuB,EAAE,IAAI,CAAC,uBAAuB,IAAI,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC;QACnE,YAAY,EAAE,IAAI,CAAC,YAAY,IAAI,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC;QAC7C,cAAc,EAAE,IAAI,CAAC,cAAc,IAAI,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC;QACjD,KAAK,EAAE,IAAI,CAAC,KAAK,IAAI,EAAE;QACvB,MAAM,EAAE,IAAI,CAAC,MAAM,IAAI,OAAO;QAC9B,QAAQ,EAAE,IAAI,CAAC,QAAQ,IAAI,EAAE;QAC7B,MAAM,EAAE,IAAI,CAAC,MAAM,IAAI,SAAS;QAChC,YAAY,EAAE,IAAI,CAAC,YAAY,IAAI,IAAI;QACvC,KAAK,EAAE,IAAI,CAAC,KAAK,IAAI,MAAM;QAC3B,MAAM,EAAE,IAAI,CAAC,MAAM,IAAI,KAAK;KAC7B,CAAC;AACJ,CAAC"}
|
package/dist/meta.d.ts
CHANGED
|
File without changes
|
package/dist/meta.d.ts.map
CHANGED
|
File without changes
|
package/dist/meta.js
CHANGED
|
File without changes
|
package/dist/meta.js.map
CHANGED
|
File without changes
|
package/dist/remark.d.ts
CHANGED
|
File without changes
|
package/dist/remark.d.ts.map
CHANGED
|
File without changes
|
package/dist/remark.js
CHANGED
|
File without changes
|
package/dist/remark.js.map
CHANGED
|
File without changes
|
package/dist/shiki.d.ts
CHANGED
|
@@ -14,6 +14,26 @@
|
|
|
14
14
|
*/
|
|
15
15
|
import type { Root } from 'hast';
|
|
16
16
|
import type { PerfectCodeOptions } from './types.js';
|
|
17
|
+
/**
|
|
18
|
+
* Run a task function inside the mutually exclusive highlighter queue.
|
|
19
|
+
* All calls are serialized globally — the next task starts only after the
|
|
20
|
+
* current one resolves or rejects.
|
|
21
|
+
*/
|
|
22
|
+
export declare function runHighlighterTask<T>(taskFn: () => Promise<T>): Promise<T>;
|
|
23
|
+
/**
|
|
24
|
+
* Pattern 3 (adopted from VitePress): Dispose all cached highlighters and
|
|
25
|
+
* clear the cache. Call this in long-running dev servers when the theme
|
|
26
|
+
* changes, or during cleanup of a build pipeline, to release the WASM
|
|
27
|
+
* engine + loaded grammars + theme cache held by Shiki.
|
|
28
|
+
*
|
|
29
|
+
* After calling this, the next render will create a fresh highlighter.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* // In a Vite dev server shutdown hook:
|
|
33
|
+
* import { disposeHighlighter } from '@dr-ishaan/rehype-perfect-code-blocks';
|
|
34
|
+
* server.http2.close(() => disposeHighlighter());
|
|
35
|
+
*/
|
|
36
|
+
export declare function disposeHighlighter(): void;
|
|
17
37
|
/**
|
|
18
38
|
* Walk the tree; for every <pre><code> that does NOT yet look Shiki-processed
|
|
19
39
|
* (i.e. no `astro-code` / `shiki` class), tokenize it via Shiki and replace
|
package/dist/shiki.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shiki.d.ts","sourceRoot":"","sources":["../src/shiki.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,KAAK,EAAW,IAAI,EAAE,MAAM,MAAM,CAAC;AAG1C,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"shiki.d.ts","sourceRoot":"","sources":["../src/shiki.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,KAAK,EAAW,IAAI,EAAE,MAAM,MAAM,CAAC;AAG1C,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAwFrD;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAQ1E;AA4CD;;;;;;;;;;;;GAYG;AACH,wBAAgB,kBAAkB,IAAI,IAAI,CAYzC;AAgPD;;;;;GAKG;AACH,wBAAsB,mBAAmB,CACvC,IAAI,EAAE,IAAI,EACV,IAAI,EAAE,QAAQ,CAAC,kBAAkB,CAAC,GACjC,OAAO,CAAC,IAAI,CAAC,CA0Tf"}
|