@dr-ishaan/rehype-perfect-code-blocks 1.1.3 → 1.1.5
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/dist/astro.d.ts +1 -2
- package/dist/astro.d.ts.map +1 -1
- package/dist/astro.js +42 -32
- package/dist/astro.js.map +1 -1
- package/package.json +1 -1
- package/src/astro.ts +47 -39
package/dist/astro.d.ts
CHANGED
|
@@ -5,13 +5,12 @@
|
|
|
5
5
|
*
|
|
6
6
|
* // astro.config.mjs
|
|
7
7
|
* import { defineConfig } from 'astro/config';
|
|
8
|
+
* import rehypeRaw from 'rehype-raw';
|
|
8
9
|
* import perfectCode from '@dr-ishaan/rehype-perfect-code-blocks/astro';
|
|
9
10
|
*
|
|
10
11
|
* export default defineConfig({
|
|
11
12
|
* integrations: [
|
|
12
13
|
* perfectCode({
|
|
13
|
-
* decorations: true,
|
|
14
|
-
* copyButton: true,
|
|
15
14
|
* rehypePlugins: [rehypeRaw], // for code blocks inside raw HTML
|
|
16
15
|
* }),
|
|
17
16
|
* ],
|
package/dist/astro.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"astro.d.ts","sourceRoot":"","sources":["../src/astro.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"astro.d.ts","sourceRoot":"","sources":["../src/astro.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,OAAO,CAAC;AAI9C,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAmBrD,MAAM,CAAC,OAAO,UAAU,WAAW,CACjC,OAAO,GAAE,kBAAuB,GAC/B,gBAAgB,CA0ElB"}
|
package/dist/astro.js
CHANGED
|
@@ -5,13 +5,12 @@
|
|
|
5
5
|
*
|
|
6
6
|
* // astro.config.mjs
|
|
7
7
|
* import { defineConfig } from 'astro/config';
|
|
8
|
+
* import rehypeRaw from 'rehype-raw';
|
|
8
9
|
* import perfectCode from '@dr-ishaan/rehype-perfect-code-blocks/astro';
|
|
9
10
|
*
|
|
10
11
|
* export default defineConfig({
|
|
11
12
|
* integrations: [
|
|
12
13
|
* perfectCode({
|
|
13
|
-
* decorations: true,
|
|
14
|
-
* copyButton: true,
|
|
15
14
|
* rehypePlugins: [rehypeRaw], // for code blocks inside raw HTML
|
|
16
15
|
* }),
|
|
17
16
|
* ],
|
|
@@ -24,15 +23,11 @@ import { readFileSync } from 'node:fs';
|
|
|
24
23
|
import { fileURLToPath } from 'node:url';
|
|
25
24
|
import { dirname, join } from 'node:path';
|
|
26
25
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
27
|
-
// Read styles.css at runtime instead of using Vite's ?raw import.
|
|
28
|
-
// This avoids "Unknown file extension .css" errors when Astro loads
|
|
29
|
-
// the config via Node's ESM loader (outside of Vite).
|
|
30
26
|
function loadCss() {
|
|
31
27
|
try {
|
|
32
28
|
return readFileSync(join(__dirname, 'styles.css'), 'utf8');
|
|
33
29
|
}
|
|
34
30
|
catch {
|
|
35
|
-
// Fallback: try from src/ (dev mode)
|
|
36
31
|
try {
|
|
37
32
|
return readFileSync(join(__dirname, '..', 'src', 'styles.css'), 'utf8');
|
|
38
33
|
}
|
|
@@ -45,10 +40,8 @@ export default function perfectCode(options = {}) {
|
|
|
45
40
|
return {
|
|
46
41
|
name: 'rehype-perfect-code-blocks',
|
|
47
42
|
hooks: {
|
|
48
|
-
'astro:config:setup': ({ updateConfig
|
|
43
|
+
'astro:config:setup': ({ updateConfig }) => {
|
|
49
44
|
// 1. Register the remark + rehype plugins with the Markdown pipeline.
|
|
50
|
-
// User-supplied rehypePlugins (e.g. rehype-raw) come FIRST so they
|
|
51
|
-
// run before our plugin (needed for raw HTML parsing).
|
|
52
45
|
const userRehypePlugins = options.rehypePlugins ?? [];
|
|
53
46
|
updateConfig({
|
|
54
47
|
markdown: {
|
|
@@ -64,30 +57,47 @@ export default function perfectCode(options = {}) {
|
|
|
64
57
|
[rehypePerfectCodeBlocks, options],
|
|
65
58
|
],
|
|
66
59
|
},
|
|
60
|
+
// 2. Inject CSS + scripts via Vite's transformIndexHtml hook.
|
|
61
|
+
// We can't use Astro's injectScript() because Astro v5 tries to
|
|
62
|
+
// parse/execute the injected JS during build (SSR), which fails
|
|
63
|
+
// because `document` isn't available. transformIndexHtml just
|
|
64
|
+
// inserts raw HTML into <head>.
|
|
65
|
+
vite: {
|
|
66
|
+
plugins: [
|
|
67
|
+
{
|
|
68
|
+
name: 'rehype-perfect-code-blocks-inject',
|
|
69
|
+
transformIndexHtml(html) {
|
|
70
|
+
const injections = [];
|
|
71
|
+
// CSS
|
|
72
|
+
if (options.injectStyles !== false) {
|
|
73
|
+
const css = loadCss();
|
|
74
|
+
if (css) {
|
|
75
|
+
injections.push(`<style data-pcb>${css}</style>`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
// Copy-button script
|
|
79
|
+
if (options.copyButton !== false) {
|
|
80
|
+
injections.push(`<script>${COPY_SCRIPT}</script>`);
|
|
81
|
+
// Graceful degradation: .no-js class
|
|
82
|
+
if (options.hideCopyWithoutJs !== false) {
|
|
83
|
+
injections.push(`<script>document.documentElement.classList.add('no-js');</script>`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
// Manual theme override
|
|
87
|
+
if (options.theme && options.theme !== 'auto') {
|
|
88
|
+
const safeTheme = ['dark', 'light'].includes(options.theme) ? options.theme : 'auto';
|
|
89
|
+
if (safeTheme !== 'auto') {
|
|
90
|
+
injections.push(`<script>document.documentElement.setAttribute('data-theme','${safeTheme}');</script>`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (injections.length === 0)
|
|
94
|
+
return html;
|
|
95
|
+
return html.replace('</head>', `${injections.join('\n')}</head>`);
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
],
|
|
99
|
+
},
|
|
67
100
|
});
|
|
68
|
-
// 2. Inject global styles (unless user opted out).
|
|
69
|
-
if (options.injectStyles !== false) {
|
|
70
|
-
const css = loadCss();
|
|
71
|
-
if (css) {
|
|
72
|
-
injectScript('page', `<style data-pcb>${css}</style>`);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
// 3. Inject the copy-button script once per page.
|
|
76
|
-
if (options.copyButton !== false) {
|
|
77
|
-
injectScript('page', `<script>${COPY_SCRIPT}</script>`);
|
|
78
|
-
// 3a. Graceful degradation: add .no-js to <html> so copy buttons are
|
|
79
|
-
// hidden via CSS until the script removes the class.
|
|
80
|
-
if (options.hideCopyWithoutJs !== false) {
|
|
81
|
-
injectScript('page', `<script>document.documentElement.classList.add('no-js');</script>`);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
// 4. Respect manual theme override (set on <html data-theme="...">).
|
|
85
|
-
if (options.theme && options.theme !== 'auto') {
|
|
86
|
-
const safeTheme = ['dark', 'light'].includes(options.theme) ? options.theme : 'auto';
|
|
87
|
-
if (safeTheme !== 'auto') {
|
|
88
|
-
injectScript('page', `<script>document.documentElement.setAttribute('data-theme','${safeTheme}');</script>`);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
101
|
},
|
|
92
102
|
},
|
|
93
103
|
};
|
package/dist/astro.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"astro.js","sourceRoot":"","sources":["../src/astro.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"astro.js","sourceRoot":"","sources":["../src/astro.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAGH,OAAO,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAC;AACrD,OAAO,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,aAAa,E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C,OAAO,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,GAAG,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;gCACpE,CAAC;6BACF;yBACF;qBACF;iBACF,CAAC,CAAC;YACL,CAAC;SACF;KACF,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dr-ishaan/rehype-perfect-code-blocks",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.5",
|
|
4
4
|
"description": "Beautiful, configurable code blocks for Astro / MDX / any rehype pipeline. Built on Shiki, inspired by rehype-pretty-code, VitePress, Docusaurus, and Expressive Code.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
package/src/astro.ts
CHANGED
|
@@ -5,13 +5,12 @@
|
|
|
5
5
|
*
|
|
6
6
|
* // astro.config.mjs
|
|
7
7
|
* import { defineConfig } from 'astro/config';
|
|
8
|
+
* import rehypeRaw from 'rehype-raw';
|
|
8
9
|
* import perfectCode from '@dr-ishaan/rehype-perfect-code-blocks/astro';
|
|
9
10
|
*
|
|
10
11
|
* export default defineConfig({
|
|
11
12
|
* integrations: [
|
|
12
13
|
* perfectCode({
|
|
13
|
-
* decorations: true,
|
|
14
|
-
* copyButton: true,
|
|
15
14
|
* rehypePlugins: [rehypeRaw], // for code blocks inside raw HTML
|
|
16
15
|
* }),
|
|
17
16
|
* ],
|
|
@@ -29,14 +28,10 @@ import { dirname, join } from 'node:path';
|
|
|
29
28
|
|
|
30
29
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
31
30
|
|
|
32
|
-
// Read styles.css at runtime instead of using Vite's ?raw import.
|
|
33
|
-
// This avoids "Unknown file extension .css" errors when Astro loads
|
|
34
|
-
// the config via Node's ESM loader (outside of Vite).
|
|
35
31
|
function loadCss(): string {
|
|
36
32
|
try {
|
|
37
33
|
return readFileSync(join(__dirname, 'styles.css'), 'utf8');
|
|
38
34
|
} catch {
|
|
39
|
-
// Fallback: try from src/ (dev mode)
|
|
40
35
|
try {
|
|
41
36
|
return readFileSync(join(__dirname, '..', 'src', 'styles.css'), 'utf8');
|
|
42
37
|
} catch {
|
|
@@ -51,10 +46,8 @@ export default function perfectCode(
|
|
|
51
46
|
return {
|
|
52
47
|
name: 'rehype-perfect-code-blocks',
|
|
53
48
|
hooks: {
|
|
54
|
-
'astro:config:setup': ({ updateConfig
|
|
49
|
+
'astro:config:setup': ({ updateConfig }) => {
|
|
55
50
|
// 1. Register the remark + rehype plugins with the Markdown pipeline.
|
|
56
|
-
// User-supplied rehypePlugins (e.g. rehype-raw) come FIRST so they
|
|
57
|
-
// run before our plugin (needed for raw HTML parsing).
|
|
58
51
|
const userRehypePlugins = options.rehypePlugins ?? [];
|
|
59
52
|
updateConfig({
|
|
60
53
|
markdown: {
|
|
@@ -71,40 +64,55 @@ export default function perfectCode(
|
|
|
71
64
|
[rehypePerfectCodeBlocks, options],
|
|
72
65
|
] as never,
|
|
73
66
|
},
|
|
74
|
-
|
|
67
|
+
// 2. Inject CSS + scripts via Vite's transformIndexHtml hook.
|
|
68
|
+
// We can't use Astro's injectScript() because Astro v5 tries to
|
|
69
|
+
// parse/execute the injected JS during build (SSR), which fails
|
|
70
|
+
// because `document` isn't available. transformIndexHtml just
|
|
71
|
+
// inserts raw HTML into <head>.
|
|
72
|
+
vite: {
|
|
73
|
+
plugins: [
|
|
74
|
+
{
|
|
75
|
+
name: 'rehype-perfect-code-blocks-inject',
|
|
76
|
+
transformIndexHtml(html: string) {
|
|
77
|
+
const injections: string[] = [];
|
|
78
|
+
|
|
79
|
+
// CSS
|
|
80
|
+
if (options.injectStyles !== false) {
|
|
81
|
+
const css = loadCss();
|
|
82
|
+
if (css) {
|
|
83
|
+
injections.push(`<style data-pcb>${css}</style>`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
75
86
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
if (css) {
|
|
80
|
-
injectScript('page', `<style data-pcb>${css}</style>`);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
87
|
+
// Copy-button script
|
|
88
|
+
if (options.copyButton !== false) {
|
|
89
|
+
injections.push(`<script>${COPY_SCRIPT}</script>`);
|
|
83
90
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
91
|
+
// Graceful degradation: .no-js class
|
|
92
|
+
if (options.hideCopyWithoutJs !== false) {
|
|
93
|
+
injections.push(
|
|
94
|
+
`<script>document.documentElement.classList.add('no-js');</script>`
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
87
98
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
99
|
+
// Manual theme override
|
|
100
|
+
if (options.theme && options.theme !== 'auto') {
|
|
101
|
+
const safeTheme = ['dark', 'light'].includes(options.theme) ? options.theme : 'auto';
|
|
102
|
+
if (safeTheme !== 'auto') {
|
|
103
|
+
injections.push(
|
|
104
|
+
`<script>document.documentElement.setAttribute('data-theme','${safeTheme}');</script>`
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
97
108
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
109
|
+
if (injections.length === 0) return html;
|
|
110
|
+
return html.replace('</head>', `${injections.join('\n')}</head>`);
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
],
|
|
114
|
+
},
|
|
115
|
+
});
|
|
108
116
|
},
|
|
109
117
|
},
|
|
110
118
|
};
|