Skip to content

Commit d922b7c

Browse files
committed
Update ec.config.mjs
1 parent d1ddcb8 commit d922b7c

File tree

1 file changed

+9
-42
lines changed

1 file changed

+9
-42
lines changed

docs/ec.config.mjs

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,18 @@
11
import { pluginCollapsibleSections } from "@expressive-code/plugin-collapsible-sections";
22
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";
64

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"
409
);
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"
4713
);
4814

15+
// Create themes from raw JSONC strings
4916
const darkMode = ExpressiveCodeTheme.fromJSONString(jsoncStringDark);
5017
const lightMode = ExpressiveCodeTheme.fromJSONString(jsoncStringLight);
5118

0 commit comments

Comments
 (0)