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 | 8x 8x 8x 8x 210x 37x 37x 37x 37x 157x 157x 157x 37x 21x 21x 5x 5x 16x 16x 7x 7x 7x 7x 9x 9x 4x 4x 7x 16x 16x 16x 16x 16x 16x 1x 1x 16x 16x 16x 1x 1x 16x 21x 21x 21x 105x 105x 21x 21x 21x 21x 11x 11x 21x 21x 21x 3x 3x 21x 21x 16x 16x 16x 16x 11x 16x 1x 15x 15x 10x 5x 21x 21x 14x 7x 21x 21x 21x 21x | // SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
/**
* @module Utils/ContentMetadata
* @description Content-based metadata analysis for articles.
*
* Analyses the **rendered article HTML** to extract insightful titles,
* descriptions, and keywords. This runs *after* {@link buildContent}
* produces the article body so that metadata truly reflects what the
* reader will see — not mechanical counts from the raw data payload.
*
* The analysis extracts:
* - Headings (h2/h3) as topic indicators
* - The lede paragraph for a content-based description
* - Key statistics (numbers, percentages) for title highlights
* - Entity names (committees, legislation titles) for keywords
* - Section counts for a structural overview
*/
import type { ArticleMetadata } from '../generators/strategies/article-strategy.js';
/** Maximum length for the enriched description */
const MAX_DESCRIPTION_LENGTH = 200;
/** Maximum number of keywords to emit */
const MAX_KEYWORDS = 15;
/** Minimum heading length to include as keyword */
const MIN_HEADING_KEYWORD_LENGTH = 4;
/** Maximum heading length to include as keyword */
const MAX_HEADING_KEYWORD_LENGTH = 80;
/**
* Strip HTML tags and decode common HTML entities to plain text.
*
* @param html - HTML string
* @returns Plain-text string
*/
function stripHtml(html: string): string {
return html
.replace(/<[^>]+>/gu, ' ')
.replace(/</gu, '<')
.replace(/>/gu, '>')
.replace(/"/gu, '"')
.replace(/'/gu, "'")
.replace(/—/gu, '\u2014')
.replace(/–/gu, '\u2013')
.replace(/&/gu, '&')
.replace(/\s+/gu, ' ')
.trim();
}
/**
* Extract all h2 and h3 heading texts from article content.
*
* @param content - Article HTML body
* @returns Array of heading text strings
*/
function extractHeadings(content: string): string[] {
const headingRegex = /<h([23])\b[^>]*>([\s\S]*?)<\/h\1>/giu;
const headings: string[] = [];
let match: RegExpExecArray | null = headingRegex.exec(content);
while (match) {
const text = stripHtml(match[2] ?? '').trim();
Eif (text.length > 0) headings.push(text);
match = headingRegex.exec(content);
}
return headings;
}
/**
* Extract the lede from article content as a plain-text description base.
*
* Prefers a <p class="lede">...</p>, then a <section class="lede">...</section>
* (using its first paragraph or full text), and finally falls back to
* the first <p> in the content if no lede-specific markup is found.
*
* @param content - Article HTML body
* @returns Plain-text lede string, or empty string
*/
function extractLede(content: string): string {
// Try explicit lede paragraph first: <p class="lede">...</p>
const ledeParagraphMatch = /<p[^>]*class="[^"]*\blede\b[^"]*"[^>]*>([\s\S]*?)<\/p>/iu.exec(
content
);
if (ledeParagraphMatch?.[1]) {
const text = stripHtml(ledeParagraphMatch[1]).trim();
Eif (text.length > 20) return text;
}
// Try section-based lede: <section class="lede"> ... <p>...</p> ... </section>
const ledeSectionMatch =
/<section[^>]*class="[^"]*\blede\b[^"]*"[^>]*>([\s\S]*?)<\/section>/iu.exec(content);
if (ledeSectionMatch?.[1]) {
const sectionParagraphMatch = /<p[^>]*>([\s\S]*?)<\/p>/iu.exec(ledeSectionMatch[1]);
Eif (sectionParagraphMatch?.[1]) {
const text = stripHtml(sectionParagraphMatch[1]).trim();
Eif (text.length > 20) return text;
}
const sectionText = stripHtml(ledeSectionMatch[1]).trim();
if (sectionText.length > 20) return sectionText;
}
// Fall back to first paragraph in article-content
const paraMatch = /<p[^>]*>([\s\S]*?)<\/p>/iu.exec(content);
if (paraMatch?.[1]) {
const text = stripHtml(paraMatch[1]).trim();
if (text.length > 20) return text;
}
return '';
}
/**
* Extract key statistics (numbers with context) from article content.
* Looks for patterns like "42 adopted texts", "85% pipeline health", etc.
*
* @param content - Article HTML body
* @returns Array of statistic highlight strings
*/
function extractStatistics(content: string): string[] {
const text = stripHtml(content);
const stats: string[] = [];
// Match "N adopted texts" / "N documents" / "N procedures" / "N events" etc.
// Use a simple alternation list — no nested quantifiers.
const countWords = [
'adopted texts',
'adopted text',
'documents',
'document',
'procedures',
'procedure',
'events',
'event',
'votes',
'vote',
'questions',
'question',
'anomalies',
'anomaly',
'committees',
'committee',
'resolutions',
'resolution',
'MEPs',
'MEP',
'sessions',
'session',
'meetings',
'meeting',
].join('|');
const countPatterns = new RegExp(`(\\d+)\\s+(${countWords})`, 'giu');
let match: RegExpExecArray | null = countPatterns.exec(text);
while (match) {
stats.push(`${match[1]} ${match[2]}`);
match = countPatterns.exec(text);
}
// Match percentages — integer or decimal followed by %
const pctPatterns = /(\d[\d.]*\d|\d)%/gu;
match = pctPatterns.exec(text);
while (match) {
stats.push(`${match[1]}%`);
match = pctPatterns.exec(text);
}
// Deduplicate
return [...new Set(stats)].slice(0, 5);
}
/**
* Extract content-derived keywords from headings and prominent terms.
*
* @param content - Article HTML body
* @param baseKeywords - Keywords from the strategy (preserved)
* @returns Deduplicated keyword array
*/
function extractContentKeywords(content: string, baseKeywords: readonly string[]): string[] {
const keywords: string[] = [...baseKeywords];
// Add headings as keywords
const headings = extractHeadings(content);
for (const h of headings) {
Eif (h.length >= MIN_HEADING_KEYWORD_LENGTH && h.length <= MAX_HEADING_KEYWORD_LENGTH) {
keywords.push(h);
}
}
// Work against plain text for entity extraction to avoid false positives from markup
const plainText = stripHtml(content);
// Extract committee abbreviations (ENVI, ECON, AFET, etc.)
const abbrRegex =
/\b(ENVI|ECON|AFET|LIBE|AGRI|ITRE|IMCO|TRAN|REGI|PECH|CULT|JURI|BUDG|CONT|EMPL|INTA|DEVE|DROI|SEDE)\b/gu;
let match: RegExpExecArray | null = abbrRegex.exec(plainText);
while (match) {
keywords.push(match[1] ?? '');
match = abbrRegex.exec(plainText);
}
// Extract political group names
const groupRegex = /\b(EPP|S&D|Renew|Greens\/EFA|ECR|The Left|ID|PfE)\b/gu;
match = groupRegex.exec(plainText);
while (match) {
keywords.push(match[1] ?? '');
match = groupRegex.exec(plainText);
}
return [...new Set(keywords)].slice(0, MAX_KEYWORDS);
}
/**
* Build a content-aware title by analysing the article body for key
* highlights and appending the most significant finding as a suffix.
*
* @param content - Article HTML body
* @param baseTitle - Localized base title from the strategy
* @returns Enriched title string
*/
function buildContentTitle(content: string, baseTitle: string): string {
// If the strategy already appended a suffix (contains em-dash), do not double-suffix
if (baseTitle.includes('—')) return baseTitle;
const headings = extractHeadings(content);
const stats = extractStatistics(content);
// Build a suffix from the first meaningful statistic
const topStat = stats[0];
// Build a suffix from the first heading that isn't a generic section label
const topHeading = headings.find(
(h) =>
h.length > 10 &&
!/^(introduction|overview|analysis|conclusion|summary|background|context)/iu.test(h)
);
if (topStat && topHeading) {
return `${baseTitle} — ${topStat}, ${topHeading}`;
}
Iif (topStat) {
return `${baseTitle} — ${topStat}`;
}
if (topHeading) {
return `${baseTitle} — ${topHeading}`;
}
return baseTitle;
}
/**
* Build a content-aware description by extracting the lede paragraph
* from the article body. Falls back to the strategy-provided subtitle.
*
* @param content - Article HTML body
* @param baseSubtitle - Subtitle from the strategy as fallback
* @returns SEO-friendly description string (≤ {@link MAX_DESCRIPTION_LENGTH} chars)
*/
function buildContentDescription(content: string, baseSubtitle: string): string {
const lede = extractLede(content);
if (lede.length > 30) {
return lede.length > MAX_DESCRIPTION_LENGTH
? lede.slice(0, MAX_DESCRIPTION_LENGTH - 3) + '...'
: lede;
}
return baseSubtitle;
}
/**
* Enrich article metadata by analysing the rendered article content.
*
* This function is the main entry point — called by the generation pipeline
* **after** {@link buildContent} produces the article HTML body but
* **before** the HTML is wrapped in the full page template.
*
* It refines the strategy-provided base metadata with content-derived
* insights so that titles, descriptions, and keywords reflect the
* actual article coverage rather than generic template text.
*
* @param content - Rendered article HTML body (from strategy.buildContent)
* @param baseMetadata - Base metadata from strategy.getMetadata
* @returns Enriched metadata with content-aware title, description, and keywords
*/
export function enrichMetadataFromContent(
content: string,
baseMetadata: ArticleMetadata
): ArticleMetadata {
const title = buildContentTitle(content, baseMetadata.title);
const subtitle = buildContentDescription(content, baseMetadata.subtitle);
const keywords = extractContentKeywords(content, baseMetadata.keywords);
return {
...baseMetadata,
title,
subtitle,
keywords,
};
}
|