diff --git a/docs/content/setup/site.md b/docs/content/setup/site.md index ff816fd..ee6d042 100644 --- a/docs/content/setup/site.md +++ b/docs/content/setup/site.md @@ -26,14 +26,14 @@ site: ```markdown theme: - extra_css: + extraCss: - /assets/extra.css - extra_js: + extraJs: - /assets/extra.js ``` -- `extra_css`: 附加的 CSS 文件路径 -- `extra_js`: 附加的 JavaScript 文件路径 +- `extraCss`: 附加的 CSS 文件路径 +- `extraJs`: 附加的 JavaScript 文件路径 ## CDN 设置 diff --git a/lib/directory-processor.js b/lib/directory-processor.js index c901018..71bb1a0 100644 --- a/lib/directory-processor.js +++ b/lib/directory-processor.js @@ -9,6 +9,7 @@ class DirectoryProcessor { this.language = language; this.outputPath = outputPath; this.pages = new Map(); + this.fileProcessor = new FileProcessor(this.config, this.pages, config.sourcePath, config.outputPath); } @@ -69,10 +70,67 @@ class DirectoryProcessor { console.log('✓ 创建根目录重定向页面:', rootIndexPath); } + async create404Pages() { + if (this.isFeatureEnabled('i18n')) { + for (const locale of this.getAvailableLocales()) { + const metadata = { + config: { + toc: false, + sidebar: false + }, + title: locale.key === 'zh-CN' ? '页面未找到' : 'Page Not Found', + message: locale.key === 'zh-CN' + ? '抱歉,您访问的页面不存在。' + : 'Sorry, the page you are looking for does not exist.', + backLink: locale.key === 'zh-CN' ? '返回首页' : 'Back to Home', + language: locale.key, + noLocalePath: `/404.html`, + layout: 'layouts/404' + }; + + const html = await this.fileProcessor.processLayoutFile(metadata, locale.key); + const outputDir = path.join(this.outputPath, locale.key); + const outputPath = path.join(outputDir, '404.html'); + + if (!fs.existsSync(outputDir)) { + fs.mkdirSync(outputDir, {recursive: true}); + } + fs.writeFileSync(outputPath, html); + console.log(`✓ 创建 404 页面: ${outputPath}`); + } + } + else { + const metadata = { + config: { + toc: false, + sidebar: false + }, + title: '页面未找到', + message: '抱歉,您访问的页面不存在。', + backLink: '返回首页', + noLocalePath: `/404.html`, + layout: 'layouts/404' + }; + + const html = await this.fileProcessor.processLayoutFile(metadata, ''); + const outputDir = path.join(this.outputPath, ''); + const outputPath = path.join(outputDir, '404.html'); + + if (!fs.existsSync(outputDir)) { + fs.mkdirSync(outputDir, {recursive: true}); + } + fs.writeFileSync(outputPath, html); + console.log(`✓ 创建 404 页面: ${outputPath}`); + } + } + async processLanguageDirectory(sourceDir, baseDir = '') { // 预加载所有页面数据 await this.preloadPages(this.config.sourcePath); + // 创建 404 页面 + await this.create404Pages(); + if (this.isFeatureEnabled('i18n')) { console.log("\n📂 正在处理国际化"); diff --git a/lib/file-processor.js b/lib/file-processor.js index dd627b0..e76987b 100644 --- a/lib/file-processor.js +++ b/lib/file-processor.js @@ -73,6 +73,9 @@ class FileProcessor { // 配置的国际化 this.config.languages = getAvailableLocales(this.config) const localPath = appendHtml(pagePath, this.config, locale); + const noLocalePath = isFeatureEnabled(this.config, 'i18n') + ? localPath.replace(`/${locale}`, '') + : localPath; const pageData = { pageData: { @@ -81,7 +84,7 @@ class FileProcessor { title: translatedTitle, content: content, path: localPath, - noLocalePath: isFeatureEnabled(this.config, 'i18n') ? localPath.replace(`/${locale}`, '') : localPath, + noLocalePath: noLocalePath, language: locale, basePath: getRelativeBasePath(baseDir), layout: metadata.layout || 'layouts/content', @@ -107,6 +110,30 @@ class FileProcessor { console.error(`✗ 编译 ${relativePath} 失败 ${error.message}`); } } + + async processLayoutFile(metadata, locale) { + const translatedTitle = metadata.title + ? getTranslation(this.config, metadata.title, locale) + : metadata.title; + metadata.title = translatedTitle; + + this.config.languages = getAvailableLocales(this.config) + + const pageData = { + pageData: { + config: metadata.config, + basePath: getRelativeBasePath('/'), + ...metadata + }, + siteData: { + nav: this.config.nav, + ...this.config + } + } + pageData.siteData.nav = transformNavigation(this.config.nav, this.pages, this.config, locale); + + return await this.templateEngine.renderWithLayout('layouts/page', pageData); + } } module.exports = FileProcessor \ No newline at end of file diff --git a/templates/layouts/404.ejs b/templates/layouts/404.ejs new file mode 100644 index 0000000..f95db17 --- /dev/null +++ b/templates/layouts/404.ejs @@ -0,0 +1,16 @@ +
+
+
+

<%- pageData.title %>

+

<%- pageData.message %>

+ + <%- pageData.backLink %> + +
+
+
\ No newline at end of file diff --git a/templates/layouts/base.ejs b/templates/layouts/base.ejs index 188172f..f5e88ad 100644 --- a/templates/layouts/base.ejs +++ b/templates/layouts/base.ejs @@ -141,6 +141,19 @@ <% } %> + + +