refactor: improve spark table lookup and error reporting
Replace the heuristic-based slug parsing with a more robust combined-slug lookup to correctly handle page names containing hyphens. Added `scanSparkTables` to provide better error messages when a table is not found.
This commit is contained in:
@@ -16,7 +16,11 @@ import { z } from "zod";
|
||||
import { For } from "solid-js";
|
||||
import { registerMessageType } from "../registry";
|
||||
import { rollFormula } from "../../md-commander/hooks";
|
||||
import { findSparkTable, rollSparkTable } from "../../utils/spark-table";
|
||||
import {
|
||||
findSparkTableByCombinedSlug,
|
||||
rollSparkTable,
|
||||
scanSparkTables,
|
||||
} from "../../utils/spark-table";
|
||||
import { getIndexedData } from "../../../data-loader/file-index";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -72,28 +76,28 @@ export async function resolveSparkPayload(raw: {
|
||||
key: string;
|
||||
filePath: string;
|
||||
}): Promise<SparkPayload> {
|
||||
// key is "pageName-columnSlug". The page name is the last segment of
|
||||
// filePath (e.g. "01-dry-dock" from "/ayi-games/.../01-dry-dock").
|
||||
// Strip it from the key to recover the column slug.
|
||||
const pathParts = raw.filePath.replace(/^\//, "").split("/");
|
||||
const pageName = pathParts[pathParts.length - 1] || raw.filePath;
|
||||
const columnSlug =
|
||||
raw.key.length > pageName.length + 1 && raw.key.startsWith(pageName + "-")
|
||||
? raw.key.slice(pageName.length + 1)
|
||||
: raw.key;
|
||||
|
||||
const filePath = `/${raw.filePath.replace(/^\//, "")}`;
|
||||
const mdPath = `/${raw.filePath.replace(/^\//, "")}.md`;
|
||||
const pageName =
|
||||
raw.filePath.replace(/^\//, "").split("/").filter(Boolean).pop() ||
|
||||
raw.filePath;
|
||||
|
||||
let content: string;
|
||||
try {
|
||||
content = await getIndexedData(filePath);
|
||||
content = await getIndexedData(mdPath);
|
||||
} catch {
|
||||
throw new Error(`Failed to load file: "${filePath}"`);
|
||||
throw new Error(`Failed to load file: "${mdPath}"`);
|
||||
}
|
||||
|
||||
const meta = findSparkTable(content, columnSlug);
|
||||
const meta = findSparkTableByCombinedSlug(content, raw.key, pageName);
|
||||
if (!meta) {
|
||||
throw new Error(`Spark table "${columnSlug}" not found in "${filePath}"`);
|
||||
const available = scanSparkTables(content);
|
||||
const names =
|
||||
available.length > 0
|
||||
? available.map((t) => `${pageName}-${t.slug}`).join(", ")
|
||||
: "(none)";
|
||||
throw new Error(
|
||||
`Spark table "${raw.key}" not found in "${mdPath}". Available: ${names}`,
|
||||
);
|
||||
}
|
||||
|
||||
const sparkResult = rollSparkTable(meta);
|
||||
|
||||
Reference in New Issue
Block a user