( generatedMarkdown: string, existingContent: string | null, manualSections: Record<string, string> )
| 2661 | } |
| 2662 | |
| 2663 | function mergeWithManualContent( |
| 2664 | generatedMarkdown: string, |
| 2665 | existingContent: string | null, |
| 2666 | manualSections: Record<string, string> |
| 2667 | ): string { |
| 2668 | if (!existingContent || Object.keys(manualSections).length === 0) { |
| 2669 | return generatedMarkdown |
| 2670 | } |
| 2671 | |
| 2672 | let mergedContent = generatedMarkdown |
| 2673 | |
| 2674 | Object.entries(manualSections).forEach(([sectionName, content]) => { |
| 2675 | const insertionPoints: Record<string, { regex: RegExp }> = { |
| 2676 | intro: { |
| 2677 | regex: /<BlockInfoCard[\s\S]*?(\/>|<\/svg>`}\s*\/>)/, |
| 2678 | }, |
| 2679 | usage: { |
| 2680 | regex: /## Usage Instructions/, |
| 2681 | }, |
| 2682 | outputs: { |
| 2683 | regex: /## Outputs/, |
| 2684 | }, |
| 2685 | notes: { |
| 2686 | regex: /## Notes/, |
| 2687 | }, |
| 2688 | } |
| 2689 | |
| 2690 | const insertionPoint = insertionPoints[sectionName] |
| 2691 | const wrapped = `{/* MANUAL-CONTENT-START:${sectionName} */}\n${content}\n{/* MANUAL-CONTENT-END */}` |
| 2692 | |
| 2693 | const match = insertionPoint ? mergedContent.match(insertionPoint.regex) : null |
| 2694 | if (match && match.index !== undefined) { |
| 2695 | const insertPosition = match.index + match[0].length |
| 2696 | mergedContent = `${mergedContent.slice(0, insertPosition)}\n\n${wrapped}\n${mergedContent.slice(insertPosition)}` |
| 2697 | } else { |
| 2698 | // Never drop manual content: when the anchor is missing (e.g. a `notes` |
| 2699 | // section with no generated "## Notes" heading), append at the end. |
| 2700 | console.log(`No insertion anchor for manual section "${sectionName}" — appending at end`) |
| 2701 | mergedContent = `${mergedContent.replace(/\s*$/, '')}\n\n${wrapped}\n` |
| 2702 | } |
| 2703 | }) |
| 2704 | |
| 2705 | return mergedContent |
| 2706 | } |
| 2707 | |
| 2708 | async function generateBlockDoc(blockPath: string) { |
| 2709 | try { |
no test coverage detected