refactor: remove as= override and render data-spark at runtime
- Drop resolveBlockAs and the as= attribute; scanDoc now switches directly on role and warns on unknown roles instead of silently stripping - md-table derives data-spark from its loaded CSV via parseSparkTableCsv; delete injectSparkDirectives and the second registry pass so pathIndex is exactly scanDoc output - Align spark-table dice header regex with isSparkTableHeader
This commit is contained in:
@@ -2,11 +2,15 @@
|
||||
* Shared block scanning utilities — safe for both CLI and browser.
|
||||
*
|
||||
* Parses fenced code blocks with attributes:
|
||||
* ```lang id=xxx role=xxx as=xxx
|
||||
* ```lang id=xxx role=xxx key=value
|
||||
*
|
||||
* - `role` dispatches to content scanners (declare, spark-table)
|
||||
* - `as` controls rendering (none, codeblock, md-table, md-card, md-dice)
|
||||
* - `id` is used for cross-references (file paths)
|
||||
* The `role` fully determines block behavior (see `scanDoc` in
|
||||
* `content-registry.ts`):
|
||||
* - spark-table → stored as CSV, rendered as an `:md-table` directive
|
||||
* - declare / file → stored, block stripped
|
||||
* - tag → block kept intact for the yaml-tag render extension
|
||||
* - unknown role → warned about and stripped
|
||||
* - no role → kept as a visible code block
|
||||
*
|
||||
* Attribute parsing itself lives in `src/markdown/block-attrs.ts` so render
|
||||
* extensions can share the same syntax.
|
||||
@@ -19,28 +23,4 @@ export { parseBlockAttrs, type BlockAttrs } from "../../markdown/block-attrs.js"
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** Matches fenced code blocks with an info string (at least one attr). */
|
||||
export const FENCED_BLOCK_RE = /^```(\w*)\s+(\S.*?)\s*\n([\s\S]*?)```\s*$/gm;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// as resolution
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Determine the effective `as` value.
|
||||
*
|
||||
* Defaults:
|
||||
* - role is set, no explicit as → "none" (strip — it's metadata)
|
||||
* - role=spark-table → "md-table" (render as md-table directive)
|
||||
* - role=tag → "codeblock" (kept for the marked yaml-tag extension)
|
||||
* - 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";
|
||||
// yaml-defined tag blocks must survive stripping so the
|
||||
// code-block-yaml-tag marked extension can render them
|
||||
if (role === "tag") return "codeblock";
|
||||
if (role) return "none";
|
||||
return "codeblock";
|
||||
}
|
||||
export const FENCED_BLOCK_RE = /^```(\w*)\s+(\S.*?)\s*\n([\s\S]*?)```\s*$/gm;
|
||||
@@ -28,7 +28,7 @@ jest.mock("github-slugger", () => {
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
import { parseDeclareCsv } from "./declare-parser";
|
||||
import { parseBlockAttrs, resolveBlockAs } from "./block-scanner";
|
||||
import { parseBlockAttrs } from "./block-scanner";
|
||||
import { evaluateExpression, exprValueToString } from "../../components/journal/variable-expression";
|
||||
import {
|
||||
initReactivity,
|
||||
@@ -220,10 +220,9 @@ describe("parseBlockAttrs", () => {
|
||||
// parseBlockAttrs receives the info string AFTER the lang.
|
||||
// The lang is extracted from the fenced block regex capture group
|
||||
// and applied separately in content-registry.
|
||||
const attrs = parseBlockAttrs("id=stats role=declare as=none");
|
||||
const attrs = parseBlockAttrs("id=stats role=declare");
|
||||
expect(attrs.id).toBe("stats");
|
||||
expect(attrs.role).toBe("declare");
|
||||
expect(attrs.as).toBe("none");
|
||||
});
|
||||
|
||||
test("parses quoted values", () => {
|
||||
@@ -242,7 +241,6 @@ describe("parseBlockAttrs", () => {
|
||||
expect(attrs.lang).toBe("");
|
||||
expect(attrs.id).toBeUndefined();
|
||||
expect(attrs.role).toBeUndefined();
|
||||
expect(attrs.as).toBeUndefined();
|
||||
});
|
||||
|
||||
test("handles empty info string (lang extracted separately)", () => {
|
||||
@@ -252,28 +250,8 @@ describe("parseBlockAttrs", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveBlockAs", () => {
|
||||
test("returns explicit as value", () => {
|
||||
expect(resolveBlockAs("declare", "codeblock")).toBe("codeblock");
|
||||
});
|
||||
|
||||
test("defaults to none when role is set", () => {
|
||||
expect(resolveBlockAs("declare", undefined)).toBe("none");
|
||||
expect(resolveBlockAs("file", undefined)).toBe("none");
|
||||
});
|
||||
|
||||
test("defaults to md-table for spark-table role", () => {
|
||||
expect(resolveBlockAs("spark-table", undefined)).toBe("md-table");
|
||||
});
|
||||
|
||||
test("defaults to codeblock for tag role", () => {
|
||||
expect(resolveBlockAs("tag", undefined)).toBe("codeblock");
|
||||
});
|
||||
|
||||
test("defaults to codeblock when no role and no as", () => {
|
||||
expect(resolveBlockAs(undefined, undefined)).toBe("codeblock");
|
||||
});
|
||||
});
|
||||
// Role behavior is covered by the scanDoc tests in
|
||||
// src/cli/content-registry.test.ts.
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// variable-expression
|
||||
|
||||
Reference in New Issue
Block a user