All files / src/aggregator progressive-disclosure.ts

98.21% Statements 55/56
85.18% Branches 23/27
100% Functions 9/9
100% Lines 50/50

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                                              16x           16x                               1002x   59852x       1082x             334x 334x 334x 334x 207x   127x 677x 677x 677x 677x 677x 677x   127x 334x       1082x 1082x 830x 458x                 334x 334x 334x 334x   334x 677x 677x 558x 320x     334x 334x 334x 334x           334x       850x       282x 282x 282x 282x                           281x 281x 281x           281x 74x                   281x 59x                   281x    
// SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
 
import { stripHtmlTags } from '../utils/html-sanitize.js';
import { escapeHTML } from '../utils/file-utils.js';
import { PROGRESSIVE_DISCLOSURE_LABELS } from '../constants/languages.js';
import { getLocalizedString } from '../constants/language-core.js';
import type { LanguageCode } from '../types/index.js';
 
export type DisclosureLayer = 'quick' | 'analysis' | 'intelligence';
 
export interface LayerWordCounts {
  readonly quick: number;
  readonly analysis: number;
  readonly intelligence: number;
}
 
export interface LayerReadingTimes {
  readonly quickRead: number;
  readonly fullAnalysis: number;
  readonly completeIntelligence: number;
}
 
export const QUICK_LAYER_SECTION_IDS = new Set([
  'executive-brief',
  'key-takeaways',
  'reader-intelligence-guide',
]);
 
export const ANALYSIS_LAYER_SECTION_IDS = new Set([
  'synthesis',
  'significance',
  'actors-forces',
  'coalitions-voting',
  'stakeholder-map',
  'economic-context',
  'risk',
]);
 
interface SectionSlice {
  readonly id: string;
  readonly html: string;
}
 
function countWordsInHtml(html: string): number {
  return stripHtmlTags(html)
    .split(/\s+/u)
    .filter((word) => word.length > 0).length;
}
 
function normalizeSectionId(id: string): string {
  return id.replace(/^section-/, '');
}
 
function splitSectionSlices(bodyHtml: string): {
  readonly preface: string;
  readonly sections: readonly SectionSlice[];
} {
  const sections: SectionSlice[] = [];
  const headingPattern = /<h2\b[^>]*\bid=(["'])([^"']+)\1[^>]*>/giu;
  const matches = Array.from(bodyHtml.matchAll(headingPattern));
  if (matches.length === 0) {
    return { preface: bodyHtml, sections };
  }
  for (let index = 0; index < matches.length; index += 1) {
    const match = matches[index];
    Iif (!match) continue;
    const start = match.index ?? 0;
    const end = matches[index + 1]?.index ?? bodyHtml.length;
    const id = match[2] ?? '';
    sections.push({ id, html: bodyHtml.slice(start, end) });
  }
  const first = matches[0]?.index ?? 0;
  return { preface: bodyHtml.slice(0, first), sections };
}
 
export function resolveDisclosureLayer(sectionId: string): DisclosureLayer {
  const normalized = normalizeSectionId(sectionId);
  if (QUICK_LAYER_SECTION_IDS.has(normalized)) return 'quick';
  if (ANALYSIS_LAYER_SECTION_IDS.has(normalized)) return 'analysis';
  return 'intelligence';
}
 
export function splitBodyIntoDisclosureLayers(bodyHtml: string): {
  readonly quickHtml: string;
  readonly analysisHtml: string;
  readonly intelligenceHtml: string;
  readonly wordCounts: LayerWordCounts;
} {
  const { preface, sections } = splitSectionSlices(bodyHtml);
  const quickParts: string[] = preface.trim().length > 0 ? [preface] : [];
  const analysisParts: string[] = [];
  const intelligenceParts: string[] = [];
 
  for (const section of sections) {
    const layer = resolveDisclosureLayer(section.id);
    if (layer === 'quick') quickParts.push(section.html);
    else if (layer === 'analysis') analysisParts.push(section.html);
    else intelligenceParts.push(section.html);
  }
 
  const quickHtml = quickParts.join('\n');
  const analysisHtml = analysisParts.join('\n');
  const intelligenceHtml = intelligenceParts.join('\n');
  const wordCounts: LayerWordCounts = {
    quick: countWordsInHtml(quickHtml),
    analysis: countWordsInHtml(analysisHtml),
    intelligence: countWordsInHtml(intelligenceHtml),
  };
 
  return { quickHtml, analysisHtml, intelligenceHtml, wordCounts };
}
 
export function estimateReadingMinutes(wordCount: number): number {
  return Math.ceil(wordCount / 238);
}
 
export function buildLayerReadingTimes(words: LayerWordCounts): LayerReadingTimes {
  const quick = words.quick;
  const analysis = words.quick + words.analysis;
  const complete = words.quick + words.analysis + words.intelligence;
  return {
    quickRead: estimateReadingMinutes(quick),
    fullAnalysis: estimateReadingMinutes(analysis),
    completeIntelligence: estimateReadingMinutes(complete),
  };
}
 
export function buildProgressiveDisclosureBody(
  bodyHtml: string,
  lang: LanguageCode = 'en'
): {
  readonly bodyHtml: string;
  readonly wordCounts: LayerWordCounts;
} {
  const labels = getLocalizedString(PROGRESSIVE_DISCLOSURE_LABELS, lang);
  const layers = splitBodyIntoDisclosureLayers(bodyHtml);
  const output: string[] = [
    `<section class="article-layer article-layer--quick" data-disclosure-layer="quick" aria-label="${escapeHTML(labels.quickRead)}">`,
    layers.quickHtml,
    `</section>`,
  ];
 
  if (layers.analysisHtml.trim().length > 0) {
    output.push(
      `<details class="article-layer article-layer--analysis article-layer-details" data-disclosure-layer="analysis" id="article-layer-analysis">`,
      `<summary class="article-layer-summary"><span class="article-layer-summary__title">${escapeHTML(labels.expandAnalysis)} ↓</span></summary>`,
      `<section class="article-layer-content" aria-label="${escapeHTML(labels.fullAnalysis)}">`,
      layers.analysisHtml,
      `</section>`,
      `</details>`
    );
  }
 
  if (layers.intelligenceHtml.trim().length > 0) {
    output.push(
      `<details class="article-layer article-layer--intelligence article-layer-details" data-disclosure-layer="intelligence" id="article-layer-intelligence">`,
      `<summary class="article-layer-summary"><span class="article-layer-summary__title">${escapeHTML(labels.expandIntelligence)} ↓</span></summary>`,
      `<section class="article-layer-content" aria-label="${escapeHTML(labels.completeIntelligence)}">`,
      layers.intelligenceHtml,
      `</section>`,
      `</details>`
    );
  }
 
  return { bodyHtml: output.join('\n'), wordCounts: layers.wordCounts };
}