@dr-ishaan/rehype-perfect-code-blocks 1.1.4 → 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.map +1 -1
- package/dist/astro.js +32 -25
- package/dist/astro.js.map +1 -1
- package/package.json +1 -1
- package/src/astro.ts +41 -36
package/dist/astro.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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,
|
|
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
|
@@ -40,7 +40,7 @@ export default function perfectCode(options = {}) {
|
|
|
40
40
|
return {
|
|
41
41
|
name: 'rehype-perfect-code-blocks',
|
|
42
42
|
hooks: {
|
|
43
|
-
'astro:config:setup': ({ updateConfig
|
|
43
|
+
'astro:config:setup': ({ updateConfig }) => {
|
|
44
44
|
// 1. Register the remark + rehype plugins with the Markdown pipeline.
|
|
45
45
|
const userRehypePlugins = options.rehypePlugins ?? [];
|
|
46
46
|
updateConfig({
|
|
@@ -57,40 +57,47 @@ export default function perfectCode(options = {}) {
|
|
|
57
57
|
[rehypePerfectCodeBlocks, options],
|
|
58
58
|
],
|
|
59
59
|
},
|
|
60
|
-
// 2. Inject CSS via
|
|
61
|
-
//
|
|
62
|
-
//
|
|
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>.
|
|
63
65
|
vite: {
|
|
64
66
|
plugins: [
|
|
65
67
|
{
|
|
66
|
-
name: 'rehype-perfect-code-blocks-
|
|
68
|
+
name: 'rehype-perfect-code-blocks-inject',
|
|
67
69
|
transformIndexHtml(html) {
|
|
68
|
-
|
|
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)
|
|
69
94
|
return html;
|
|
70
|
-
|
|
71
|
-
if (!css)
|
|
72
|
-
return html;
|
|
73
|
-
return html.replace('</head>', `<style data-pcb>${css}</style></head>`);
|
|
95
|
+
return html.replace('</head>', `${injections.join('\n')}</head>`);
|
|
74
96
|
},
|
|
75
97
|
},
|
|
76
98
|
],
|
|
77
99
|
},
|
|
78
100
|
});
|
|
79
|
-
// 3. Inject the copy-button script once per page.
|
|
80
|
-
if (options.copyButton !== false) {
|
|
81
|
-
injectScript('page', `<script>${COPY_SCRIPT}</script>`);
|
|
82
|
-
// 3a. Graceful degradation: add .no-js to <html>.
|
|
83
|
-
if (options.hideCopyWithoutJs !== false) {
|
|
84
|
-
injectScript('page', `<script>document.documentElement.classList.add('no-js');</script>`);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
// 4. Respect manual theme override.
|
|
88
|
-
if (options.theme && options.theme !== 'auto') {
|
|
89
|
-
const safeTheme = ['dark', 'light'].includes(options.theme) ? options.theme : 'auto';
|
|
90
|
-
if (safeTheme !== 'auto') {
|
|
91
|
-
injectScript('page', `<script>document.documentElement.setAttribute('data-theme','${safeTheme}');</script>`);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
101
|
},
|
|
95
102
|
},
|
|
96
103
|
};
|
package/dist/astro.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAE1C,MAAM,SAAS,GAAG,OAAO,CAAC,aAA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+
"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
|
@@ -46,7 +46,7 @@ export default function perfectCode(
|
|
|
46
46
|
return {
|
|
47
47
|
name: 'rehype-perfect-code-blocks',
|
|
48
48
|
hooks: {
|
|
49
|
-
'astro:config:setup': ({ updateConfig
|
|
49
|
+
'astro:config:setup': ({ updateConfig }) => {
|
|
50
50
|
// 1. Register the remark + rehype plugins with the Markdown pipeline.
|
|
51
51
|
const userRehypePlugins = options.rehypePlugins ?? [];
|
|
52
52
|
updateConfig({
|
|
@@ -64,50 +64,55 @@ export default function perfectCode(
|
|
|
64
64
|
[rehypePerfectCodeBlocks, options],
|
|
65
65
|
] as never,
|
|
66
66
|
},
|
|
67
|
-
// 2. Inject CSS via
|
|
68
|
-
//
|
|
69
|
-
//
|
|
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>.
|
|
70
72
|
vite: {
|
|
71
73
|
plugins: [
|
|
72
74
|
{
|
|
73
|
-
name: 'rehype-perfect-code-blocks-
|
|
75
|
+
name: 'rehype-perfect-code-blocks-inject',
|
|
74
76
|
transformIndexHtml(html: string) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
+
}
|
|
86
|
+
|
|
87
|
+
// Copy-button script
|
|
88
|
+
if (options.copyButton !== false) {
|
|
89
|
+
injections.push(`<script>${COPY_SCRIPT}</script>`);
|
|
90
|
+
|
|
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
|
+
}
|
|
98
|
+
|
|
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
|
+
}
|
|
108
|
+
|
|
109
|
+
if (injections.length === 0) return html;
|
|
110
|
+
return html.replace('</head>', `${injections.join('\n')}</head>`);
|
|
82
111
|
},
|
|
83
112
|
},
|
|
84
113
|
],
|
|
85
114
|
},
|
|
86
115
|
});
|
|
87
|
-
|
|
88
|
-
// 3. Inject the copy-button script once per page.
|
|
89
|
-
if (options.copyButton !== false) {
|
|
90
|
-
injectScript('page', `<script>${COPY_SCRIPT}</script>`);
|
|
91
|
-
|
|
92
|
-
// 3a. Graceful degradation: add .no-js to <html>.
|
|
93
|
-
if (options.hideCopyWithoutJs !== false) {
|
|
94
|
-
injectScript(
|
|
95
|
-
'page',
|
|
96
|
-
`<script>document.documentElement.classList.add('no-js');</script>`
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// 4. Respect manual theme override.
|
|
102
|
-
if (options.theme && options.theme !== 'auto') {
|
|
103
|
-
const safeTheme = ['dark', 'light'].includes(options.theme) ? options.theme : 'auto';
|
|
104
|
-
if (safeTheme !== 'auto') {
|
|
105
|
-
injectScript(
|
|
106
|
-
'page',
|
|
107
|
-
`<script>document.documentElement.setAttribute('data-theme','${safeTheme}');</script>`
|
|
108
|
-
);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
116
|
},
|
|
112
117
|
},
|
|
113
118
|
};
|