|
1 | 1 | import { pluginCollapsibleSections } from "@expressive-code/plugin-collapsible-sections"; |
2 | 2 | import { ExpressiveCodeTheme } from "@astrojs/starlight/expressive-code"; |
3 | | -import fs from "node:fs"; |
4 | | -import path from "node:path"; |
| 3 | +import { readFileSync } from "node:fs"; |
5 | 4 |
|
6 | | -// Define allowed paths relative to project root |
7 | | -const ALLOWED_PATHS = ["src/themes/expressive-code"]; |
8 | | - |
9 | | -function readFileSyncSafe(url) { |
10 | | - if (url.protocol !== "file:") { |
11 | | - throw new Error("Invalid URL protocol"); |
12 | | - } |
13 | | - |
14 | | - // Convert URL to filesystem path and normalize |
15 | | - const filePath = path.normalize(url.pathname); |
16 | | - |
17 | | - // Ensure path is within allowed directories |
18 | | - const isAllowed = ALLOWED_PATHS.some((allowedPath) => |
19 | | - filePath.includes(path.normalize(allowedPath)) |
20 | | - ); |
21 | | - |
22 | | - if (!isAllowed) { |
23 | | - throw new Error("Access to this directory is not allowed"); |
24 | | - } |
25 | | - |
26 | | - return fs.readFileSync(url, "utf-8"); |
27 | | -} |
28 | | - |
29 | | -const jsoncStringLight = readFileSyncSafe( |
30 | | - new URL( |
31 | | - "./src/themes/expressive-code/Snazzy-Light-color-theme.json", |
32 | | - import.meta.url |
33 | | - ) |
| 5 | +// Load theme files as raw strings (supports JSON with comments/trailing commas) |
| 6 | +const jsoncStringLight = readFileSync( |
| 7 | + "./src/themes/expressive-code/Snazzy-Light-color-theme.json", |
| 8 | + "utf-8" |
34 | 9 | ); |
35 | | - |
36 | | -const jsoncStringDark = readFileSyncSafe( |
37 | | - new URL( |
38 | | - "./src/themes/expressive-code/aura-soft-dark-soft-text-color-theme.json", |
39 | | - import.meta.url |
40 | | - ) |
| 10 | +const jsoncStringDark = readFileSync( |
| 11 | + "./src/themes/expressive-code/aura-soft-dark-soft-text-color-theme.json", |
| 12 | + "utf-8" |
41 | 13 | ); |
42 | 14 |
|
| 15 | +// Create themes from raw JSONC strings |
43 | 16 | const darkMode = ExpressiveCodeTheme.fromJSONString(jsoncStringDark); |
44 | 17 | const lightMode = ExpressiveCodeTheme.fromJSONString(jsoncStringLight); |
45 | 18 |
|
|
0 commit comments