Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | 18493x 18492x 8292x 8292x 8292x 8292x 8292x 23570x 23570x 5070x 5070x 18500x 10209x 10209x 8291x 1x 1x 1x 1x 8290x 8292x 1x 4x 8288x 8288x 8288x 7020x 1268x 1268x 1266x 2x 18445x 18444x 18443x 28600x 28600x 8289x 8289x 8288x 8288x 5409x 5409x 2879x 2879x 2879x 28600x 28600x 2879x 10155x 3x 2x 2x 5x 3x 5x 5x 2x 2x 2x 47x 46x 45x 89x 89x 3x 3x 2x 43x | // SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
/**
* @module Aggregator/EditorialBriefResolver
* @description Language-aware editorial brief lookup.
*
* Companion to `article-metadata.ts`. Where that module's
* `extractArtifactHighlight` is intentionally language-agnostic (the first
* canonical editorial artefact wins, regardless of locale), this module
* answers the **per-language** question:
*
* "Given a run directory and a target language `<lang>`, is there a
* sibling `executive-brief_<lang>.md` (or `extended/…` variant) that
* the article-metadata resolver should prefer over the English brief?"
*
* The answer drives the localized-brief precedence rule documented in
* [`.github/prompts/04-article-generation.md`](../../.github/prompts/04-article-generation.md) § 6.2:
*
* 1. translated `executive-brief_<lang>.md` (this module's job)
* 2. translated `extended/executive-brief_<lang>.md` (also this module)
* 3. English `executive-brief.md` — `article-metadata.ts` already
* handles this via the canonical candidate list
*
* The module is intentionally small and side-effect-free; it composes
* the existing low-level helpers (`extractFirstH1`,
* `extractLedeAfterHeading`, `extractStrongProseLine`, `isGenericHeading`,
* `stripArtifactCategoryAffix`, `truncateTitle`) so the parsing rules
* stay identical across the English and localized paths.
*/
import fs from 'fs';
import path from 'path';
import type { LanguageCode } from '../types/index.js';
import {
extractFirstH1,
extractLedeAfterHeading,
extractExtendedLedeAfterHeading,
extractStrongProseLine,
isGenericHeading,
stripArtifactCategoryAffix,
truncateTitle,
} from './article-metadata.js';
import { extractBriefingHighlight } from './metadata/briefing-highlight.js';
/**
* One resolved per-language brief highlight.
*
* `headline` and `summary` follow the same semantics as
* `extractArtifactHighlight` in `article-metadata.ts` — either may be
* empty when the localized brief lacks the relevant section.
*
* `sourceFile` is the run-relative path to the file that produced the
* highlight; downstream callers can record this when populating
* `manifest.metadataFallback` so editors can later audit which locales
* fell through to English.
*
* `sourceLang` matches the language code of the brief that produced the
* highlight (always equal to the requested language for a successful
* lookup; the caller infers `"en"` fallback when this module returns
* `null`).
*/
export interface LocalizedBriefHighlight {
readonly headline: string;
readonly summary: string;
/**
* Longer (up to ~300 chars) summary lifted from the same brief BLUF
* paragraph as {@link summary}, used for `og:description` and
* `twitter:description`. Empty string when the BLUF is short enough
* that the regular `summary` already captures it — see
* `truncateExtendedDescription` for the cutoff. The caller should
* fall back to {@link summary} when this field is empty.
*/
readonly extendedSummary: string;
readonly sourceFile: string;
readonly sourceLang: LanguageCode;
}
/**
* Run-relative candidate paths for a translated brief, in precedence
* order. Mirrors the `executive-brief.md` → `extended/executive-brief.md`
* progression used by the English path so the localized resolution stays
* symmetric.
*
* @param lang - Target language code
* @returns Ordered run-relative paths to probe
*/
export function localizedBriefCandidates(lang: LanguageCode): readonly string[] {
if (lang === 'en') return [];
return [`executive-brief_${lang}.md`, `extended/executive-brief_${lang}.md`];
}
/**
* Read an editorial artefact body while skipping any SPDX HTML-comment
* preamble. Kept private here so the localized path doesn't depend on
* non-exported helpers from `article-metadata.ts`.
*
* Both single-line (`<!-- … -->`) and multi-line comment blocks (where
* `<!--` and `-->` appear on different lines) are stripped, so SPDX
* headers written as
*
* ```
* <!--
* SPDX-FileCopyrightText: 2024-2026 Hack23 AB
* SPDX-License-Identifier: Apache-2.0
* -->
* ```
*
* are handled symmetrically with the single-line form. An unterminated
* `<!--` block (no closing `-->`) is treated as malformed and the body
* is returned starting at that line — downstream `extractFirstH1` will
* then fail to find a heading and the caller will return `null`, which
* is the safe behaviour for a broken brief.
*
* @param abs - Absolute file path
* @returns File contents with SPDX comment lines dropped, or `''` on error
*/
function readArtefactBody(abs: string): string {
let text: string;
try {
text = fs.readFileSync(abs, 'utf8');
} catch {
return '';
}
const lines = text.split('\n');
let i = 0;
while (i < lines.length) {
const line = (lines[i] ?? '').trim();
if (line === '') {
i++;
continue;
}
if (line.startsWith('<!--') && line.endsWith('-->') && line.length >= 7) {
i++;
continue;
}
if (line.startsWith('<!--')) {
// Multi-line comment block — scan forward to the closing `-->`.
const closeIdx = findCommentClose(lines, i);
Iif (closeIdx === -1) break; // malformed: bail out, retain content
i = closeIdx + 1;
continue;
}
break;
}
return lines.slice(i).join('\n');
}
/**
* Locate the line index of the first line that ends with `-->` at or
* after `start`. The terminator must appear at end-of-line (after a
* trim) so an inline `-->` embedded in editorial prose cannot be
* mis-read as the close of an SPDX preamble block.
*
* @param lines - File lines being scanned (read-only)
* @param start - Line index where the unterminated `<!--` was seen
* @returns Closing line index, or `-1` when no terminator is found.
*/
function findCommentClose(lines: readonly string[], start: number): number {
for (let j = start; j < lines.length; j++) {
if ((lines[j] ?? '').trimEnd().endsWith('-->')) return j;
}
return -1;
}
/**
* Compute the editorial headline from a localized brief body. Returns
* `''` when the H1 is fully generic and the stripped variant is also
* generic. Kept private so callers don't need to know about generic-H1
* stripping rules.
*
* @param body - Brief body with the SPDX preamble already stripped
* @param articleType - Article-type slug (for {@link isGenericHeading})
* @param date - ISO run date (for {@link isGenericHeading})
* @returns Editorial headline, possibly empty
*/
function deriveHeadline(body: string, articleType: string, date: string): string {
const rawHeadline = extractFirstH1(body);
Iif (!rawHeadline) return '';
if (!isGenericHeading(rawHeadline, articleType, date)) {
return truncateTitle(rawHeadline);
}
const stripped = stripArtifactCategoryAffix(rawHeadline);
if (stripped && !isGenericHeading(stripped, articleType, date)) {
return truncateTitle(stripped);
}
return '';
}
/**
* Attempt to resolve the localized brief highlight for one language.
* Returns `null` when no `executive-brief_<lang>.md` (or extended
* variant) exists in `runDir`. For `lang === 'en'` always returns `null`
* — the English brief is handled by the canonical
* `extractArtifactHighlight` resolver.
*
* The body of the localized brief is parsed with the same lede/H1
* extractors used for the English path, so the semantic rules are
* symmetric. Generic artefact-category headings
* (e.g. `# Executive Brief — Breaking News (2026-05-15)`) are detected
* and stripped via {@link stripArtifactCategoryAffix} so they cannot
* leak into the SEO surfaces.
*
* @param runDir - Absolute run directory (the parent of the brief)
* @param lang - Target language code
* @param articleType - Article-type slug (for {@link isGenericHeading})
* @param date - ISO run date (for {@link isGenericHeading})
* @returns Resolved localized highlight, or `null` when no brief exists
*/
export function resolveLocalizedBriefHighlight(
runDir: string,
lang: LanguageCode,
articleType: string,
date: string
): LocalizedBriefHighlight | null {
if (lang === 'en') return null;
if (!runDir || !fs.existsSync(runDir)) return null;
for (const rel of localizedBriefCandidates(lang)) {
const abs = path.join(runDir, rel);
if (!fs.existsSync(abs)) continue;
const body = readArtefactBody(abs);
if (!body) continue;
// Tier 1 (NEW, May-2026): structural extraction of `## Strategic
// Intelligence Summary` / `## Reader Briefing` sections. The
// briefing extractor is language-agnostic — it matches on the
// English section headings, which the translation pipeline
// preserves verbatim under the localized brief contract — so a
// Swedish brief whose synthesis section is still written as
// `## Strategic Intelligence Summary` (with translated body
// prose) will resolve correctly here. When the translator has
// additionally localized the section heading the matcher falls
// back to the legacy lede/H1 path below, producing the
// localized H1 as headline.
const briefing = extractBriefingHighlight(body, lang);
if (briefing && (briefing.headline || briefing.summary)) {
const fallbackHeadline = deriveHeadline(body, articleType, date);
return {
// Prefer the H1-derived headline for translated briefs because
// the translator explicitly crafts it as the article headline.
// Only fall back to the structural extraction headline when the
// H1 is generic/empty (deriveHeadline returns '').
headline: fallbackHeadline || briefing.headline,
summary: briefing.summary,
extendedSummary: briefing.extendedSummary || extractExtendedLedeAfterHeading(body),
sourceFile: rel,
sourceLang: lang,
};
}
const headline = deriveHeadline(body, articleType, date);
const lede = extractLedeAfterHeading(body);
const summary = lede || extractStrongProseLine(body);
const extendedSummary = extractExtendedLedeAfterHeading(body);
if (headline || summary) {
return {
headline,
summary,
extendedSummary,
sourceFile: rel,
sourceLang: lang,
};
}
}
return null;
}
/**
* Return the set of language codes for which a translated brief is
* present in `runDir`. Useful for telemetry and for the `metadataFallback`
* accounting that records which locales fall back to English.
*
* @param runDir - Absolute run directory
* @param languages - Ordered list of language codes to probe
* @returns Subset of {@link languages} for which at least one localized
* brief candidate file exists
*/
export function discoverLocalizedBriefs(
runDir: string,
languages: readonly LanguageCode[]
): readonly LanguageCode[] {
if (!runDir || !fs.existsSync(runDir)) return [];
const out: LanguageCode[] = [];
for (const lang of languages) {
if (lang === 'en') continue;
for (const rel of localizedBriefCandidates(lang)) {
const abs = path.join(runDir, rel);
if (fs.existsSync(abs)) {
out.push(lang);
break;
}
}
}
return out;
}
/**
* Localized brief body suitable for HTML rendering.
*
* Unlike {@link resolveLocalizedBriefHighlight} — which extracts a few
* short SEO/metadata fields (headline, summary) for `<meta>` tags and
* JSON-LD — this helper returns the **full body** of the translated
* executive brief, with the SPDX preamble stripped, so the caller can
* render it through {@link renderMarkdown} and splice the resulting
* HTML into the per-language article variant.
*
* Used by the article-generator HTML pipeline (`render-one.ts`) to
* upgrade non-English variants from the English aggregated body to a
* truly localized one whenever a translated `executive-brief_<lang>.md`
* exists in the run directory.
*/
export interface LocalizedBriefBody {
/** Markdown body of the localized brief (post-SPDX strip). */
readonly markdown: string;
/** Run-relative path of the file that produced {@link markdown}. */
readonly sourceFile: string;
}
/**
* Read the **full markdown body** of a translated executive brief for
* `lang` from `runDir`, searching the standard candidate paths
* (`executive-brief_<lang>.md` → `extended/executive-brief_<lang>.md`).
* SPDX HTML-comment preambles are stripped using the same logic as the
* SEO-metadata path, so the returned markdown starts at the first real
* content line (`# Headline` or similar).
*
* Returns `null` when `runDir` is missing, the language is English, or
* no candidate file exists. The caller is expected to fall back to the
* English aggregated body in that case — see `render-one.ts`.
*
* @param runDir - Absolute run directory
* @param lang - Target language code (omitted when `lang === 'en'`)
* @returns Localized brief body + source file, or `null` when absent
*/
export function readLocalizedBriefBody(
runDir: string,
lang: LanguageCode
): LocalizedBriefBody | null {
if (!runDir || lang === 'en') return null;
if (!fs.existsSync(runDir)) return null;
for (const rel of localizedBriefCandidates(lang)) {
const abs = path.join(runDir, rel);
if (!fs.existsSync(abs)) continue;
const body = readArtefactBody(abs);
if (body.trim().length === 0) continue;
return { markdown: body, sourceFile: rel };
}
return null;
}
|