refactor: move spark table parsing to directive scanner
Relocate spark table logic from the block processor to a dedicated directive scanner. This allows spark tables to be treated as `:md-table` directives and ensures they are processed in a second pass after the content index is fully populated.
This commit is contained in:
@@ -7,7 +7,6 @@
|
||||
|
||||
import { posix } from "path";
|
||||
import { createHash } from "crypto";
|
||||
import Slugger from "github-slugger";
|
||||
import {
|
||||
parseStatYaml,
|
||||
parseStatCsv,
|
||||
@@ -20,16 +19,14 @@ import {
|
||||
FENCED_BLOCK_RE,
|
||||
parseBlockAttrs,
|
||||
resolveBlockAs,
|
||||
parseSparkTableCsv,
|
||||
} from "./block-scanner.js";
|
||||
import type { SparkTableCompletion, StatSheet } from "./types.js";
|
||||
import type { StatSheet } from "./types.js";
|
||||
|
||||
// Re-export shared pieces for convenience
|
||||
export {
|
||||
FENCED_BLOCK_RE,
|
||||
parseBlockAttrs,
|
||||
resolveBlockAs,
|
||||
parseSparkTableCsv,
|
||||
type BlockAttrs,
|
||||
} from "./block-scanner.js";
|
||||
|
||||
@@ -40,7 +37,6 @@ export {
|
||||
export interface ProcessedBlocks {
|
||||
stats: StatDef[];
|
||||
statTemplates: StatTemplate[];
|
||||
sparkTables: SparkTableCompletion[];
|
||||
statSheets: StatSheet[];
|
||||
}
|
||||
|
||||
@@ -68,7 +64,9 @@ function contentHash(body: string): string {
|
||||
*
|
||||
* - Strips/replaces blocks based on `as`
|
||||
* - Injects `role=file` bodies into the content index
|
||||
* - Collects stat/template/spark-table blocks for completions
|
||||
* - Collects stat/template blocks for completions
|
||||
* - role=spark-table blocks are converted to :md-table directives
|
||||
* (spark table completions are collected later by the directive scanner)
|
||||
*/
|
||||
export function processBlocks(
|
||||
content: string,
|
||||
@@ -76,12 +74,10 @@ export function processBlocks(
|
||||
index: Record<string, string>,
|
||||
): BlockResult {
|
||||
const fileDir = posix.dirname(fileRelativePath);
|
||||
const slugger = new Slugger();
|
||||
|
||||
const blocks: ProcessedBlocks = {
|
||||
stats: [],
|
||||
statTemplates: [],
|
||||
sparkTables: [],
|
||||
statSheets: [],
|
||||
};
|
||||
|
||||
@@ -122,11 +118,6 @@ export function processBlocks(
|
||||
blocks.statTemplates.push(result.template);
|
||||
}
|
||||
|
||||
if (attrs.role === "spark-table") {
|
||||
const parsed = parseSparkTableCsv(body, fileRelativePath, slugger);
|
||||
if (parsed) blocks.sparkTables.push(parsed);
|
||||
}
|
||||
|
||||
if (attrs.role === "file") {
|
||||
const filename = attrs.id
|
||||
? `${attrs.id}.${attrs.lang || "txt"}`
|
||||
|
||||
@@ -9,9 +9,6 @@
|
||||
* - `id` is used for cross-references (template names, file paths)
|
||||
*/
|
||||
|
||||
import Slugger from "github-slugger";
|
||||
import type { SparkTableCompletion } from "./types.js";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Types
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -58,46 +55,13 @@ export function parseBlockAttrs(info: string): BlockAttrs {
|
||||
*
|
||||
* Defaults:
|
||||
* - role is set, no explicit as → "none" (strip — it's metadata)
|
||||
* - role=spark-table → "md-table" (render as md-table directive)
|
||||
* - no role, no as → "codeblock" (keep as visible code block)
|
||||
*/
|
||||
export function resolveBlockAs(role: string | undefined, as: string | undefined): string {
|
||||
if (as) return as;
|
||||
// spark-table blocks render as md-table by default
|
||||
if (role === "spark-table") return "md-table";
|
||||
if (role) return "none";
|
||||
return "codeblock";
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Spark table parsing
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const DICE_RE = /^d\d+$/i;
|
||||
|
||||
export function parseSparkTableCsv(
|
||||
body: string,
|
||||
filePath: string,
|
||||
slugger: Slugger,
|
||||
): SparkTableCompletion | null {
|
||||
const lines = body.trim().split(/\r?\n/);
|
||||
if (lines.length < 2) return null;
|
||||
|
||||
const headers = lines[0].split(",").map((h) => h.trim());
|
||||
if (headers.length < 2) return null;
|
||||
if (!DICE_RE.test(headers[0])) return null;
|
||||
|
||||
const dataHeaders = headers.slice(1);
|
||||
const slug = dataHeaders
|
||||
.map((h) => slugger.slug(h.toLowerCase()))
|
||||
.join("-");
|
||||
|
||||
const basePath = filePath.replace(/\.md$/, "");
|
||||
const fileName = basePath.split("/").filter(Boolean).pop() || basePath;
|
||||
const combinedSlug = `${fileName}-${slug}`;
|
||||
|
||||
return {
|
||||
label: `${fileName} § ${slug}`,
|
||||
notation: headers[0],
|
||||
slug: combinedSlug,
|
||||
filePath: basePath,
|
||||
headers: dataHeaders,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,329 @@
|
||||
/**
|
||||
* Unified directive scanner — shared between CLI and browser.
|
||||
*
|
||||
* One pass over stripped markdown content that:
|
||||
* 1. Detects markdown tables that look like spark tables → coerces to
|
||||
* :md-table[./_inline_{hash}.csv] directives
|
||||
* 2. Scans :md-dice[...] directives → collects DiceCompletion
|
||||
* 3. Scans :md-table[...] directives → resolves CSV, checks if spark table
|
||||
* → collects SparkTableCompletion
|
||||
* 4. Scans :md-card[...] directives → same as md-table
|
||||
*
|
||||
* Safe for both Node and browser. No Node-specific imports.
|
||||
*/
|
||||
|
||||
import Slugger from "github-slugger";
|
||||
import type { DiceCompletion, SparkTableCompletion } from "./types.js";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Types
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export interface DirectiveScanResult {
|
||||
/** Rewritten content with markdown tables coerced to directives */
|
||||
rewritten: string;
|
||||
/** Dice completions discovered */
|
||||
dice: DiceCompletion[];
|
||||
/** Spark table completions discovered */
|
||||
sparkTables: SparkTableCompletion[];
|
||||
/** New index entries for inline CSV bodies (key → CSV content) */
|
||||
newIndexEntries: Record<string, string>;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const DICE_HEADER_RE = /^\d*d\d+$/i;
|
||||
|
||||
function contentHash(body: string): string {
|
||||
// Simple hash suitable for both Node and browser
|
||||
let hash = 0;
|
||||
for (let i = 0; i < body.length; i++) {
|
||||
const ch = body.charCodeAt(i);
|
||||
hash = ((hash << 5) - hash + ch) | 0;
|
||||
}
|
||||
return Math.abs(hash).toString(16).slice(0, 8);
|
||||
}
|
||||
|
||||
function looksLikeDice(raw: string): boolean {
|
||||
if (raw.length > 80) return false;
|
||||
return /^\d*d\d+/i.test(raw) || /^[+-]/.test(raw);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Markdown table → CSV conversion
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Split a markdown table row into cells.
|
||||
* Handles leading/trailing pipes and trims whitespace.
|
||||
*/
|
||||
function splitTableRow(row: string): string[] {
|
||||
return row
|
||||
.replace(/^\|/, "")
|
||||
.replace(/\|$/, "")
|
||||
.split("|")
|
||||
.map((c) => c.trim());
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape a cell value for CSV output.
|
||||
*/
|
||||
function escapeCsvCell(cell: string): string {
|
||||
if (
|
||||
cell.includes(",") ||
|
||||
cell.includes("\n") ||
|
||||
cell.includes('"') ||
|
||||
cell.includes("#")
|
||||
) {
|
||||
return `"${cell.replace(/"/g, '""')}"`;
|
||||
}
|
||||
return cell;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a markdown table (header + separator + rows) to a CSV string.
|
||||
*/
|
||||
function markdownTableToCsv(
|
||||
headerRow: string,
|
||||
separatorRow: string,
|
||||
bodyRows: string[],
|
||||
): string | null {
|
||||
const headers = splitTableRow(headerRow);
|
||||
if (headers.length === 0) return null;
|
||||
|
||||
// Validate separator row (must contain dashes)
|
||||
const sepCells = splitTableRow(separatorRow);
|
||||
if (!sepCells.every((c) => /^:?-{3,}:?$/.test(c))) return null;
|
||||
if (sepCells.length !== headers.length) return null;
|
||||
|
||||
const csvHeader = headers.map(escapeCsvCell).join(",");
|
||||
|
||||
const csvRows = bodyRows.map((row) => {
|
||||
const cells = splitTableRow(row);
|
||||
// Pad to match header length
|
||||
while (cells.length < headers.length) cells.push("");
|
||||
return cells.slice(0, headers.length).map(escapeCsvCell).join(",");
|
||||
});
|
||||
|
||||
return [csvHeader, ...csvRows].join("\n");
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Spark table CSV inspection
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Check if a CSV body represents a spark table.
|
||||
* Returns the data column headers (excluding the dice column) if so, or null.
|
||||
*/
|
||||
export function inspectSparkTableCsv(csv: string): string[] | null {
|
||||
const lines = csv.trim().split(/\r?\n/);
|
||||
if (lines.length < 2) return null;
|
||||
|
||||
const headers = lines[0].split(",").map((h) => h.trim());
|
||||
if (headers.length < 2) return null;
|
||||
if (!DICE_HEADER_RE.test(headers[0])) return null;
|
||||
|
||||
return headers.slice(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a SparkTableCompletion from CSV data and file path.
|
||||
*/
|
||||
export function buildSparkTableCompletion(
|
||||
csv: string,
|
||||
filePath: string,
|
||||
slugger: Slugger,
|
||||
): SparkTableCompletion | null {
|
||||
const dataHeaders = inspectSparkTableCsv(csv);
|
||||
if (!dataHeaders) return null;
|
||||
|
||||
const lines = csv.trim().split(/\r?\n/);
|
||||
const headers = lines[0].split(",").map((h) => h.trim());
|
||||
|
||||
const slug = dataHeaders
|
||||
.map((h) => slugger.slug(h.toLowerCase()))
|
||||
.join("-");
|
||||
|
||||
const basePath = filePath.replace(/\.md$/, "");
|
||||
const fileName = basePath.split("/").filter(Boolean).pop() || basePath;
|
||||
const combinedSlug = `${fileName}-${slug}`;
|
||||
|
||||
return {
|
||||
label: `${fileName} § ${slug}`,
|
||||
notation: headers[0],
|
||||
slug: combinedSlug,
|
||||
filePath: basePath,
|
||||
headers: dataHeaders,
|
||||
};
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Main scanner
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Scan a single markdown file's stripped content for directives and
|
||||
* spark-shaped markdown tables.
|
||||
*
|
||||
* @param content - Stripped markdown content (after block processing)
|
||||
* @param filePath - The file's path (e.g. "/rules/combat.md")
|
||||
* @param index - The content index for resolving CSV paths
|
||||
* @param fileDir - Directory of the file (for resolving relative paths)
|
||||
*/
|
||||
export function scanDirectives(
|
||||
content: string,
|
||||
filePath: string,
|
||||
index: Record<string, string>,
|
||||
fileDir: string,
|
||||
): DirectiveScanResult {
|
||||
const slugger = new Slugger();
|
||||
const dice: DiceCompletion[] = [];
|
||||
const sparkTables: SparkTableCompletion[] = [];
|
||||
const newIndexEntries: Record<string, string> = {};
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Pass 1: Coerce spark-shaped markdown tables to :md-table directives
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
const mdTableRegex =
|
||||
/^(\|.+\|)\n(\|[-: |]+\|)\n((?:\|.+\|\n?)+)/gm;
|
||||
|
||||
let rewritten = content;
|
||||
let mdMatch: RegExpExecArray | null;
|
||||
|
||||
// Collect matches first (rewriting while iterating is tricky with regex)
|
||||
interface TableMatch {
|
||||
fullMatch: string;
|
||||
headerRow: string;
|
||||
separatorRow: string;
|
||||
bodyRowsText: string;
|
||||
index: number;
|
||||
}
|
||||
const tableMatches: TableMatch[] = [];
|
||||
|
||||
while ((mdMatch = mdTableRegex.exec(content)) !== null) {
|
||||
const [, headerRow, separatorRow, bodyRowsText] = mdMatch;
|
||||
const headers = splitTableRow(headerRow);
|
||||
|
||||
// Check if this is a spark-shaped table: first column is a dice formula
|
||||
// and one of the headers is a label marker
|
||||
const isSpark =
|
||||
DICE_HEADER_RE.test(headers[0]) &&
|
||||
headers.some(
|
||||
(h) =>
|
||||
h === "label" ||
|
||||
h === "md-table-label" ||
|
||||
h === "md-roll-label",
|
||||
);
|
||||
|
||||
if (!isSpark) continue;
|
||||
|
||||
const bodyRows = bodyRowsText
|
||||
.trim()
|
||||
.split(/\n/)
|
||||
.filter((r) => r.trim().startsWith("|"));
|
||||
|
||||
const csv = markdownTableToCsv(headerRow, separatorRow, bodyRows);
|
||||
if (!csv) continue;
|
||||
|
||||
tableMatches.push({
|
||||
fullMatch: mdMatch[0],
|
||||
headerRow,
|
||||
separatorRow,
|
||||
bodyRowsText,
|
||||
index: mdMatch.index,
|
||||
});
|
||||
}
|
||||
|
||||
// Replace matches from end to start to preserve indices
|
||||
for (let i = tableMatches.length - 1; i >= 0; i--) {
|
||||
const m = tableMatches[i];
|
||||
const bodyRows = m.bodyRowsText
|
||||
.trim()
|
||||
.split(/\n/)
|
||||
.filter((r) => r.trim().startsWith("|"));
|
||||
const csv = markdownTableToCsv(m.headerRow, m.separatorRow, bodyRows)!;
|
||||
|
||||
const hash = contentHash(csv);
|
||||
const filename = `_spark_md_${hash}.csv`;
|
||||
const resolvedPath = `${fileDir}/${filename}`;
|
||||
|
||||
newIndexEntries[resolvedPath] = csv;
|
||||
|
||||
// Collect spark table completion
|
||||
const st = buildSparkTableCompletion(csv, filePath, slugger);
|
||||
if (st) {
|
||||
sparkTables.push(st);
|
||||
}
|
||||
|
||||
// Replace markdown table with :md-table directive
|
||||
const directive = `:md-table[./${filename}]{data-spark="${st?.slug ?? ""}"}`;
|
||||
rewritten =
|
||||
rewritten.slice(0, m.index) +
|
||||
directive +
|
||||
rewritten.slice(m.index + m.fullMatch.length);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Pass 2: Scan :md-dice[...] directives
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
const diceRegex = /:md-dice\[([^[\]]+)\]/gi;
|
||||
let diceMatch: RegExpExecArray | null;
|
||||
while ((diceMatch = diceRegex.exec(rewritten)) !== null) {
|
||||
const raw = diceMatch[1].trim();
|
||||
if (!raw || !looksLikeDice(raw)) continue;
|
||||
dice.push({ label: raw, notation: raw, source: filePath });
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Pass 3: Scan :md-table[...] and :md-card[...] directives
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
const tableDirectiveRegex = /:md-(table|card)\[([^[\]]+)\](?:\{([^}]*)\})?/gi;
|
||||
let tableMatch: RegExpExecArray | null;
|
||||
while ((tableMatch = tableDirectiveRegex.exec(rewritten)) !== null) {
|
||||
const [, /* type */ , path, extraStr] = tableMatch;
|
||||
|
||||
// Resolve the CSV path
|
||||
const csvPath = path.startsWith("./")
|
||||
? `${fileDir}/${path.slice(2)}`
|
||||
: path;
|
||||
|
||||
let csv = index[csvPath] ?? newIndexEntries[csvPath];
|
||||
if (!csv) continue;
|
||||
|
||||
const st = buildSparkTableCompletion(csv, filePath, slugger);
|
||||
if (!st) continue;
|
||||
|
||||
// Check if data-spark is already set in extra attrs
|
||||
if (!extraStr || !extraStr.includes("data-spark=")) {
|
||||
// Inject data-spark attribute into the directive
|
||||
const fullMatch = tableMatch[0];
|
||||
const insertPos = fullMatch.indexOf("]") + 1;
|
||||
const before = fullMatch.slice(0, insertPos);
|
||||
const after = fullMatch.slice(insertPos);
|
||||
|
||||
const sparkAttr = `{data-spark="${st.slug}"}`;
|
||||
let replacement: string;
|
||||
if (after.startsWith("{")) {
|
||||
// Merge into existing attrs
|
||||
replacement = before + after.replace(/^\{/, `{data-spark="${st.slug}" `);
|
||||
} else {
|
||||
replacement = before + sparkAttr + after;
|
||||
}
|
||||
|
||||
rewritten =
|
||||
rewritten.slice(0, tableMatch.index) +
|
||||
replacement +
|
||||
rewritten.slice(tableMatch.index + fullMatch.length);
|
||||
}
|
||||
|
||||
sparkTables.push(st);
|
||||
}
|
||||
|
||||
return { rewritten, dice, sparkTables, newIndexEntries };
|
||||
}
|
||||
@@ -2,10 +2,10 @@
|
||||
* Completion index — orchestrates all registered completion sources.
|
||||
*/
|
||||
|
||||
import { diceSource } from "./sources/dice.js";
|
||||
import { linksSource } from "./sources/links.js";
|
||||
import type { ProcessedBlocks } from "./block-processor.js";
|
||||
import type { CompletionsPayload } from "./types.js";
|
||||
import type { DirectiveScanResult } from "./directive-scanner.js";
|
||||
|
||||
export type {
|
||||
CompletionsPayload,
|
||||
@@ -18,20 +18,29 @@ export type {
|
||||
} from "./types.js";
|
||||
|
||||
/**
|
||||
* Build completions from the content index and pre-collected blocks.
|
||||
* Build completions from the content index, pre-collected blocks,
|
||||
* and directive scan results.
|
||||
* Called at server startup and on any file change.
|
||||
*/
|
||||
export function scanCompletions(
|
||||
index: Record<string, string>,
|
||||
blocks: ProcessedBlocks,
|
||||
directiveResults: DirectiveScanResult[],
|
||||
): CompletionsPayload {
|
||||
const dice = diceSource.scan(index) as CompletionsPayload["dice"];
|
||||
const links = linksSource.scan(index) as CompletionsPayload["links"];
|
||||
|
||||
// Merge all directive scan results
|
||||
const dice: CompletionsPayload["dice"] = [];
|
||||
const sparkTables: CompletionsPayload["sparkTables"] = [];
|
||||
for (const dr of directiveResults) {
|
||||
dice.push(...dr.dice);
|
||||
sparkTables.push(...dr.sparkTables);
|
||||
}
|
||||
|
||||
return {
|
||||
dice,
|
||||
links,
|
||||
sparkTables: blocks.sparkTables,
|
||||
sparkTables,
|
||||
stats: blocks.stats,
|
||||
statTemplates: blocks.statTemplates,
|
||||
statSheets: blocks.statSheets,
|
||||
|
||||
Reference in New Issue
Block a user