From 761ba0e552c9903c273c5e4591936f2e931c5be3 Mon Sep 17 00:00:00 2001 From: qianmoQ Date: Wed, 1 Jan 2025 20:51:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8A=A0=E8=BD=BD=E6=A8=A1?= =?UTF-8?q?=E7=89=88=E6=95=B0=E6=8D=AE=E6=9C=AA=E4=BC=A0=E9=80=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/content/index.md | 2 +- lib/file-processor.js | 19 ++----- lib/template-engine.js | 105 ++++++++++++++++--------------------- templates/layouts/base.ejs | 4 +- 4 files changed, 51 insertions(+), 79 deletions(-) diff --git a/docs/content/index.md b/docs/content/index.md index 5488b40..9f053a1 100644 --- a/docs/content/index.md +++ b/docs/content/index.md @@ -1,6 +1,6 @@ --- title: 欢迎使用 PageForge -layout: home/modern +layout: home config: sidebar: false diff --git a/lib/file-processor.js b/lib/file-processor.js index 5d793a5..dd627b0 100644 --- a/lib/file-processor.js +++ b/lib/file-processor.js @@ -39,20 +39,6 @@ class FileProcessor { return {metadata: data, content: html}; } - getLayoutPath(layout) { - if (!layout) { - return 'layouts/page'; - } - - // 如果已经包含 layouts/ 前缀,直接返回 - if (layout.startsWith('layouts/')) { - return layout; - } - - // 处理嵌套路径的情况 - return `layouts/${layout}`; - } - async processMarkdownFile(sourceDir, baseDir, filename, locale = '', originalFilePath = baseDir) { const sourcePath = path.join(sourceDir, filename); const relativePath = path.join(baseDir, filename); @@ -98,7 +84,8 @@ class FileProcessor { noLocalePath: isFeatureEnabled(this.config, 'i18n') ? localPath.replace(`/${locale}`, '') : localPath, language: locale, basePath: getRelativeBasePath(baseDir), - layout: this.getLayoutPath(metadata.layout) + layout: metadata.layout || 'layouts/content', + ...metadata }, siteData: { nav: this.config.nav, @@ -108,7 +95,7 @@ class FileProcessor { pageData.siteData.nav = transformNavigation(this.config.nav, this.pages, this.config, locale); // 渲染页面 - const html = await this.templateEngine.renderWithLayout(pageData.pageData.layout, pageData); + const html = await this.templateEngine.renderWithLayout('layouts/page', pageData); const outputFile = path.join(outputDir, outputFilename); fs.writeFileSync(outputFile, html); diff --git a/lib/template-engine.js b/lib/template-engine.js index 2024c8e..ef5e30a 100644 --- a/lib/template-engine.js +++ b/lib/template-engine.js @@ -24,20 +24,21 @@ class TemplateEngine { // 检查内容布局文件是否存在 findContentLayout(layout) { - // 如果未指定布局,使用默认布局 - if (!layout) { - return this.findTemplate('layouts/content'); + // 确保布局路径正确 + if (!layout.startsWith('layouts/')) { + layout = `layouts/${layout}`; } - // 如果布局路径不以 layouts/ 开头,添加前缀 - const layoutPath = layout.startsWith('layouts/') ? layout : `layouts/${layout}`; - try { - return this.findTemplate(layoutPath); - } catch (error) { + return this.findTemplate(layout); + } + catch (error) { // 如果找不到指定布局,使用默认内容布局 - console.warn(`Content layout '${layoutPath}' not found, falling back to 'layouts/content'`); - return this.findTemplate('layouts/content'); + if (layout !== 'layouts/content') { + console.warn(`Content layout '${layout}' not found, falling back to 'layouts/content'`); + return this.findTemplate('layouts/content'); + } + throw error; } } @@ -59,63 +60,47 @@ class TemplateEngine { }, options); } - // 渲染页面内容 - async renderContent(layout, data) { + // 渲染内容布局 + async renderContent(content, data) { + // 获取内容布局名称 + let contentLayout; try { - // 获取内容布局模板路径 - const contentLayoutPath = await this.findContentLayout(layout); - const contentTemplate = fs.readFileSync(contentLayoutPath, 'utf-8'); - - // 渲染内容布局 - const options = { - filename: contentLayoutPath, - views: [ - this.templatePath, - this.defaultTemplatesPath - ] - }; + const requestedLayout = data.layout || 'content'; + contentLayout = requestedLayout; + // 尝试找到内容布局文件 + this.findContentLayout(contentLayout); + } + catch (error) { + // 如果找不到任何内容布局,直接返回原始内容 + console.error(`No content layout found: ${error.message}`); + return content; + } - return ejs.render(contentTemplate, { - ...data, - content: data.content - }, options); - } catch (error) { - console.error('Error rendering content:', error); - // 如果渲染失败,返回原始内容 - return data.content; + // 确保布局路径正确 + if (!contentLayout.startsWith('layouts/')) { + contentLayout = `layouts/${contentLayout}`; } + + // console.log(data.content) + // 渲染内容布局 + return this.render(contentLayout, { + ...data, + content + }); } async renderWithLayout(template, data) { - try { - // 准备渲染数据 - const enhancedData = { - ...data, - page: { - ...data.page || {}, - config: data.config || {} - } - }; - - // 首先渲染内容布局 - const layout = data.pageData?.layout || template; - const renderedContent = await this.renderContent(layout, { - ...enhancedData, - content: data.pageData?.content - }); - - // 更新数据中的内容 - enhancedData.pageData = { - ...enhancedData.pageData, - content: renderedContent - }; + const enhancedData = { + ...data, + page: { + ...data.page || {}, + config: data.config || {} + } + }; - // 最后渲染基础布局 - return this.render('layouts/base', enhancedData); - } catch (error) { - console.error('Error in renderWithLayout:', error); - throw error; - } + return this.render('layouts/base', { + ...enhancedData + }); } } diff --git a/templates/layouts/base.ejs b/templates/layouts/base.ejs index 552211f..188172f 100644 --- a/templates/layouts/base.ejs +++ b/templates/layouts/base.ejs @@ -66,8 +66,8 @@
- <%- include(pageData.layout.startsWith('layouts/') ? pageData.layout : 'content') %> + max-w-none md:max-w-2xl lg:max-w-full"> + <%- include(`${pageData.layout || 'content'}`) %>