@dr-ishaan/rehype-perfect-code-blocks 1.1.0 → 1.1.1
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 +2 -2
- package/dist/astro.d.ts.map +1 -1
- package/dist/astro.js +27 -8
- package/dist/astro.js.map +1 -1
- package/package.json +1 -1
- package/src/astro.ts +27 -8
package/dist/astro.d.ts
CHANGED
|
@@ -5,14 +5,14 @@
|
|
|
5
5
|
*
|
|
6
6
|
* // astro.config.mjs
|
|
7
7
|
* import { defineConfig } from 'astro/config';
|
|
8
|
-
* import perfectCode from 'rehype-perfect-code-blocks/astro';
|
|
8
|
+
* import perfectCode from '@dr-ishaan/rehype-perfect-code-blocks/astro';
|
|
9
9
|
*
|
|
10
10
|
* export default defineConfig({
|
|
11
11
|
* integrations: [
|
|
12
12
|
* perfectCode({
|
|
13
13
|
* decorations: true,
|
|
14
14
|
* copyButton: true,
|
|
15
|
-
* //
|
|
15
|
+
* rehypePlugins: [rehypeRaw], // for code blocks inside raw HTML
|
|
16
16
|
* }),
|
|
17
17
|
* ],
|
|
18
18
|
* });
|
package/dist/astro.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"astro.d.ts","sourceRoot":"","sources":["../src/astro.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,OAAO,CAAC;AAI9C,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"astro.d.ts","sourceRoot":"","sources":["../src/astro.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,OAAO,CAAC;AAI9C,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAuBrD,MAAM,CAAC,OAAO,UAAU,WAAW,CACjC,OAAO,GAAE,kBAAuB,GAC/B,gBAAgB,CAuDlB"}
|
package/dist/astro.js
CHANGED
|
@@ -5,14 +5,14 @@
|
|
|
5
5
|
*
|
|
6
6
|
* // astro.config.mjs
|
|
7
7
|
* import { defineConfig } from 'astro/config';
|
|
8
|
-
* import perfectCode from 'rehype-perfect-code-blocks/astro';
|
|
8
|
+
* import perfectCode from '@dr-ishaan/rehype-perfect-code-blocks/astro';
|
|
9
9
|
*
|
|
10
10
|
* export default defineConfig({
|
|
11
11
|
* integrations: [
|
|
12
12
|
* perfectCode({
|
|
13
13
|
* decorations: true,
|
|
14
14
|
* copyButton: true,
|
|
15
|
-
* //
|
|
15
|
+
* rehypePlugins: [rehypeRaw], // for code blocks inside raw HTML
|
|
16
16
|
* }),
|
|
17
17
|
* ],
|
|
18
18
|
* });
|
|
@@ -20,16 +20,33 @@
|
|
|
20
20
|
import { rehypePerfectCodeBlocks } from './index.js';
|
|
21
21
|
import { remarkPreserveCodeMeta } from './remark.js';
|
|
22
22
|
import { COPY_SCRIPT } from './copy-script.js';
|
|
23
|
-
|
|
24
|
-
import
|
|
23
|
+
import { readFileSync } from 'node:fs';
|
|
24
|
+
import { fileURLToPath } from 'node:url';
|
|
25
|
+
import { dirname, join } from 'node:path';
|
|
26
|
+
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
|
+
function loadCss() {
|
|
31
|
+
try {
|
|
32
|
+
return readFileSync(join(__dirname, 'styles.css'), 'utf8');
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
// Fallback: try from src/ (dev mode)
|
|
36
|
+
try {
|
|
37
|
+
return readFileSync(join(__dirname, '..', 'src', 'styles.css'), 'utf8');
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
return '';
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
25
44
|
export default function perfectCode(options = {}) {
|
|
26
45
|
return {
|
|
27
46
|
name: 'rehype-perfect-code-blocks',
|
|
28
47
|
hooks: {
|
|
29
48
|
'astro:config:setup': ({ updateConfig, injectScript }) => {
|
|
30
49
|
// 1. Register the remark + rehype plugins with the Markdown pipeline.
|
|
31
|
-
// The remark plugin preserves fence meta (title="...", {1,3-5}, flags)
|
|
32
|
-
// onto the hast tree so the rehype plugin can read it.
|
|
33
50
|
updateConfig({
|
|
34
51
|
markdown: {
|
|
35
52
|
syntaxHighlight: 'shiki',
|
|
@@ -44,7 +61,10 @@ export default function perfectCode(options = {}) {
|
|
|
44
61
|
});
|
|
45
62
|
// 2. Inject global styles (unless user opted out).
|
|
46
63
|
if (options.injectStyles !== false) {
|
|
47
|
-
|
|
64
|
+
const css = loadCss();
|
|
65
|
+
if (css) {
|
|
66
|
+
injectScript('page', `<style data-pcb>${css}</style>`);
|
|
67
|
+
}
|
|
48
68
|
}
|
|
49
69
|
// 3. Inject the copy-button script once per page.
|
|
50
70
|
if (options.copyButton !== false) {
|
|
@@ -56,7 +76,6 @@ export default function perfectCode(options = {}) {
|
|
|
56
76
|
}
|
|
57
77
|
}
|
|
58
78
|
// 4. Respect manual theme override (set on <html data-theme="...">).
|
|
59
|
-
// Validate against allowlist to prevent injection.
|
|
60
79
|
if (options.theme && options.theme !== 'auto') {
|
|
61
80
|
const safeTheme = ['dark', 'light'].includes(options.theme) ? options.theme : 'auto';
|
|
62
81
|
if (safeTheme !== 'auto') {
|
package/dist/astro.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"astro.js","sourceRoot":"","sources":["../src/astro.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAGH,OAAO,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAC;AACrD,OAAO,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AAC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ACrF,IAAI,SAAS,KAAK,MAAM,EAAE,CAAC;wBACzB,YAAY,CACV,MAAM,EACN,+DAA+D,SAAS,cAAc,CACvF,CAAC;oBACJ,CAAC;gBACH,CAAC;YACH,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.1",
|
|
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,14 +5,14 @@
|
|
|
5
5
|
*
|
|
6
6
|
* // astro.config.mjs
|
|
7
7
|
* import { defineConfig } from 'astro/config';
|
|
8
|
-
* import perfectCode from 'rehype-perfect-code-blocks/astro';
|
|
8
|
+
* import perfectCode from '@dr-ishaan/rehype-perfect-code-blocks/astro';
|
|
9
9
|
*
|
|
10
10
|
* export default defineConfig({
|
|
11
11
|
* integrations: [
|
|
12
12
|
* perfectCode({
|
|
13
13
|
* decorations: true,
|
|
14
14
|
* copyButton: true,
|
|
15
|
-
* //
|
|
15
|
+
* rehypePlugins: [rehypeRaw], // for code blocks inside raw HTML
|
|
16
16
|
* }),
|
|
17
17
|
* ],
|
|
18
18
|
* });
|
|
@@ -23,8 +23,27 @@ import { rehypePerfectCodeBlocks } from './index.js';
|
|
|
23
23
|
import { remarkPreserveCodeMeta } from './remark.js';
|
|
24
24
|
import { COPY_SCRIPT } from './copy-script.js';
|
|
25
25
|
import type { PerfectCodeOptions } from './types.js';
|
|
26
|
-
|
|
27
|
-
import
|
|
26
|
+
import { readFileSync } from 'node:fs';
|
|
27
|
+
import { fileURLToPath } from 'node:url';
|
|
28
|
+
import { dirname, join } from 'node:path';
|
|
29
|
+
|
|
30
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
31
|
+
|
|
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
|
+
function loadCss(): string {
|
|
36
|
+
try {
|
|
37
|
+
return readFileSync(join(__dirname, 'styles.css'), 'utf8');
|
|
38
|
+
} catch {
|
|
39
|
+
// Fallback: try from src/ (dev mode)
|
|
40
|
+
try {
|
|
41
|
+
return readFileSync(join(__dirname, '..', 'src', 'styles.css'), 'utf8');
|
|
42
|
+
} catch {
|
|
43
|
+
return '';
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
28
47
|
|
|
29
48
|
export default function perfectCode(
|
|
30
49
|
options: PerfectCodeOptions = {}
|
|
@@ -34,8 +53,6 @@ export default function perfectCode(
|
|
|
34
53
|
hooks: {
|
|
35
54
|
'astro:config:setup': ({ updateConfig, injectScript }) => {
|
|
36
55
|
// 1. Register the remark + rehype plugins with the Markdown pipeline.
|
|
37
|
-
// The remark plugin preserves fence meta (title="...", {1,3-5}, flags)
|
|
38
|
-
// onto the hast tree so the rehype plugin can read it.
|
|
39
56
|
updateConfig({
|
|
40
57
|
markdown: {
|
|
41
58
|
syntaxHighlight: 'shiki',
|
|
@@ -52,7 +69,10 @@ export default function perfectCode(
|
|
|
52
69
|
|
|
53
70
|
// 2. Inject global styles (unless user opted out).
|
|
54
71
|
if (options.injectStyles !== false) {
|
|
55
|
-
|
|
72
|
+
const css = loadCss();
|
|
73
|
+
if (css) {
|
|
74
|
+
injectScript('page', `<style data-pcb>${css}</style>`);
|
|
75
|
+
}
|
|
56
76
|
}
|
|
57
77
|
|
|
58
78
|
// 3. Inject the copy-button script once per page.
|
|
@@ -70,7 +90,6 @@ export default function perfectCode(
|
|
|
70
90
|
}
|
|
71
91
|
|
|
72
92
|
// 4. Respect manual theme override (set on <html data-theme="...">).
|
|
73
|
-
// Validate against allowlist to prevent injection.
|
|
74
93
|
if (options.theme && options.theme !== 'auto') {
|
|
75
94
|
const safeTheme = ['dark', 'light'].includes(options.theme) ? options.theme : 'auto';
|
|
76
95
|
if (safeTheme !== 'auto') {
|