refactor: improve column slug extraction logic

This commit is contained in:
hypercross 2026-07-07 19:05:47 +08:00
parent 2f29f8774d
commit 97148aa010
1 changed files with 12 additions and 11 deletions

View File

@ -13,16 +13,10 @@
*/
import { z } from "zod";
import { Show, For } from "solid-js";
import { For } from "solid-js";
import { registerMessageType } from "../registry";
import { rollFormula } from "../../md-commander/hooks";
import {
findSparkTable,
rollSparkTable,
parseMarkdownTables,
isSparkTable,
sparkTableSlug,
} from "../../utils/spark-table";
import { findSparkTable, rollSparkTable } from "../../utils/spark-table";
import { getIndexedData } from "../../../data-loader/file-index";
// ---------------------------------------------------------------------------
@ -78,9 +72,16 @@ export async function resolveSparkPayload(raw: {
key: string;
filePath: string;
}): Promise<SparkPayload> {
// key is "pageName-columnSlug"; columnSlug is the part after the first "-"
const dashIdx = raw.key.indexOf("-");
const columnSlug = dashIdx === -1 ? raw.key : raw.key.slice(dashIdx + 1);
// 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(/^\//, "")}`;
let content: string;