import { search } from '../api.js'; import { esc, highlight } from '../util.js'; const HOT = ['三省六部', '军机处', '科举制', '行省', '二府三司', '内阁', '刺史', '九品中正制']; function resultRow(r, q) { return `
${highlight(r.title, q)}
${esc(r.subtitle)}
${highlight(r.snippet, q)}
`; } export async function renderSearch({ query }) { const q = (query.get('q') || '').trim(); const data = q ? await search(q) : null; let body = ''; if (!q) { body = `
试着检索 ${HOT.map((t) => `${esc(t)}`).join('')}
二十四朝按时代与国祚翻阅 五条沿革宰相、监察、地方、选官、军事 四十词条职官与制度术语释义

检索范围包括朝代名与概要、国体政体表述、中央机构与长官、地方层级、沿革要点、史料引文,以及五条沿革的全部节点与词条释义。

`; } else if (!data.total) { body = `

没有找到与「${esc(q)}」相关的内容。可换个说法,或从总目逐朝翻阅。

`; } else { const groups = [ ['朝代', data.groups.dynasty], ['沿革', data.groups.thread], ['词条', data.groups.term], ].filter(([, list]) => list.length); body = `

共 ${data.total} 条结果,显示 ${data.groups.dynasty.length + data.groups.thread.length + data.groups.term.length} 条

${groups.map(([label, list]) => `

${label}

${list.map((r) => resultRow(r, q)).join('')}
`).join('')}
`; } const html = `

检索 · 全志

检索

${body} `; function mount() { const form = document.querySelector('[data-search-form]'); form?.addEventListener('submit', (e) => { e.preventDefault(); const val = form.querySelector('input[name="q"]').value.trim(); location.hash = val ? `#/search?q=${encodeURIComponent(val)}` : '#/search'; }); } return { html, title: q ? `检索 · ${q}` : '检索', mount }; }