|
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"; |
5 | | -import { fileURLToPath } from "node:url"; |
| 3 | +import { readFileSync } from "node:fs"; |
6 | 4 |
|
7 | | -// Define allowed paths relative to this config file's directory |
8 | | -const ALLOWED_PATHS = ["src/themes/expressive-code"]; |
9 | | - |
10 | | -function readFileSyncSafe(url) { |
11 | | - if (url.protocol !== "file:") { |
12 | | - throw new Error("Invalid URL protocol"); |
13 | | - } |
14 | | - |
15 | | - // Convert URL to filesystem path and normalize (Windows-safe) |
16 | | - const filePath = path.normalize(fileURLToPath(url)); |
17 | | - |
18 | | - // Resolve allowed directories relative to this file's directory |
19 | | - const baseDir = path.dirname(fileURLToPath(import.meta.url)); |
20 | | - const allowedAbs = ALLOWED_PATHS.map((p) => path.resolve(baseDir, p)); |
21 | | - |
22 | | - // Ensure path is within one of the allowed directories using path.relative |
23 | | - const isAllowed = allowedAbs.some((allowedDir) => { |
24 | | - const rel = path.relative(allowedDir, filePath); |
25 | | - return rel && !rel.startsWith("..") && !path.isAbsolute(rel); |
26 | | - }); |
27 | | - |
28 | | - if (!isAllowed) { |
29 | | - throw new Error(`Access to this file is not allowed: ${filePath}`); |
30 | | - } |
31 | | - |
32 | | - return fs.readFileSync(filePath, "utf-8"); |
33 | | -} |
34 | | - |
35 | | -const jsoncStringLight = readFileSyncSafe( |
36 | | - new URL( |
37 | | - "./src/themes/expressive-code/Snazzy-Light-color-theme.json", |
38 | | - import.meta.url |
39 | | - ) |
| 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" |
40 | 9 | ); |
41 | | - |
42 | | -const jsoncStringDark = readFileSyncSafe( |
43 | | - new URL( |
44 | | - "./src/themes/expressive-code/aura-soft-dark-soft-text-color-theme.json", |
45 | | - import.meta.url |
46 | | - ) |
| 10 | +const jsoncStringDark = readFileSync( |
| 11 | + "./src/themes/expressive-code/aura-soft-dark-soft-text-color-theme.json", |
| 12 | + "utf-8" |
47 | 13 | ); |
48 | 14 |
|
| 15 | +// Create themes from raw JSONC strings |
49 | 16 | const darkMode = ExpressiveCodeTheme.fromJSONString(jsoncStringDark); |
50 | 17 | const lightMode = ExpressiveCodeTheme.fromJSONString(jsoncStringLight); |
51 | 18 |
|
|
0 commit comments