Skip to content

Commit

Permalink
修复加载模版数据未传递
Browse files Browse the repository at this point in the history
  • Loading branch information
qianmoQ committed Jan 1, 2025
1 parent a08186f commit 761ba0e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 79 deletions.
2 changes: 1 addition & 1 deletion docs/content/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: 欢迎使用 PageForge
layout: home/modern
layout: home

config:
sidebar: false
Expand Down
19 changes: 3 additions & 16 deletions lib/file-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down
105 changes: 45 additions & 60 deletions lib/template-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand All @@ -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
});
}
}

Expand Down
4 changes: 2 additions & 2 deletions templates/layouts/base.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
<!-- 中间内容 -->
<main class="flex-grow w-full mt-4 md:w-2 md:mt-12">
<article class="prose dark:prose-invert lg:prose-lg mx-auto
max-w-none md:max-w-2xl lg:max-w-4xl">
<%- include(pageData.layout.startsWith('layouts/') ? pageData.layout : 'content') %>
max-w-none md:max-w-2xl lg:max-w-full">
<%- include(`${pageData.layout || 'content'}`) %>
</article>
</main>
Expand Down

0 comments on commit 761ba0e

Please sign in to comment.