All files / src/aggregator/generator reader-guide-insertion.ts

93.75% Statements 15/16
87.5% Branches 7/8
100% Functions 2/2
92.85% Lines 13/14

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                                                              56x 56x 56x 1x   55x 55x 55x 55x 55x 1x   54x                           55x 53x      
// SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
 
/**
 * @module Aggregator/Generator/ReaderGuideInsertion
 * @description Splice the regenerated Reader Intelligence Guide HTML
 * into the rendered article body at the Executive Brief boundary so
 * the documented section order is preserved (Executive Brief → Reader
 * Intelligence Guide → Key Takeaways → deep sections). Uses
 * `indexOf`-based search to stay clear of polynomial-regex
 * backtracking on pathological input.
 */
 
/**
 * Insert the regenerated Reader Intelligence Guide HTML immediately after
 * the Executive Brief section so the rendered article body matches the
 * documented order (Executive Brief → Reader Intelligence Guide → Key
 * Takeaways → deep sections). The Executive Brief section ends where the
 * next H2 begins; we splice at that boundary. When the brief is absent
 * (sparse runs) we fall back to prepending so the guide still appears
 * at the top of the body.
 *
 * Implementation uses `indexOf` rather than a regex so the splice point
 * is deterministic and immune to polynomial-regex backtracking on
 * pathological input.
 *
 * @param bodyHtml - Rendered article body
 * @param guideHtml - Reader Intelligence Guide HTML fragment
 * @returns Body HTML with the guide spliced after the Executive Brief
 */
export function insertReaderGuideAfterExecutiveBrief(bodyHtml: string, guideHtml: string): string {
  const execBriefAnchor = 'id="section-executive-brief"';
  const briefIdx = bodyHtml.indexOf(execBriefAnchor);
  if (briefIdx === -1) {
    return guideHtml + '\n' + bodyHtml;
  }
  const afterBrief = briefIdx + execBriefAnchor.length;
  const nextH2Tagged = bodyHtml.indexOf('<h2 ', afterBrief);
  const nextH2Bare = bodyHtml.indexOf('<h2>', afterBrief);
  const nextH2 = pickEarliestIndex(nextH2Tagged, nextH2Bare);
  if (nextH2 === -1) {
    return bodyHtml + '\n' + guideHtml;
  }
  return bodyHtml.slice(0, nextH2) + guideHtml + '\n' + bodyHtml.slice(nextH2);
}
 
/**
 * Return the smaller of two `indexOf` results, treating `-1` as "not
 * found" so the caller gets `-1` only when both probes failed. Extracted
 * to keep {@link insertReaderGuideAfterExecutiveBrief} under the
 * useless-assignment lint.
 *
 * @param a - First `indexOf` result
 * @param b - Second `indexOf` result
 * @returns Smaller non-negative index, or `-1` when both are `-1`
 */
function pickEarliestIndex(a: number, b: number): number {
  if (a === -1) return b;
  Eif (b === -1) return a;
  return Math.min(a, b);
}