Files
袁永志 aa12b9f910 初始化政体志子站
收录 zhengquan-site 前后端完整代码:24 朝数据、24 幅权力结构图、
5 条沿革(67 节点 / 269 条细释)、44 个词条,以及部署配置与接口测试。
2026-11-19 11:59:51 +08:00

62 lines
1.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
export function esc(value) {
return String(value ?? '')
.replaceAll('&', '&')
.replaceAll('<', '&lt;')
.replaceAll('>', '&gt;')
.replaceAll('"', '&quot;')
.replaceAll("'", '&#39;');
}
export const fmtYear = (y) => (y < 0 ? `前${Math.abs(y)}` : String(y));
export const fmtNum = (n) => Number(n || 0).toLocaleString('zh-CN');
export function truncate(text, max = 26) {
const s = String(text || '');
return s.length > max ? s.slice(0, max) + '…' : s;
}
export function highlight(text, query) {
const safe = esc(text);
const q = String(query || '').trim();
if (!q) return safe;
const terms = [...new Set(q.split(/\s+/).filter(Boolean))].map((t) => esc(t)).filter(Boolean);
if (!terms.length) return safe;
let out = safe;
for (const t of terms.sort((a, b) => b.length - a.length)) {
out = out.replaceAll(t, `<mark>${t}</mark>`);
}
return out;
}
export function parseHash() {
const raw = location.hash.replace(/^#/, '') || '/';
const [pathPart, queryPart] = raw.split('?');
const path = pathPart || '/';
return { path, query: new URLSearchParams(queryPart || '') };
}
export function scrollTop() {
const reduce = matchMedia('(prefers-reduced-motion: reduce)').matches;
window.scrollTo({ top: 0, behavior: reduce ? 'auto' : 'smooth' });
}
export function setTitle(parts) {
const list = [...parts.filter(Boolean), '舆纪历史 · 政体志'];
document.title = list.join('|');
}
export function markClass(name) {
const n = [...String(name)].length;
if (n >= 6) return 'long2';
if (n >= 4) return 'long';
return '';
}
/** 把「中书省 · 门下省」这类分隔文本拆成标签 */
export const splitTags = (text) =>
String(text || '')
.split(/[·、,,//]/)
.map((s) => s.trim())
.filter(Boolean);