Skip to content

Commit a74f14f

Browse files
committed
improve(splash): improve splash with frontmatter block scalar syntax
1 parent b29e74d commit a74f14f

3 files changed

Lines changed: 57 additions & 8 deletions

File tree

splash/src/loaders/frontmatter.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,44 @@
1717
* defeat the loader's "respect what's there" stance.
1818
*/
1919

20+
/**
21+
* YAML block scalar header: `|`, `>`, with optional chomping (`-`/`+`) and an
22+
* optional explicit indent digit — e.g. `>-`, `|2`, `>2-`.
23+
*/
24+
const BLOCK_SCALAR_RE = /^([|>])([0-9]*)([-+]?)$/;
25+
26+
/**
27+
* Read an indented text block beneath a block-scalar header and fold it per the
28+
* YAML rules we actually use.
29+
*
30+
* `|` literal — newlines preserved
31+
* `>` folded — lines joined with a single space
32+
* `-` strip — no trailing newline (what every lede/summary in this tree uses)
33+
*
34+
* Returns null when there is no indented body, so the caller can fall through.
35+
*/
36+
function readIndentedBlock(
37+
lines: string[],
38+
start: number,
39+
style: string,
40+
chomp: string,
41+
): { text: string | null; consumed: number } {
42+
const collected: string[] = [];
43+
let i = start;
44+
while (i < lines.length) {
45+
const line = lines[i];
46+
if (line.trim() === '') { collected.push(''); i++; continue; }
47+
if (!/^[ \t]/.test(line)) break; // dedent ends the block
48+
collected.push(line.replace(/^[ \t]+/, ''));
49+
i++;
50+
}
51+
while (collected.length && collected[collected.length - 1] === '') collected.pop();
52+
if (collected.length === 0) return { text: null, consumed: 0 };
53+
let text = style === '|' ? collected.join('\n') : collected.join(' ').replace(/\s+/g, ' ').trim();
54+
if (chomp === '+') text += '\n';
55+
return { text, consumed: i - start };
56+
}
57+
2058
const FENCE_RE = /^---\s*\r?\n([\s\S]*?)\r?\n---\s*\r?\n?([\s\S]*)$/;
2159

2260
export interface ParsedFrontmatter {
@@ -47,13 +85,24 @@ function parseYamlSubset(text: string): Record<string, unknown> {
4785
const key = line.slice(0, colonIdx).trim();
4886
const rest = line.slice(colonIdx + 1).trim();
4987

50-
if (rest === '' || rest === '|' || rest === '>') {
88+
if (rest === '' || BLOCK_SCALAR_RE.test(rest)) {
5189
const { items, consumed } = readIndentedArray(lines, i + 1);
5290
if (items !== null) {
5391
data[key] = items;
5492
i += 1 + consumed;
5593
continue;
5694
}
95+
// Not a list. If a block-scalar header was given, the indented body is
96+
// text — fold it rather than dropping the value on the floor.
97+
const bs = rest.match(BLOCK_SCALAR_RE);
98+
if (bs) {
99+
const { text, consumed: used } = readIndentedBlock(lines, i + 1, bs[1], bs[3]);
100+
if (text !== null) {
101+
data[key] = text;
102+
i += 1 + used;
103+
continue;
104+
}
105+
}
57106
data[key] = null;
58107
i++;
59108
continue;

splash/src/pages/context-v/[...slug].astro

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ export async function getStaticPaths() {
1616
const { entry } = Astro.props;
1717
const { Content } = await render(entry);
1818
19-
const d = toDate(entry.data.date_modified ?? entry.data.date_updated ?? entry.data.date_created);
20-
const created = toDate(entry.data.date_created);
21-
const modified = toDate(entry.data.date_modified ?? entry.data.date_updated);
19+
const d = toDate(entry.data.date_modified ?? entry.data.date_updated ?? entry.data.date_created ?? entry.data.date_authored_current_draft ?? entry.data.date_authored_initial_draft ?? entry.data.date_authored_current_draft ?? entry.data.date_authored_initial_draft ?? entry.data.date_authored_current_draft ?? entry.data.date_authored_initial_draft);
20+
const created = toDate(entry.data.date_created ?? entry.data.date_authored_initial_draft);
21+
const modified = toDate(entry.data.date_modified ?? entry.data.date_updated ?? entry.data.date_authored_current_draft);
2222
2323
const segments = entry.id.split('/');
2424
const group = segments.length > 1 ? segments[0] : 'general';

splash/src/pages/context-v/index.astro

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ for (const entry of entries) {
1818
1919
for (const arr of groups.values()) {
2020
arr.sort((a, b) => {
21-
const am = toDate(a.data.date_modified ?? a.data.date_updated ?? a.data.date_created);
22-
const bm = toDate(b.data.date_modified ?? b.data.date_updated ?? b.data.date_created);
21+
const am = toDate(a.data.date_modified ?? a.data.date_updated ?? a.data.date_created ?? a.data.date_authored_current_draft ?? a.data.date_authored_initial_draft ?? a.data.date_authored_current_draft ?? a.data.date_authored_initial_draft ?? a.data.date_authored_current_draft ?? a.data.date_authored_initial_draft);
22+
const bm = toDate(b.data.date_modified ?? b.data.date_updated ?? b.data.date_created ?? b.data.date_authored_current_draft ?? b.data.date_authored_initial_draft ?? b.data.date_authored_current_draft ?? b.data.date_authored_initial_draft ?? b.data.date_authored_current_draft ?? b.data.date_authored_initial_draft);
2323
const ta = am ? am.getTime() : 0;
2424
const tb = bm ? bm.getTime() : 0;
2525
if (tb !== ta) return tb - ta;
@@ -80,8 +80,8 @@ const base = import.meta.env.BASE_URL;
8080
<h2 class="ctx-group__title">{group}</h2>
8181
<ul class="entry-list" data-pagefind-ignore="all" data-sort-target="context-v">
8282
{items.map((entry) => {
83-
const created = toDate(entry.data.date_created);
84-
const modified = toDate(entry.data.date_modified ?? entry.data.date_updated);
83+
const created = toDate(entry.data.date_created ?? entry.data.date_authored_initial_draft);
84+
const modified = toDate(entry.data.date_modified ?? entry.data.date_updated ?? entry.data.date_authored_current_draft);
8585
const published = toDate(entry.data.date_first_published);
8686
const d = modified ?? created;
8787
const title = (entry.data.title ?? entry.id).toString().toLowerCase();

0 commit comments

Comments
 (0)