// @requirements REQ-PRODUCT-DOCS-SEARCH import {JSDOM} from 'jsdom'; import {searchIndexVersion,maxSearchIndexBytes,prepareGuideSearch} from './guide-search.mjs'; import {guideSections} from './navigation.mjs'; const e=value=>String(value).replace(/[&<>"']/gu,c=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c])); export function guideSearchPage(entry){ const dom=new JSDOM(entry.html),sections=[];let heading=entry.title,url=entry.route.replace(/\/$/u,'')+'/',buffer=''; const flush=()=>{if(buffer.trim())sections.push({heading,url,text:buffer.trim()});buffer='';}; try{ for(const el of dom.window.document.querySelectorAll('script,style,nav,button'))el.remove(); for(const node of dom.window.document.body.childNodes){ if(node.nodeType===1&&/^H[1-6]$/u.test(node.tagName)){ flush();heading=node.textContent.trim().slice(0,300);url=entry.route.replace(/\/$/u,'')+'/'+(node.id?'#'+encodeURIComponent(node.id):''); continue; } // Separate text nodes keep table cells and inline code searchable as words. const walker=dom.window.document.createTreeWalker(node,dom.window.NodeFilter.SHOW_TEXT);let value='',item; if(node.nodeType===3)value=node.textContent;else while((item=walker.nextNode()))value+=' '+item.textContent; value=value.replace(/\s+/gu,' ').trim();if(!value)continue; buffer+=(buffer?' ':'')+value; while(buffer.length>3600){let end=buffer.lastIndexOf(' ',3500);if(end<3000)end=3500;sections.push({heading,url,text:buffer.slice(0,end)});buffer=buffer.slice(Math.max(0,end-160));} } flush();if(!sections.length)sections.push({heading,url,text:''});return {title:entry.title,area:entry.area,url:entry.route.replace(/\/$/u,'')+'/',sections}; }finally{dom.window.close();} } export function buildGuideSearchIndex(pages){ const index={version:searchIndexVersion,pages:[...pages].sort((a,b)=>a.url.localeCompare(b.url,'en'))}; prepareGuideSearch(index); const json=JSON.stringify(index)+'\n';if(Buffer.byteLength(json)>maxSearchIndexBytes)throw Error('Published guide search index exceeds its delivery bound'); return json; } export function guideSearchBody(pages){ const groups=new Map(guideSections.map(section=>[section.title,[]]));for(const page of pages){if(!groups.has(page.area))groups.set(page.area,[]);groups.get(page.area).push(page);} return `

Documentation

Find the right guide

Search by topic, API name or requirement ID.

`; }