import { renderCards } from './cards.mjs'; const escape = value => String(value).replace(/[&<>"']/g,c=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c])); const locale = language => language==='zh-Hans'?'zh':'en'; const url = work => `/${locale(work.language)}/essays/${work.slug}/`; const draftLabel = lang => lang==='zh'?'私有草稿':'Private draft'; const dateLabel = (work,lang) => work.date?``:work.publicDisplay?'':`${draftLabel(lang)}`; const labels={ en:{selected:'Selected',topics:'Topics',archive:'Archive',home:'Selected writing',intro:'Engineering AI infrastructure, compute, and storage for correctness, performance, and reliability at scale—grounded in production experience.',preview:'Local preview · Not published',all:'All articles',read:'Reading paths',recent:'From the archive',empty:'No articles in this preview.',original:'English original',sourceDate:'First published on Medium',back:'All articles',series:'Storage–compute separation',nav:'Main navigation',skip:'Skip to content'}, zh:{selected:'精选',topics:'专题',archive:'归档',home:'精选文章',intro:'立足生产一线,剖析 AI 基础设施、计算与存储的系统设计,探究大规模运行下的正确性、性能与稳定性。',preview:'本地预览 · 未发布',all:'全部文章',read:'专题阅读',recent:'归档选读',empty:'当前预览暂无文章。',original:'英文原稿',sourceDate:'首次发表于 Medium',back:'全部文章',series:'存算分离',nav:'主导航',skip:'跳到正文'} }; const topicNames={ performance:{en:'Performance & memory',zh:'性能与内存'}, systems:{en:'Storage & coordination',zh:'存储与协调'}, debugging:{en:'Production debugging',zh:'线上故障诊断'} }; const evidenceNames={ 'engineering-record':{en:'Engineering record',zh:'工程实录'}, 'design-analysis':{en:'Design analysis',zh:'设计分析'}, hypothesis:{en:'Hypothesis',zh:'研究假设'} }; function shell({lang='en',title,active,content,alternate,article=false,publicRelease=false}) { const t=labels[lang]; const other=lang==='en'?'zh':'en'; const links=[['selected',`/${lang}/`],['topics',`/${lang}/topics/`],['archive',`/${lang}/archive/`]]; const alt=alternate?`${other==='zh'?'中文':'EN'}`:''; return ` ${publicRelease?'':''} ${escape(title)} — MrPresent-Han ${publicRelease?'':`
${t.preview}
`}
${content}
`; } function rows(works,lang) { if(!works.length)return `

${labels[lang].empty}

`; return `
    ${works.map(w=>`
  1. ${dateLabel(w,lang)}

    ${escape(w.title)}

  2. `).join('')}
`; } function topicLinks(works,lang) { return ``; } export function renderSite(works,options={}) { const publicRelease=options.publicRelease===true; const locales=publicRelease?['en','zh'].filter(lang=>works.some(work=>locale(work.language)===lang)):['en','zh']; const renderShell=args=>shell({...args,publicRelease,alternate:publicRelease&&args.alternate&&!locales.includes(args.alternate.split('/')[1])?null:args.alternate}); const pages=new Map(); const sorted=[...works].sort((a,b)=>(b.date??'9999').localeCompare(a.date??'9999')||(a.seriesOrder??0)-(b.seriesOrder??0)); for(const lang of locales) { const t=labels[lang]; const other=lang==='en'?'zh':'en'; const entries=sorted.filter(w=>w.language===(lang==='en'?'en':'zh-Hans') || !publicRelease&&!works.some(otherWork=>otherWork.id===w.id&&otherWork.language===(lang==='en'?'en':'zh-Hans'))); const selected=entries.filter(w=>w.featured); pages.set(`/${lang}/`,renderShell({lang,title:t.home,active:'selected',alternate:`/${other}/`,content:`

${t.home}

${renderCards([...selected,...entries.filter(w=>!w.featured)],lang)}

${t.read}

${t.topics}
${topicLinks(entries,lang)}
`})); pages.set(`/${lang}/topics/`,renderShell({lang,title:t.topics,active:'topics',alternate:`/${other}/topics/`,content:`

${t.topics}

${topicLinks(entries,lang)}${!entries.length?`

${t.empty}

`:''}`})); for(const [key,name] of Object.entries(topicNames)) { const group=entries.filter(w=>w.topic===key); if(!group.length)continue; const series=group.filter(w=>w.seriesOrder).sort((a,b)=>a.seriesOrder-b.seriesOrder); pages.set(`/${lang}/topics/${key}/`,renderShell({lang,title:name[lang],active:'topics',alternate:!publicRelease||works.some(w=>locale(w.language)===other&&w.topic===key)?`/${other}/topics/${key}/`:null,content:`${t.topics}

${name[lang]}

${series.length?`

${t.series}

${rows(series,lang)}
`:''}${rows(group.filter(w=>!w.seriesOrder),lang)}`})); } const drafts=entries.filter(w=>!w.date); const years=[...new Set(entries.filter(w=>w.date).map(w=>w.date.slice(0,4)))]; pages.set(`/${lang}/archive/`,renderShell({lang,title:t.archive,active:'archive',alternate:`/${other}/archive/`,content:`

${t.all}

${drafts.length?`
${publicRelease?'':`

${draftLabel(lang)}

`}${rows(drafts,lang)}
`:''}${entries.length?years.map(year=>`

${year}

${rows(entries.filter(w=>w.date?.startsWith(year)),lang)}
`).join(''):`

${t.empty}

`}`})); } for(const work of works) { const lang=locale(work.language), t=labels[lang]; const alternate=works.find(w=>w.id===work.id&&w.language!==work.language); const series=works.filter(w=>w.seriesOrder&&w.language===work.language).sort((a,b)=>a.seriesOrder-b.seriesOrder); pages.set(url(work),renderShell({lang,title:work.title,article:true,alternate:alternate?url(alternate):null,content:` ${t.back}
${topicNames[work.topic][lang]}${evidenceNames[work.evidence][lang]}

${escape(work.title)}

${work.body}

— MrPresent-Han

${work.omittedReferenceLinks?`

${publicRelease?(lang==='zh'?'参考材料未公开;相关引用仅显示标题。':'Reference materials are not public; their titles are shown without links.'):(lang==='zh'?'文中参考草稿未纳入本次预览;相关引用仅显示标题。':'Linked reference drafts are not included in this preview; their titles are shown without links.')}

`:''} ${work.seriesOrder?``:''}`})); } pages.set('/',pages.get(`/${locales[0]}/`)); pages.set('/404.html',renderShell({lang:locales[0],title:'Page not found',content:`

Page not found

${publicRelease?'Page unavailable.':'This page is not part of the local preview.'}

Selected writing
`})); return pages; }