#!/usr/bin/env node 'use strict' // 站点生成器:用 site/_home.tpl.html 与 site/_plugins.tpl.html 渲染首页 / 插件页, // 数据来自 site/releases.json(版本)与 market/index.json(插件)。 const fs = require('fs') const path = require('path') const root = path.resolve(__dirname, '..') const site = path.join(root, 'site') const esc = (s) => String(s == null ? '' : s).replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c])) const mb = (n) => (Number(n) / 1048576).toFixed(1) + ' MB' const data = JSON.parse(fs.readFileSync(path.join(site, 'releases.json'), 'utf8')) const rel = Array.isArray(data.releases) ? data.releases.slice() : (Array.isArray(data) ? data.slice() : []) rel.sort((a, b) => String(b.version).localeCompare(String(a.version), undefined, { numeric: true })) const latest = rel[0] || { version: '', platforms: {} } const V = esc(latest.version) const STAMP = encodeURIComponent(V + '-' + Date.now().toString(36)) const marketFile = path.join(root, 'market', 'index.json') const market = fs.existsSync(marketFile) ? JSON.parse(fs.readFileSync(marketFile, 'utf8')) : { plugins: [] } const plugins = Array.isArray(market.plugins) ? market.plugins : [] const nav = (active) => [ '', ].join('\n') const footer = [ '', ].join('\n') const card = (p) => [ '
' + f[3] + '
$1')
.replace(/\*\*([^*]+)\*\*/g, '$1')
.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '$1')
const lines = String(md).split('\n')
const out = []
let i = 0
let inCode = false
let code = []
let inList = ''
const closeList = () => { if (inList) { out.push('' + inList + '>'); inList = '' } }
while (i < lines.length) {
const line = lines[i]
if (line.trim().indexOf('```') === 0) {
if (!inCode) { inCode = true; code = [] } else {
inCode = false
out.push('' + esc2(code.join('\n')) + '')
}
i++; continue
}
if (inCode) { code.push(line); i++; continue }
if (!line.trim()) { closeList(); i++; continue }
if (/^---+$/.test(line.trim())) { closeList(); out.push('| ' + inline(c) + ' | ').join('') + '
|---|
| ' + inline(c) + ' | ').join('') + '
' + esc2(block.join('\n')) + '')
continue
}
const li = line.match(/^\s*[-*]\s+(.*)$/)
if (li) {
if (inList !== 'ul') { closeList(); out.push('' + inline(line) + '
'); i++ } closeList() if (inCode) out.push('' + esc2(code.join('\n')) + '')
return out.join('\n')
}
function render(tpl, map) {
let out = fs.readFileSync(path.join(site, tpl), 'utf8')
for (const k of Object.keys(map)) out = out.split('').join(map[k])
return out
}
let home = render('_home.tpl.html', {
NAV: nav('home'),
FOOTER: footer,
VERSION: V,
PLUGIN_COUNT: String(plugins.length),
FEAT_CARDS: featCards,
PLUGIN_CARDS: plugins.slice(0, 6).map((p) => card(p).replace(/^\s+/, ' ')).join('\n'),
DL_CARDS: dlCards,
})
home = home.replace('/assets/style.css', '/assets/style.css?v=' + STAMP).replace('/assets/painting.jpg', '/assets/painting.jpg?v=' + STAMP)
fs.writeFileSync(path.join(site, 'index.html'), home)
let pluginsPage = render('_plugins.tpl.html', {
NAV: nav('plugins'),
FOOTER: footer,
PLUGIN_COUNT: String(plugins.length),
TAG_CHIPS: tagChips,
PLUGIN_CARDS: plugins.map(card).join('\n'),
})
fs.mkdirSync(path.join(site, 'plugins'), { recursive: true })
pluginsPage = pluginsPage.replace('/assets/style.css', '/assets/style.css?v=' + STAMP)
fs.writeFileSync(path.join(site, 'plugins', 'index.html'), pluginsPage)
fs.writeFileSync(path.join(site, 'plugins-index.json'), JSON.stringify(market, null, 2) + '\n')
// 插件开发规范(由 docs/standards/plugin-development.md 生成)
const devMdPath = path.join(root, 'docs', 'standards', 'plugin-development.md')
if (fs.existsSync(devMdPath)) {
const devPage = render('_dev.tpl.html', {
NAV: nav('dev'),
FOOTER: footer,
DEV_CONTENT: mdToHtml(fs.readFileSync(devMdPath, 'utf8')),
}).replace('/assets/style.css', '/assets/style.css?v=' + STAMP)
fs.mkdirSync(path.join(site, 'dev'), { recursive: true })
fs.writeFileSync(path.join(site, 'dev', 'index.html'), devPage)
}
console.log('站点已生成:首页 / 插件页(' + plugins.length + ' 个插件,' + tags.length + ' 个标签,版本 v' + V + ')')