feat: serve styled mirror directories with nginx fancyindex
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import { mkdir, readFile, writeFile, unlink } from 'node:fs/promises'
|
||||
import { createI18n } from 'vue-i18n'
|
||||
import { directoryMessages } from '../src/directory/messages.ts'
|
||||
|
||||
const i18n = createI18n({ legacy: false, locale: 'zh-CN', messages: directoryMessages })
|
||||
const t = (key: string) => i18n.global.t(key)
|
||||
const escape = (value: string) => value.replace(/[&<>"']/g, char => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[char]!)
|
||||
const text = (key: string) => `<span data-i18n="${key}">${escape(t(key))}</span>`
|
||||
const navigation = [
|
||||
['mirrors', '/'], ['news', 'https://www.itouc.cn/announcements/'], ['status', '/status'],
|
||||
['feedback', 'https://git.itouc.cn/ITStudio_OUC/OUCMirror-WebUI/issues'],
|
||||
].map(([key, href]) => `<a class="nav-link${key === 'mirrors' ? ' active' : ''}" href="${href}">${text(`common.nav.${key}`)}</a>`).join('')
|
||||
const languages = Object.entries(directoryMessages).map(([locale, messages]) => `<button class="language-option" data-locale="${locale}" lang="${locale}">${escape(messages.common.languageName)}</button>`).join('')
|
||||
const themes = ['light', 'dark', 'system'].map(theme => `<button data-theme-choice="${theme}">${text(`common.themes.${theme}`)}</button>`).join('')
|
||||
const specials: Record<string, string> = { navigation, languages, themes }
|
||||
const html = (await readFile('dist/directory.html', 'utf8')).replace(/\{\{([\w.]+)\}\}/g, (_, key) => {
|
||||
if (key in specials) return specials[key]!
|
||||
if (!i18n.global.te(key)) throw new Error(`Missing directory template message: ${key}`)
|
||||
return escape(t(key))
|
||||
})
|
||||
const parts = html.split('<!-- DIRECTORY_LIST -->')
|
||||
if (parts.length !== 2) throw new Error('Expected exactly one Nginx directory insertion point')
|
||||
await mkdir('dist/fancy-index', { recursive: true })
|
||||
await writeFile('dist/fancy-index/header.html', parts[0]!)
|
||||
await writeFile('dist/fancy-index/footer.html', parts[1]!)
|
||||
// Translate the four native Fancyindex labels on the server as well, so the
|
||||
// default Chinese listing is complete and accessible even without JavaScript.
|
||||
const replacements = [
|
||||
['File Name</a>', `${text('directory.name')}</a>`],
|
||||
['File Size</a>', `${text('directory.size')}</a>`],
|
||||
['Date</a>', `${text('directory.modified')}</a>`],
|
||||
['Parent directory/</a>', `${text('directory.parent')}</a>`],
|
||||
]
|
||||
const quote = (value: string) => '"' + value.replace(/\\/g, '\\\\').replace(/\$/g, '\\$').replace(/"/g, '\\"') + '"'
|
||||
await writeFile('dist/fancy-index/labels.conf', 'sub_filter_once on;\n' + replacements.map(([before, after]) => `sub_filter ${quote(before!)} ${quote(after!)};`).join('\n') + '\n')
|
||||
// Only the two internal fragments are served; Nginx inserts the table between them.
|
||||
await unlink('dist/directory.html')
|
||||
console.log('Built Nginx directory header, footer and locale labels.')
|
||||
@@ -0,0 +1,16 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import { readFile } from 'node:fs/promises'
|
||||
|
||||
// Run after the build to check the actual Nginx fragments and hashed assets.
|
||||
const header = await readFile('dist/fancy-index/header.html', 'utf8')
|
||||
const footer = await readFile('dist/fancy-index/footer.html', 'utf8')
|
||||
const labels = await readFile('dist/fancy-index/labels.conf', 'utf8')
|
||||
assert.ok(!header.includes('directory-path'))
|
||||
assert.ok(footer.includes('</html>'))
|
||||
assert.ok(labels.includes('data-i18n=\\"directory.parent\\"'))
|
||||
assert.ok(!/\{\{[\w.]+\}\}/.test(header + footer))
|
||||
assert.ok(!header.includes('/src/'))
|
||||
for (const [, url] of [...header.matchAll(/(?:src|href)="(\/assets\/[^"?]+)"/g)]) await readFile('dist' + url)
|
||||
assert.ok(header.includes('data-locale="zh-CN"') && header.includes('data-locale="en-US"'))
|
||||
assert.ok(header.includes('data-theme-choice="system"'))
|
||||
console.log('Nginx directory fragments, locale substitution and referenced assets passed.')
|
||||
@@ -10,6 +10,8 @@ import { datetimeFormats as datetimeZh, numberFormats as numbersZh } from '../sr
|
||||
import { datetimeFormats as datetimeEn, numberFormats as numbersEn } from '../src/locales/en-US/formats.ts'
|
||||
import { createI18n } from 'vue-i18n'
|
||||
import { adminZh, adminEn } from '../src/locales/admin-messages.ts'
|
||||
import directoryZh from '../src/locales/zh-CN/directory.ts'
|
||||
import directoryEn from '../src/locales/en-US/directory.ts'
|
||||
|
||||
function flatten(value: Record<string, unknown>, prefix = ''): Record<string, string> {
|
||||
return Object.fromEntries(Object.entries(value).flatMap(([key, entry]) => {
|
||||
@@ -18,8 +20,8 @@ function flatten(value: Record<string, unknown>, prefix = ''): Record<string, st
|
||||
}))
|
||||
}
|
||||
|
||||
const zh = flatten({ common: commonZh, mirrors: mirrorsZh, announcements: announcementsZh, monitor: monitorZh, admin: adminZh })
|
||||
const en = flatten({ common: commonEn, mirrors: mirrorsEn, announcements: announcementsEn, monitor: monitorEn, admin: adminEn })
|
||||
const zh = flatten({ common: commonZh, mirrors: mirrorsZh, announcements: announcementsZh, monitor: monitorZh, admin: adminZh, directory: directoryZh })
|
||||
const en = flatten({ common: commonEn, mirrors: mirrorsEn, announcements: announcementsEn, monitor: monitorEn, admin: adminEn, directory: directoryEn })
|
||||
const errors: string[] = []
|
||||
const formats = {
|
||||
'zh-CN': { datetimeFormats: datetimeZh, numberFormats: numbersZh },
|
||||
|
||||
Reference in New Issue
Block a user