refactor: use CSV path instead of markdown file for spark tables

Switch the spark table resolution logic to use the direct path to the
backing CSV file rather than attempting to parse the containing
markdown file. This simplifies the lookup process and improves
reliability.

Also refactor `SparkTableMeta` to store rows as objects keyed by
header instead of raw string arrays.
This commit is contained in:
2026-07-11 11:45:38 +08:00
parent b8cfb05e53
commit 719bb6ad8a
6 changed files with 65 additions and 34 deletions
+2 -2
View File
@@ -127,8 +127,8 @@ export const JournalInput: Component = () => {
try {
const key = (parsed.payload as { key: string }).key;
const match = comp.data.sparkTables.find((s) => s.slug === key);
const filePath = match?.filePath ?? "";
const p = await resolveSparkPayload({ key, filePath });
const csvPath = match?.csvPath ?? "";
const p = await resolveSparkPayload({ key, csvPath });
const result = sendMessage("spark", p);
const r = unwrap(result);
finish(r.ok, r.err);
+1 -2
View File
@@ -29,8 +29,6 @@ import {
} from "../../cli/completions/block-scanner";
import {
scanDirectives,
buildSparkTableCompletion,
inspectSparkTableCsv,
} from "../../cli/completions/directive-scanner";
export type { StatDef, StatTemplate, StatSheet };
@@ -54,6 +52,7 @@ export interface SparkTableCompletion {
notation: string;
slug: string;
filePath: string;
csvPath: string;
headers: string[];
}
+8 -19
View File
@@ -17,9 +17,8 @@ import { For } from "solid-js";
import { registerMessageType } from "../registry";
import { rollFormula } from "../../md-commander/hooks";
import {
findSparkTableByCombinedSlug,
parseSparkTableCsv,
rollSparkTable,
scanSparkTables,
} from "../../utils/spark-table";
import { getIndexedData } from "../../../data-loader/file-index";
@@ -70,33 +69,23 @@ export type SparkPayload = z.infer<typeof schema>;
* Resolve a spark table roll.
*
* `key` is the combined slug (pageName-columnSlug).
* `filePath` is the .md file path (without .md extension) from completions.
* `csvPath` is the resolved .csv file path from completions.
*/
export async function resolveSparkPayload(raw: {
key: string;
filePath: string;
csvPath: string;
}): Promise<SparkPayload> {
const mdPath = `/${raw.filePath.replace(/^\//, "")}.md`;
const pageName =
raw.filePath.replace(/^\//, "").split("/").filter(Boolean).pop() ||
raw.filePath;
let content: string;
let csv: string;
try {
content = await getIndexedData(mdPath);
csv = await getIndexedData(raw.csvPath);
} catch {
throw new Error(`Failed to load file: "${mdPath}"`);
throw new Error(`Failed to load CSV: "${raw.csvPath}"`);
}
const meta = findSparkTableByCombinedSlug(content, raw.key, pageName);
const meta = parseSparkTableCsv(csv);
if (!meta) {
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}`,
`Spark table "${raw.key}" not found in CSV "${raw.csvPath}"`,
);
}