// Heading furniture for the hand-authored sheets. // // Many sheets open their headings with an emoji (`## 📖 How It Works`, // `### 🔴 Rubeus — Windows`). On a mono, ruled page they read as stickers, and // they repeat down the contents rail. This pass removes them from the rendered // heading and replaces what they were doing with the site's own marks: // // - every `h2` gets `data-n` ("01", "02", …), drawn as a section number — // unless the sheet already numbers its own sections; // - a heading that opened with a coloured dot gets `data-mark`, drawn as a // small square in that hue, so "blue is enumerate, red is exploit" survives. // // The id is assigned *first*, from the untouched text, exactly as Astro would // have assigned it. Sheets link to these anchors (`[Overview](#-overview)`), // and so do bookmarks, so the emoji leaves the page but not the URL. Astro's // own heading pass runs after user plugins and keeps an id that is already a // string, which also means the contents rail is built from the cleaned text. // // Only `src/content/sheets/` is touched. The mirrors are other people's pages // and stay as their authors wrote them. import Slugger from 'github-slugger'; const RAW_NODE_TYPES = new Set(['text', 'raw', 'mdxTextExpression']); const CODE_TAG_NAMES = new Set(['code', 'pre']); /** * A heading's text, collected the way @astrojs/markdown-remark collects it, * so a slugger fed this produces the id Astro would. * @param {any} heading hast element * @param {boolean} [isMdx] * @returns {string} */ export function headingText(heading, isMdx = false) { let text = ''; const walk = (node, parent) => { if (node.type !== 'element' && parent !== null && RAW_NODE_TYPES.has(node.type)) { if (!(node.type === 'raw' && /^\n?<.*>\n?$/.test(node.value))) { text += isMdx || CODE_TAG_NAMES.has(parent.tagName) ? node.value : node.value.replace(/\{/g, '${'); } } if (node.children) for (const child of node.children) walk(child, node); }; walk(heading, null); return text; } // Pictographs, plus the joiners and selectors that ride along with them // (`⚙️` is U+2699 U+FE0F), plus the whitespace between and after. const LEADING_EMOJI = /^(?:\s*(?:\p{Extended_Pictographic}|\p{Regional_Indicator}|[️‍⃣]))+\s*/u; /** Coloured dots that mean something, and the palette hue that keeps it. */ const MARKS = { '🔴': 'love', '🔵': 'foam', '🔷': 'foam', '🟢': 'pine', '🟡': 'gold', '🟠': 'rose', '🟣': 'iris', }; /** * Whether a sheet numbers its own sections (`1. Setup`, `Phase 2 — …`). Such * a sheet gets no generated numbers: "03 2. Choose a mode" helps nobody. * Shared with the contents rail so heading and rail always agree. * @param {string[]} titles the sheet's h2 texts, emoji already removed */ export function isSelfNumbered(titles) { return titles.some((t) => /^\s*(?:\d+(?:\s*\+\s*\d+)*\s*[.):—-]|\d+\s|(?:step|stage|phase|part)\s+\d+)/i.test(t)); } const isHeading = (node) => node.type === 'element' && /^h[1-6]$/.test(node.tagName); /** First text node of a heading, if the heading opens with text at all. */ function leadText(heading) { const first = heading.children?.[0]; return first && first.type === 'text' ? first : null; } export function rehypeHeadingMarks() { return (tree, file) => { const source = (file?.history?.[0] ?? '').replace(/\\/g, '/'); if (!source.includes('/src/content/sheets/')) return; const isMdx = source.endsWith('.mdx'); const headings = []; const collect = (node) => { if (isHeading(node)) { headings.push(node); return; } // headings do not nest if (node.children) for (const child of node.children) collect(child); }; collect(tree); const slugger = new Slugger(); for (const node of headings) { node.properties = node.properties || {}; // Fix the anchor before anything is removed from the text. if (typeof node.properties.id !== 'string') node.properties.id = slugger.slug(headingText(node, isMdx)); const lead = leadText(node); const match = lead && LEADING_EMOJI.exec(lead.value); if (!match) continue; // A heading that is *only* emoji keeps it: an empty heading is worse. if (!headingText(node, isMdx).replace(LEADING_EMOJI, '').trim()) continue; const mark = MARKS[[...match[0].trim()][0]]; if (mark) node.properties.dataMark = mark; lead.value = lead.value.slice(match[0].length); } const sections = headings.filter((h) => h.tagName === 'h2'); if (isSelfNumbered(sections.map((h) => headingText(h, isMdx)))) return; sections.forEach((h, i) => { h.properties.dataN = String(i + 1).padStart(2, '0'); }); }; }