@dr-ishaan/rehype-perfect-code-blocks 1.1.3 → 1.1.4

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 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
  * ],
@@ -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;AAuBrD,MAAM,CAAC,OAAO,UAAU,WAAW,CACjC,OAAO,GAAE,kBAAuB,GAC/B,gBAAgB,CA6DlB"}
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,CAqElB"}
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
  }
@@ -47,8 +42,6 @@ export default function perfectCode(options = {}) {
47
42
  hooks: {
48
43
  'astro:config:setup': ({ updateConfig, injectScript }) => {
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,24 +57,34 @@ export default function perfectCode(options = {}) {
64
57
  [rehypePerfectCodeBlocks, options],
65
58
  ],
66
59
  },
60
+ // 2. Inject CSS via a Vite plugin (not injectScript, which is JS-only
61
+ // in Astro v5+). The transformIndexHtml hook inserts a <style>
62
+ // tag into every page's <head>.
63
+ vite: {
64
+ plugins: [
65
+ {
66
+ name: 'rehype-perfect-code-blocks-css',
67
+ transformIndexHtml(html) {
68
+ if (options.injectStyles === false)
69
+ return html;
70
+ const css = loadCss();
71
+ if (!css)
72
+ return html;
73
+ return html.replace('</head>', `<style data-pcb>${css}</style></head>`);
74
+ },
75
+ },
76
+ ],
77
+ },
67
78
  });
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
79
  // 3. Inject the copy-button script once per page.
76
80
  if (options.copyButton !== false) {
77
81
  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.
82
+ // 3a. Graceful degradation: add .no-js to <html>.
80
83
  if (options.hideCopyWithoutJs !== false) {
81
84
  injectScript('page', `<script>document.documentElement.classList.add('no-js');</script>`);
82
85
  }
83
86
  }
84
- // 4. Respect manual theme override (set on <html data-theme="...">).
87
+ // 4. Respect manual theme override.
85
88
  if (options.theme && options.theme !== 'auto') {
86
89
  const safeTheme = ['dark', 'light'].includes(options.theme) ? options.theme : 'auto';
87
90
  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;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,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;AAE1D,kEAAkE;AAClE,oEAAoE;AACpE,sDAAsD;AACtD,SAAS,OAAO;IACd,IAAI,CAAC;QACH,OAAO,YAAY,CAAC,IAAI,C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1.1.4",
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 {
@@ -53,8 +48,6 @@ export default function perfectCode(
53
48
  hooks: {
54
49
  'astro:config:setup': ({ updateConfig, injectScript }) => {
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,22 +64,32 @@ export default function perfectCode(
71
64
  [rehypePerfectCodeBlocks, options],
72
65
  ] as never,
73
66
  },
67
+ // 2. Inject CSS via a Vite plugin (not injectScript, which is JS-only
68
+ // in Astro v5+). The transformIndexHtml hook inserts a <style>
69
+ // tag into every page's <head>.
70
+ vite: {
71
+ plugins: [
72
+ {
73
+ name: 'rehype-perfect-code-blocks-css',
74
+ transformIndexHtml(html: string) {
75
+ if (options.injectStyles === false) return html;
76
+ const css = loadCss();
77
+ if (!css) return html;
78
+ return html.replace(
79
+ '</head>',
80
+ `<style data-pcb>${css}</style></head>`
81
+ );
82
+ },
83
+ },
84
+ ],
85
+ },
74
86
  });
75
87
 
76
- // 2. Inject global styles (unless user opted out).
77
- if (options.injectStyles !== false) {
78
- const css = loadCss();
79
- if (css) {
80
- injectScript('page', `<style data-pcb>${css}</style>`);
81
- }
82
- }
83
-
84
88
  // 3. Inject the copy-button script once per page.
85
89
  if (options.copyButton !== false) {
86
90
  injectScript('page', `<script>${COPY_SCRIPT}</script>`);
87
91
 
88
- // 3a. Graceful degradation: add .no-js to <html> so copy buttons are
89
- // hidden via CSS until the script removes the class.
92
+ // 3a. Graceful degradation: add .no-js to <html>.
90
93
  if (options.hideCopyWithoutJs !== false) {
91
94
  injectScript(
92
95
  'page',
@@ -95,7 +98,7 @@ export default function perfectCode(
95
98
  }
96
99
  }
97
100
 
98
- // 4. Respect manual theme override (set on <html data-theme="...">).
101
+ // 4. Respect manual theme override.
99
102
  if (options.theme && options.theme !== 'auto') {
100
103
  const safeTheme = ['dark', 'light'].includes(options.theme) ? options.theme : 'auto';
101
104
  if (safeTheme !== 'auto') {