|
| 1 | +/** |
| 2 | + * @module Infrastructure/RenderLib/ArticleBriefLead |
| 3 | + * @category Intelligence Operations / Supporting Infrastructure |
| 4 | + * @name Localized executive-brief lead substitution + carrier stripping |
| 5 | + * |
| 6 | + * @description |
| 7 | + * The aggregated `analysis/daily/$DATE/$SUB/article.md` is an |
| 8 | + * English-canonical document. It opens with the English executive brief |
| 9 | + * (the `## What Happened` lead section) and — because the aggregator |
| 10 | + * splices *every* `.md` sibling into the body — also embeds the 13 |
| 11 | + * localized briefs (`executive-brief_<lang>.md`) as trailing |
| 12 | + * `## Executive Brief Sv`, `## Executive Brief De`, … carrier sections. |
| 13 | + * |
| 14 | + * Those carrier sections were never meant to render inline: they bloat |
| 15 | + * every published page (each carries the full brief in a foreign language) |
| 16 | + * and they leave a non-English reader meeting the *English* lead before |
| 17 | + * their own-language summary. The SEO cascade already localizes the |
| 18 | + * `<title>` / `<meta description>` from `executive-brief_<lang>.md` |
| 19 | + * (see `aggregator/seo/localized-brief.ts`); this module brings the |
| 20 | + * on-page **lead** into lock-step with that cascade. |
| 21 | + * |
| 22 | + * {@link localizeExecutiveBriefLead} is a pure (no-I/O) string transform |
| 23 | + * applied by `renderArticleHtml` to the article-markdown body per target |
| 24 | + * language. It: |
| 25 | + * |
| 26 | + * 1. removes every embedded `## Executive Brief <Lang>` carrier section |
| 27 | + * for **all** languages (English included); and |
| 28 | + * 2. for a non-English target with a localized brief, replaces the body |
| 29 | + * of the first `<h2>` lead section (`## What Happened`) with the |
| 30 | + * cleaned `executive-brief_<lang>.md` content so the reader's first |
| 31 | + * screen is entirely in their own language. When the localized brief |
| 32 | + * is absent, the English lead is left in place (the same "localized |
| 33 | + * if exists, English otherwise" rule the SEO cascade follows). |
| 34 | + * |
| 35 | + * The localized body is cleaned with the **same** pipeline the aggregator |
| 36 | + * uses for the carrier sections — `cleanArtifactBody` (front-matter / H1 / |
| 37 | + * admin-byline strip + `##` → `###` heading demotion) followed by |
| 38 | + * `rewriteRelativeLinks` — so the swapped-in lead is byte-identical to |
| 39 | + * what the aggregator would have embedded. Crucially it does **not** run |
| 40 | + * `normalizeNarrativeTerminology`, whose English first-use annotations |
| 41 | + * (`Riksdag document #… (HD…)`, `Lede`, confidence glosses) must never be |
| 42 | + * injected into localized prose. |
| 43 | + * |
| 44 | + * @author Hack23 AB (Infrastructure Team) |
| 45 | + * @license Apache-2.0 |
| 46 | + */ |
| 47 | + |
| 48 | +import type { Language } from '../types/language.js'; |
| 49 | +import { LANGUAGES } from './constants.js'; |
| 50 | +import { buildGithubBlobUrl } from './url-helpers.js'; |
| 51 | +import { |
| 52 | + cleanArtifactBody, |
| 53 | + rewriteRelativeLinks, |
| 54 | +} from './aggregator/cleaning/structural.js'; |
| 55 | + |
| 56 | +/** |
| 57 | + * Title-cased single-segment language codes for the 13 non-English |
| 58 | + * locales, matching `prettifyFallbackTitle('executive-brief_<lang>.md')` |
| 59 | + * in `aggregator/order.ts` (e.g. `sv` → `Sv`, `no` → `No`, `zh` → `Zh`). |
| 60 | + * English is excluded — its brief renders as `## What Happened`, never as |
| 61 | + * a `## Executive Brief <Lang>` carrier. |
| 62 | + */ |
| 63 | +const LOCALIZED_BRIEF_TITLE_SUFFIXES: readonly string[] = LANGUAGES |
| 64 | + .filter((l) => l !== 'en') |
| 65 | + .map((l) => l.charAt(0).toUpperCase() + l.slice(1)); |
| 66 | + |
| 67 | +/** |
| 68 | + * Matches an embedded `## Executive Brief <Lang>` carrier section: the |
| 69 | + * heading line through every following line up to (but excluding) the |
| 70 | + * next `<h2>`. Mirrors the line-anchored sweep used by |
| 71 | + * `stripBodyDuplicateSections` so `###`/`# `/code-fence lines inside the |
| 72 | + * section are consumed while the next `## ` boundary stops the match. |
| 73 | + */ |
| 74 | +const EMBEDDED_BRIEF_SECTION_RE = new RegExp( |
| 75 | + String.raw`^##\s+Executive Brief (?:${LOCALIZED_BRIEF_TITLE_SUFFIXES.join('|')})\b[^\n]*\n(?:(?!^##\s)[^\n]*\n?)*`, |
| 76 | + 'gim', |
| 77 | +); |
| 78 | + |
| 79 | +/** |
| 80 | + * Strip all embedded `## Executive Brief <Lang>` carrier sections from an |
| 81 | + * article-markdown body. Applied for every language, English included. |
| 82 | + */ |
| 83 | +export function stripEmbeddedLocalizedBriefSections(content: string): string { |
| 84 | + const stripped = content.replace(EMBEDDED_BRIEF_SECTION_RE, ''); |
| 85 | + // Collapse the blank-line run left where the carrier block used to sit. |
| 86 | + return `${stripped.replace(/\n{3,}/g, '\n\n').trimEnd()}\n`; |
| 87 | +} |
| 88 | + |
| 89 | +interface LeadBounds { |
| 90 | + readonly headingLine: string; |
| 91 | + readonly firstH2: number; |
| 92 | + readonly secondH2: number; |
| 93 | +} |
| 94 | + |
| 95 | +/** Locate the first and second `## ` (h2) line indices in a markdown body. */ |
| 96 | +function findLeadBounds(lines: readonly string[]): LeadBounds | null { |
| 97 | + let firstH2 = -1; |
| 98 | + let secondH2 = -1; |
| 99 | + for (let i = 0; i < lines.length; i += 1) { |
| 100 | + if (/^##\s/.test(lines[i]!)) { |
| 101 | + if (firstH2 === -1) { |
| 102 | + firstH2 = i; |
| 103 | + } else { |
| 104 | + secondH2 = i; |
| 105 | + break; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + if (firstH2 === -1) return null; |
| 110 | + return { headingLine: lines[firstH2]!, firstH2, secondH2 }; |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * Replace the body of the first `<h2>` lead section with `localizedBody`, |
| 115 | + * keeping the original heading and repointing the provenance comment at |
| 116 | + * `executive-brief_<lang>.md`. |
| 117 | + */ |
| 118 | +function replaceLeadSectionBody( |
| 119 | + content: string, |
| 120 | + lang: Language, |
| 121 | + localizedBody: string, |
| 122 | + subfolderRepoRelPath: string, |
| 123 | +): string { |
| 124 | + const lines = content.split('\n'); |
| 125 | + const bounds = findLeadBounds(lines); |
| 126 | + if (!bounds) return content; |
| 127 | + |
| 128 | + const sourceRel = `executive-brief_${lang}.md`; |
| 129 | + const sourceUrl = subfolderRepoRelPath |
| 130 | + ? buildGithubBlobUrl(`${subfolderRepoRelPath}/${sourceRel}`) |
| 131 | + : sourceRel; |
| 132 | + |
| 133 | + const before = lines.slice(0, bounds.firstH2); |
| 134 | + const after = bounds.secondH2 === -1 ? [] : lines.slice(bounds.secondH2); |
| 135 | + |
| 136 | + const leadBlock = [ |
| 137 | + bounds.headingLine, |
| 138 | + `<!-- source: ${sourceRel} :: ${sourceUrl} -->`, |
| 139 | + '', |
| 140 | + localizedBody.trim(), |
| 141 | + '', |
| 142 | + ]; |
| 143 | + |
| 144 | + return [...before, ...leadBlock, ...after].join('\n'); |
| 145 | +} |
| 146 | + |
| 147 | +export interface LocalizeExecutiveBriefLeadInput { |
| 148 | + /** Article-markdown body (front-matter already removed). */ |
| 149 | + readonly content: string; |
| 150 | + /** Target language. */ |
| 151 | + readonly lang: Language; |
| 152 | + /** Raw `executive-brief_<lang>.md` markdown when one exists on disk. */ |
| 153 | + readonly localizedBriefMarkdown?: string; |
| 154 | + /** Repo-relative analysis folder, used to rewrite relative links. */ |
| 155 | + readonly subfolderRepoRelPath?: string; |
| 156 | +} |
| 157 | + |
| 158 | +/** |
| 159 | + * Localize the on-page executive-brief lead and strip embedded carrier |
| 160 | + * sections. See the module JSDoc for the full contract. |
| 161 | + */ |
| 162 | +export function localizeExecutiveBriefLead( |
| 163 | + input: LocalizeExecutiveBriefLeadInput, |
| 164 | +): string { |
| 165 | + const stripped = stripEmbeddedLocalizedBriefSections(input.content); |
| 166 | + |
| 167 | + // English keeps the canonical `## What Happened` lead verbatim. |
| 168 | + if (input.lang === 'en') return stripped; |
| 169 | + |
| 170 | + const brief = input.localizedBriefMarkdown; |
| 171 | + if (!brief || brief.trim().length === 0) return stripped; |
| 172 | + |
| 173 | + const cleaned = rewriteRelativeLinks( |
| 174 | + cleanArtifactBody(brief), |
| 175 | + input.subfolderRepoRelPath ?? '', |
| 176 | + ); |
| 177 | + if (cleaned.trim().length === 0) return stripped; |
| 178 | + |
| 179 | + return replaceLeadSectionBody( |
| 180 | + stripped, |
| 181 | + input.lang, |
| 182 | + cleaned, |
| 183 | + input.subfolderRepoRelPath ?? '', |
| 184 | + ); |
| 185 | +} |
0 commit comments