@contentbit/studio 2.0.0-next.1 → 2.0.0-next.2
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/server/api.d.ts.map +1 -1
- package/dist/server/index.js +73 -39
- package/package.json +4 -4
- package/src/server/api.ts +16 -13
- package/src/server/scan.ts +4 -4
- package/src/server/site.test.ts +32 -3
package/dist/server/api.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../src/server/api.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA;
|
|
1
|
+
{"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../src/server/api.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA;AAKhE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAE/C,wBAAsB,sBAAsB,CAC1C,OAAO,EAAE,aAAa,EACtB,GAAG,EAAE,eAAe,EACpB,GAAG,EAAE,cAAc,GAClB,OAAO,CAAC,OAAO,CAAC,CA0DlB"}
|
package/dist/server/index.js
CHANGED
|
@@ -1809,16 +1809,10 @@ import { spawn } from "node:child_process";
|
|
|
1809
1809
|
import { existsSync as existsSync4 } from "node:fs";
|
|
1810
1810
|
import { createRequire } from "node:module";
|
|
1811
1811
|
import { createServer as createNetServer } from "node:net";
|
|
1812
|
-
import { dirname, isAbsolute as isAbsolute3, join as join5, resolve as resolvePath } from "node:path";
|
|
1812
|
+
import { dirname as dirname2, isAbsolute as isAbsolute3, join as join5, resolve as resolvePath } from "node:path";
|
|
1813
1813
|
import { fileURLToPath } from "node:url";
|
|
1814
1814
|
import { createServer } from "vite";
|
|
1815
1815
|
|
|
1816
|
-
// src/server/scan.ts
|
|
1817
|
-
import {
|
|
1818
|
-
createSeoBrief as createSeoBrief2,
|
|
1819
|
-
readContentPageFacts as readContentPageFacts2
|
|
1820
|
-
} from "@contentbit/core";
|
|
1821
|
-
|
|
1822
1816
|
// ../built-site/dist/findings.js
|
|
1823
1817
|
var DEFAULT_SEVERITY_BY_CODE = {
|
|
1824
1818
|
CB_PAGE_TITLE_MISSING: "error",
|
|
@@ -5721,11 +5715,11 @@ function compileEntry(entry, index) {
|
|
|
5721
5715
|
function list(value) {
|
|
5722
5716
|
return value === void 0 ? [] : [value].flat();
|
|
5723
5717
|
}
|
|
5724
|
-
function globMatcher(globs,
|
|
5718
|
+
function globMatcher(globs, normalize2 = (value) => value) {
|
|
5725
5719
|
if (globs.length === 0)
|
|
5726
5720
|
return void 0;
|
|
5727
|
-
const isMatch = (0, import_picomatch.default)(globs.map(
|
|
5728
|
-
return (values) => values.some((value) => isMatch(
|
|
5721
|
+
const isMatch = (0, import_picomatch.default)(globs.map(normalize2), { dot: true, windows: false });
|
|
5722
|
+
return (values) => values.some((value) => isMatch(normalize2(value)));
|
|
5729
5723
|
}
|
|
5730
5724
|
function withoutTrailingSlash(value) {
|
|
5731
5725
|
return value.length > 1 && value.endsWith("/") ? value.slice(0, -1) : value;
|
|
@@ -5737,7 +5731,7 @@ import { createBlockRegistry, scanContentProject } from "@contentbit/core";
|
|
|
5737
5731
|
import { createHash } from "node:crypto";
|
|
5738
5732
|
import { existsSync } from "node:fs";
|
|
5739
5733
|
import { readFile as readFile2 } from "node:fs/promises";
|
|
5740
|
-
import { extname, isAbsolute, join as join2 } from "node:path";
|
|
5734
|
+
import { basename as basename2, dirname, extname, isAbsolute, join as join2 } from "node:path";
|
|
5741
5735
|
import { pathToFileURL } from "node:url";
|
|
5742
5736
|
import { glob } from "tinyglobby";
|
|
5743
5737
|
var ProjectLoadError = class extends Error {
|
|
@@ -5789,7 +5783,35 @@ async function loadRegistry(registryPath, options = {}) {
|
|
|
5789
5783
|
return registry;
|
|
5790
5784
|
}
|
|
5791
5785
|
async function importFresh(path) {
|
|
5792
|
-
|
|
5786
|
+
const { href } = await versionedUrl(path);
|
|
5787
|
+
try {
|
|
5788
|
+
return await import(href);
|
|
5789
|
+
} catch (err) {
|
|
5790
|
+
if (err instanceof SyntaxError && await isEsmInCommonJsPackage(path)) {
|
|
5791
|
+
const extension = extname(path);
|
|
5792
|
+
throw new ProjectLoadError(2, `${path} uses import or export, but its package.json sets "type": "commonjs", so Node reads it as CommonJS. Rename it to ${basename2(path, extension)}.m${extension.slice(1)}.`);
|
|
5793
|
+
}
|
|
5794
|
+
throw err;
|
|
5795
|
+
}
|
|
5796
|
+
}
|
|
5797
|
+
async function isEsmInCommonJsPackage(path) {
|
|
5798
|
+
if (!/\.[jt]s$/.test(path))
|
|
5799
|
+
return false;
|
|
5800
|
+
const source = await readFile2(path, "utf8").catch(() => "");
|
|
5801
|
+
if (!/^\s*(?:import|export)\s/m.test(source))
|
|
5802
|
+
return false;
|
|
5803
|
+
for (let dir = dirname(path); ; dir = dirname(dir)) {
|
|
5804
|
+
const manifest = await readFile2(join2(dir, "package.json"), "utf8").catch(() => void 0);
|
|
5805
|
+
if (manifest !== void 0) {
|
|
5806
|
+
try {
|
|
5807
|
+
return JSON.parse(manifest).type === "commonjs";
|
|
5808
|
+
} catch {
|
|
5809
|
+
return false;
|
|
5810
|
+
}
|
|
5811
|
+
}
|
|
5812
|
+
if (dirname(dir) === dir)
|
|
5813
|
+
return false;
|
|
5814
|
+
}
|
|
5793
5815
|
}
|
|
5794
5816
|
async function versionedUrl(path) {
|
|
5795
5817
|
const url = pathToFileURL(path);
|
|
@@ -5808,6 +5830,7 @@ async function loadContentFiles(files, input) {
|
|
|
5808
5830
|
const linkOptions = input.linkOptions ?? {};
|
|
5809
5831
|
const scan = scanContentProject(sources, registry, {
|
|
5810
5832
|
linkOptions,
|
|
5833
|
+
root: input.cwd ?? process.cwd(),
|
|
5811
5834
|
...input.scan
|
|
5812
5835
|
});
|
|
5813
5836
|
return { files, registry, sources, linkOptions, scan };
|
|
@@ -5987,15 +6010,15 @@ async function readBuild(cmd, path, root) {
|
|
|
5987
6010
|
} catch (error) {
|
|
5988
6011
|
const code = error.code;
|
|
5989
6012
|
if (code === "ENOENT") {
|
|
5990
|
-
throw new ProjectLoadError(2, `${cmd}: the build folder ${displayPath(path)} does not exist. ${buildAdvice(root)}`);
|
|
6013
|
+
throw new ProjectLoadError(2, `${cmd}: the build folder ${displayPath(path, root)} does not exist. ${buildAdvice(root)}`);
|
|
5991
6014
|
}
|
|
5992
6015
|
if (code === "ENOTDIR") {
|
|
5993
|
-
throw new ProjectLoadError(2, `${cmd}: ${displayPath(path)} is not a folder.`);
|
|
6016
|
+
throw new ProjectLoadError(2, `${cmd}: ${displayPath(path, root)} is not a folder.`);
|
|
5994
6017
|
}
|
|
5995
6018
|
throw error;
|
|
5996
6019
|
}
|
|
5997
6020
|
if (site.pages.length === 0) {
|
|
5998
|
-
throw new ProjectLoadError(2, `${cmd}: the build folder ${displayPath(path)} holds no HTML page. ${buildAdvice(root)}`);
|
|
6021
|
+
throw new ProjectLoadError(2, `${cmd}: the build folder ${displayPath(path, root)} holds no HTML page. ${buildAdvice(root)}`);
|
|
5999
6022
|
}
|
|
6000
6023
|
return site;
|
|
6001
6024
|
}
|
|
@@ -6006,8 +6029,15 @@ function buildAdvice(root) {
|
|
|
6006
6029
|
return server;
|
|
6007
6030
|
return `Build the site with ${buildCommand(root)} first, or set site.build in contentbit.config.ts to the folder its build writes.`;
|
|
6008
6031
|
}
|
|
6009
|
-
function displayPath(file) {
|
|
6010
|
-
|
|
6032
|
+
function displayPath(file, root) {
|
|
6033
|
+
if (relative2(root, file) === "")
|
|
6034
|
+
return ".";
|
|
6035
|
+
return relativeInside(root, file) ?? file;
|
|
6036
|
+
}
|
|
6037
|
+
function pathsFromRoot(root, build) {
|
|
6038
|
+
const folders = build === void 0 ? [root] : [root, build];
|
|
6039
|
+
const isPath = (text) => folders.some((folder) => text === folder || text.startsWith(`${folder}${sep}`));
|
|
6040
|
+
return (_key, value) => typeof value === "string" && isPath(value) ? relative2(root, value).split(sep).join("/") || "." : value;
|
|
6011
6041
|
}
|
|
6012
6042
|
function relativeInside(folder, file) {
|
|
6013
6043
|
const path = relative2(folder, file);
|
|
@@ -6088,7 +6118,11 @@ function resolveOrigin(flag, configured, site) {
|
|
|
6088
6118
|
}
|
|
6089
6119
|
|
|
6090
6120
|
// src/server/scan.ts
|
|
6091
|
-
import {
|
|
6121
|
+
import {
|
|
6122
|
+
createSeoBrief as createSeoBrief2,
|
|
6123
|
+
readContentPageFacts as readContentPageFacts2
|
|
6124
|
+
} from "@contentbit/core";
|
|
6125
|
+
import { basename as basename3, normalize } from "node:path";
|
|
6092
6126
|
|
|
6093
6127
|
// src/server/preview.ts
|
|
6094
6128
|
import { ContentBlocks } from "@contentbit/react";
|
|
@@ -6389,7 +6423,7 @@ function fileSummary(file, cwd, frontmatter, stats, findings, options, seoPage)
|
|
|
6389
6423
|
};
|
|
6390
6424
|
}
|
|
6391
6425
|
function titleFor(file, frontmatter, stats) {
|
|
6392
|
-
return stringValue(frontmatter.title) ?? stats.outline[0]?.text ??
|
|
6426
|
+
return stringValue(frontmatter.title) ?? stats.outline[0]?.text ?? basename3(file);
|
|
6393
6427
|
}
|
|
6394
6428
|
function summarize(findings) {
|
|
6395
6429
|
const count = (severity) => findings.filter((finding) => finding.severity === severity).length;
|
|
@@ -6402,9 +6436,8 @@ function statusFor(findings) {
|
|
|
6402
6436
|
return "healthy";
|
|
6403
6437
|
}
|
|
6404
6438
|
function findAllowedFile(context, cwd, requestedPath) {
|
|
6405
|
-
|
|
6406
|
-
|
|
6407
|
-
) ?? null;
|
|
6439
|
+
const requested = normalize(requestedPath);
|
|
6440
|
+
return context.files.find((file) => file === requested || relativePath(cwd, file) === requested) ?? null;
|
|
6408
6441
|
}
|
|
6409
6442
|
function stringValue(value) {
|
|
6410
6443
|
return typeof value === "string" && value.trim().length > 0 ? value : void 0;
|
|
@@ -6418,58 +6451,59 @@ async function handleStudioApiRequest(options, req, res) {
|
|
|
6418
6451
|
if (!req.url) return false;
|
|
6419
6452
|
const url = new URL(req.url, "http://contentbit.local");
|
|
6420
6453
|
if (!url.pathname.startsWith("/api/")) return false;
|
|
6454
|
+
const root = options.cwd ?? process.cwd();
|
|
6421
6455
|
try {
|
|
6422
6456
|
if (req.method !== "GET") {
|
|
6423
|
-
sendJson(res, 405, { error: "Studio API is read-only." });
|
|
6457
|
+
sendJson(res, root, 405, { error: "Studio API is read-only." });
|
|
6424
6458
|
return true;
|
|
6425
6459
|
}
|
|
6426
6460
|
if (url.pathname === "/api/project") {
|
|
6427
|
-
sendJson(res, 200, await scanProject(options));
|
|
6461
|
+
sendJson(res, root, 200, await scanProject(options));
|
|
6428
6462
|
return true;
|
|
6429
6463
|
}
|
|
6430
6464
|
if (url.pathname === "/api/site") {
|
|
6431
|
-
sendJson(res, 200, await scanSite(options));
|
|
6465
|
+
sendJson(res, root, 200, await scanSite(options));
|
|
6432
6466
|
return true;
|
|
6433
6467
|
}
|
|
6434
6468
|
if (url.pathname === "/api/page") {
|
|
6435
6469
|
const path = url.searchParams.get("path");
|
|
6436
6470
|
if (!path) {
|
|
6437
|
-
sendJson(res, 400, { error: "Missing ?path=" });
|
|
6471
|
+
sendJson(res, root, 400, { error: "Missing ?path=" });
|
|
6438
6472
|
return true;
|
|
6439
6473
|
}
|
|
6440
6474
|
const page = await scanPage(options, path);
|
|
6441
6475
|
if (!page) {
|
|
6442
|
-
sendJson(res, 404, { error: `The build has no page ${path}.` });
|
|
6476
|
+
sendJson(res, root, 404, { error: `The build has no page ${path}.` });
|
|
6443
6477
|
return true;
|
|
6444
6478
|
}
|
|
6445
|
-
sendJson(res, 200, page);
|
|
6479
|
+
sendJson(res, root, 200, page);
|
|
6446
6480
|
return true;
|
|
6447
6481
|
}
|
|
6448
6482
|
if (url.pathname === "/api/document") {
|
|
6449
6483
|
const path = url.searchParams.get("path");
|
|
6450
6484
|
if (!path) {
|
|
6451
|
-
sendJson(res, 400, { error: "Missing ?path=" });
|
|
6485
|
+
sendJson(res, root, 400, { error: "Missing ?path=" });
|
|
6452
6486
|
return true;
|
|
6453
6487
|
}
|
|
6454
6488
|
const document = await scanDocument(options, path);
|
|
6455
6489
|
if (!document) {
|
|
6456
|
-
sendJson(res, 404, { error: "Document is not in the configured content set." });
|
|
6490
|
+
sendJson(res, root, 404, { error: "Document is not in the configured content set." });
|
|
6457
6491
|
return true;
|
|
6458
6492
|
}
|
|
6459
|
-
sendJson(res, 200, document);
|
|
6493
|
+
sendJson(res, root, 200, document);
|
|
6460
6494
|
return true;
|
|
6461
6495
|
}
|
|
6462
|
-
sendJson(res, 404, { error: "Unknown Studio API route." });
|
|
6496
|
+
sendJson(res, root, 404, { error: "Unknown Studio API route." });
|
|
6463
6497
|
return true;
|
|
6464
6498
|
} catch (err) {
|
|
6465
|
-
sendJson(res, 500, { error: err instanceof Error ? err.message : String(err) });
|
|
6499
|
+
sendJson(res, root, 500, { error: err instanceof Error ? err.message : String(err) });
|
|
6466
6500
|
return true;
|
|
6467
6501
|
}
|
|
6468
6502
|
}
|
|
6469
|
-
function sendJson(res, status, body) {
|
|
6503
|
+
function sendJson(res, root, status, body) {
|
|
6470
6504
|
res.statusCode = status;
|
|
6471
6505
|
res.setHeader("content-type", "application/json; charset=utf-8");
|
|
6472
|
-
res.end(JSON.stringify(body,
|
|
6506
|
+
res.end(JSON.stringify(body, pathsFromRoot(root), 2));
|
|
6473
6507
|
}
|
|
6474
6508
|
|
|
6475
6509
|
// src/server/index.ts
|
|
@@ -6478,7 +6512,7 @@ async function startStudio(options) {
|
|
|
6478
6512
|
const host = options.host ?? "127.0.0.1";
|
|
6479
6513
|
const port = options.port === 0 ? await freePort(host) : options.port ?? 4377;
|
|
6480
6514
|
const cwd = options.cwd ?? process.cwd();
|
|
6481
|
-
const fontRoot =
|
|
6515
|
+
const fontRoot = dirname2(
|
|
6482
6516
|
createRequire(join5(root, "package.json")).resolve("@fontsource-variable/inter/package.json")
|
|
6483
6517
|
);
|
|
6484
6518
|
const fsAllow = [root, fontRoot];
|
|
@@ -6548,7 +6582,7 @@ function componentCandidates(options) {
|
|
|
6548
6582
|
const dirs = /* @__PURE__ */ new Set();
|
|
6549
6583
|
if (options.registryPath) {
|
|
6550
6584
|
const registryPath = isAbsolute3(options.registryPath) ? options.registryPath : resolvePath(options.cwd, options.registryPath);
|
|
6551
|
-
dirs.add(
|
|
6585
|
+
dirs.add(dirname2(registryPath));
|
|
6552
6586
|
}
|
|
6553
6587
|
dirs.add(join5(options.cwd, "blocks"));
|
|
6554
6588
|
const rendererCandidates = [
|
|
@@ -6599,10 +6633,10 @@ async function freePort(host) {
|
|
|
6599
6633
|
return port;
|
|
6600
6634
|
}
|
|
6601
6635
|
function findPackageRoot() {
|
|
6602
|
-
let dir =
|
|
6636
|
+
let dir = dirname2(fileURLToPath(import.meta.url));
|
|
6603
6637
|
for (; ; ) {
|
|
6604
6638
|
if (existsSync4(join5(dir, "package.json"))) return dir;
|
|
6605
|
-
const next =
|
|
6639
|
+
const next = dirname2(dir);
|
|
6606
6640
|
if (next === dir) throw new Error("Could not locate @contentbit/studio package root.");
|
|
6607
6641
|
dir = next;
|
|
6608
6642
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contentbit/studio",
|
|
3
|
-
"version": "2.0.0-next.
|
|
3
|
+
"version": "2.0.0-next.2",
|
|
4
4
|
"description": "Local read-only dashboard for contentbit: a site's built pages, page context, template findings, and Markdown.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"contentbit",
|
|
@@ -57,9 +57,9 @@
|
|
|
57
57
|
"tinyglobby": "^0.2.10",
|
|
58
58
|
"tw-animate-css": "^1.4.0",
|
|
59
59
|
"vite": "^8",
|
|
60
|
-
"@contentbit/blocks": "2.0.0-next.
|
|
61
|
-
"@contentbit/core": "2.0.0-next.
|
|
62
|
-
"@contentbit/react": "2.0.0-next.
|
|
60
|
+
"@contentbit/blocks": "2.0.0-next.2",
|
|
61
|
+
"@contentbit/core": "2.0.0-next.2",
|
|
62
|
+
"@contentbit/react": "2.0.0-next.2"
|
|
63
63
|
},
|
|
64
64
|
"devDependencies": {
|
|
65
65
|
"@testing-library/dom": "^10.4.2",
|
package/src/server/api.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { IncomingMessage, ServerResponse } from 'node:http'
|
|
2
|
+
import { pathsFromRoot } from '@contentbit/project'
|
|
2
3
|
|
|
3
4
|
import { scanDocument, scanProject } from './scan.js'
|
|
4
5
|
import { scanPage, scanSite } from './site.js'
|
|
@@ -12,63 +13,65 @@ export async function handleStudioApiRequest(
|
|
|
12
13
|
if (!req.url) return false
|
|
13
14
|
const url = new URL(req.url, 'http://contentbit.local')
|
|
14
15
|
if (!url.pathname.startsWith('/api/')) return false
|
|
16
|
+
const root = options.cwd ?? process.cwd()
|
|
15
17
|
|
|
16
18
|
try {
|
|
17
19
|
if (req.method !== 'GET') {
|
|
18
|
-
sendJson(res, 405, { error: 'Studio API is read-only.' })
|
|
20
|
+
sendJson(res, root, 405, { error: 'Studio API is read-only.' })
|
|
19
21
|
return true
|
|
20
22
|
}
|
|
21
23
|
|
|
22
24
|
if (url.pathname === '/api/project') {
|
|
23
|
-
sendJson(res, 200, await scanProject(options))
|
|
25
|
+
sendJson(res, root, 200, await scanProject(options))
|
|
24
26
|
return true
|
|
25
27
|
}
|
|
26
28
|
|
|
27
29
|
if (url.pathname === '/api/site') {
|
|
28
|
-
sendJson(res, 200, await scanSite(options))
|
|
30
|
+
sendJson(res, root, 200, await scanSite(options))
|
|
29
31
|
return true
|
|
30
32
|
}
|
|
31
33
|
|
|
32
34
|
if (url.pathname === '/api/page') {
|
|
33
35
|
const path = url.searchParams.get('path')
|
|
34
36
|
if (!path) {
|
|
35
|
-
sendJson(res, 400, { error: 'Missing ?path=' })
|
|
37
|
+
sendJson(res, root, 400, { error: 'Missing ?path=' })
|
|
36
38
|
return true
|
|
37
39
|
}
|
|
38
40
|
const page = await scanPage(options, path)
|
|
39
41
|
if (!page) {
|
|
40
|
-
sendJson(res, 404, { error: `The build has no page ${path}.` })
|
|
42
|
+
sendJson(res, root, 404, { error: `The build has no page ${path}.` })
|
|
41
43
|
return true
|
|
42
44
|
}
|
|
43
|
-
sendJson(res, 200, page)
|
|
45
|
+
sendJson(res, root, 200, page)
|
|
44
46
|
return true
|
|
45
47
|
}
|
|
46
48
|
|
|
47
49
|
if (url.pathname === '/api/document') {
|
|
48
50
|
const path = url.searchParams.get('path')
|
|
49
51
|
if (!path) {
|
|
50
|
-
sendJson(res, 400, { error: 'Missing ?path=' })
|
|
52
|
+
sendJson(res, root, 400, { error: 'Missing ?path=' })
|
|
51
53
|
return true
|
|
52
54
|
}
|
|
53
55
|
const document = await scanDocument(options, path)
|
|
54
56
|
if (!document) {
|
|
55
|
-
sendJson(res, 404, { error: 'Document is not in the configured content set.' })
|
|
57
|
+
sendJson(res, root, 404, { error: 'Document is not in the configured content set.' })
|
|
56
58
|
return true
|
|
57
59
|
}
|
|
58
|
-
sendJson(res, 200, document)
|
|
60
|
+
sendJson(res, root, 200, document)
|
|
59
61
|
return true
|
|
60
62
|
}
|
|
61
63
|
|
|
62
|
-
sendJson(res, 404, { error: 'Unknown Studio API route.' })
|
|
64
|
+
sendJson(res, root, 404, { error: 'Unknown Studio API route.' })
|
|
63
65
|
return true
|
|
64
66
|
} catch (err) {
|
|
65
|
-
sendJson(res, 500, { error: err instanceof Error ? err.message : String(err) })
|
|
67
|
+
sendJson(res, root, 500, { error: err instanceof Error ? err.message : String(err) })
|
|
66
68
|
return true
|
|
67
69
|
}
|
|
68
70
|
}
|
|
69
71
|
|
|
70
|
-
|
|
72
|
+
/** The UI shows and copies the paths a response holds, so each is from the project root. */
|
|
73
|
+
function sendJson(res: ServerResponse, root: string, status: number, body: unknown): void {
|
|
71
74
|
res.statusCode = status
|
|
72
75
|
res.setHeader('content-type', 'application/json; charset=utf-8')
|
|
73
|
-
res.end(JSON.stringify(body,
|
|
76
|
+
res.end(JSON.stringify(body, pathsFromRoot(root), 2))
|
|
74
77
|
}
|
package/src/server/scan.ts
CHANGED
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
type SeoPage,
|
|
7
7
|
} from '@contentbit/core'
|
|
8
8
|
import { relativeInside } from '@contentbit/project'
|
|
9
|
-
import { basename } from 'node:path'
|
|
9
|
+
import { basename, normalize } from 'node:path'
|
|
10
10
|
|
|
11
11
|
import { renderStudioPreview } from './preview.js'
|
|
12
12
|
import { loadSiteOrJoinInFlight, pagesByFile } from './site.js'
|
|
@@ -248,10 +248,10 @@ function statusFor(findings: StudioFileSummary['findings']): StudioStatus {
|
|
|
248
248
|
}
|
|
249
249
|
|
|
250
250
|
function findAllowedFile(context: ScanContext, cwd: string, requestedPath: string): string | null {
|
|
251
|
+
const requested = normalize(requestedPath)
|
|
251
252
|
return (
|
|
252
|
-
context.files.find(
|
|
253
|
-
|
|
254
|
-
) ?? null
|
|
253
|
+
context.files.find((file) => file === requested || relativePath(cwd, file) === requested) ??
|
|
254
|
+
null
|
|
255
255
|
)
|
|
256
256
|
}
|
|
257
257
|
|
package/src/server/site.test.ts
CHANGED
|
@@ -5,8 +5,8 @@ import { afterEach, assert, expect, test } from 'vitest'
|
|
|
5
5
|
|
|
6
6
|
import { startStudio, type StudioServer } from './index'
|
|
7
7
|
import { scanDocument, scanProject } from './scan'
|
|
8
|
-
import { scanPage, scanSite } from './site'
|
|
9
|
-
import type { StudioOptions } from './types'
|
|
8
|
+
import { scanPage, scanSite, type StudioPage, type StudioSite } from './site'
|
|
9
|
+
import type { StudioDocument, StudioOptions, StudioProject } from './types'
|
|
10
10
|
import { tempDir } from '../../../../test/temp-dir.js'
|
|
11
11
|
|
|
12
12
|
const runningServers: StudioServer[] = []
|
|
@@ -298,7 +298,7 @@ test('startStudio serves the site and page context endpoints from the build fold
|
|
|
298
298
|
expect(await site.json()).toMatchObject({
|
|
299
299
|
kind: 'built',
|
|
300
300
|
context: {
|
|
301
|
-
build:
|
|
301
|
+
build: 'web',
|
|
302
302
|
origin: { url: 'https://www.bakery.example', source: 'flag' },
|
|
303
303
|
pages: { length: 5 },
|
|
304
304
|
},
|
|
@@ -310,6 +310,35 @@ test('startStudio serves the site and page context endpoints from the build fold
|
|
|
310
310
|
expect(noPath.status).toBe(400)
|
|
311
311
|
})
|
|
312
312
|
|
|
313
|
+
test('every path in a Studio API response is from the project root, and a document opens by it', async () => {
|
|
314
|
+
const dir = await fixture(bakery)
|
|
315
|
+
const server = await startStudio({ ...options(dir), port: 0, open: false })
|
|
316
|
+
runningServers.push(server)
|
|
317
|
+
const get = async (path: string) => {
|
|
318
|
+
const response = await fetch(new URL(path, server.url))
|
|
319
|
+
expect(response.status, path).toBe(200)
|
|
320
|
+
return response.text()
|
|
321
|
+
}
|
|
322
|
+
const query = (route: string, path: string) => `${route}?path=${encodeURIComponent(path)}`
|
|
323
|
+
|
|
324
|
+
const project = await get('/api/project')
|
|
325
|
+
const site = await get('/api/site')
|
|
326
|
+
const built = JSON.parse(site) as Extract<StudioSite, { kind: 'built' }>
|
|
327
|
+
const pages = await Promise.all(
|
|
328
|
+
built.context.pages.map(({ path }) => get(query('/api/page', path))),
|
|
329
|
+
)
|
|
330
|
+
const files = (JSON.parse(project) as StudioProject).files.map(({ path }) => path)
|
|
331
|
+
const documents = await Promise.all(files.map((file) => get(query('/api/document', file))))
|
|
332
|
+
const sourdough = JSON.parse(await get(query('/api/page', '/blog/sourdough/'))) as StudioPage
|
|
333
|
+
const rye = JSON.parse(await get(query('/api/document', 'content/blog/rye.md'))) as StudioDocument
|
|
334
|
+
|
|
335
|
+
expect(sourdough.context.source?.file).toBe('content/blog/sourdough.md')
|
|
336
|
+
await get(query('/api/document', sourdough.context.source?.file ?? ''))
|
|
337
|
+
expect(rye.seoBrief?.target.path).toBe('content/blog/rye.md')
|
|
338
|
+
expect(rye.seoBrief?.relatedPages.map(({ path }) => path)).toEqual(['content/blog/spelt.md'])
|
|
339
|
+
for (const body of [project, site, ...pages, ...documents]) expect(body).not.toContain(dir)
|
|
340
|
+
})
|
|
341
|
+
|
|
313
342
|
test('a request whose URL Studio cannot parse gets an error, and Studio keeps serving', async () => {
|
|
314
343
|
const dir = await fixture(bakery)
|
|
315
344
|
const server = await startStudio({ ...options(dir), port: 0, open: false })
|