// tests/helpers/md-section.mjs // The text of one markdown section, for the prose-pin tests. import { strict as assert } from 'node:assert'; import { findSection } from '../../lib/parsers/plan-schema.mjs'; /** * The text of a heading's section (level from its #-count), up to the next * heading of the same or a higher level outside a fence. The heading must exist. * @param {string} text * @param {string} heading - e.g. `## Phase 8 — Final report` * @returns {string} */ export function sectionText(text, heading) { const level = heading.match(/^#+/)[0].length; const s = findSection(text, heading.replace(/^#+ /, ''), level); assert.ok(s, `missing ${heading}`); const lines = s.text.split('\n'); let fenced = false; for (let i = 1; i < lines.length; i++) { if (/^\s{0,3}(`{3,}|~{3,})/.test(lines[i])) fenced = !fenced; const m = !fenced && lines[i].match(/^(#+) /); if (m && m[1].length < level) return lines.slice(0, i).join('\n'); } return s.text; }