Skip to content

Commit

Permalink
支持 markdown ejs 模版语法
Browse files Browse the repository at this point in the history
  • Loading branch information
qianmoQ committed Jan 2, 2025
1 parent 13df36f commit 9b2487a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
6 changes: 3 additions & 3 deletions docs/content/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ hero:
title: 创新技术解决方案
description: 为您的企业或网站提供最前沿的技术支持和解决方案
primaryCta:
url: /getting-started/get-started.html
url: /<%= pageData.language %>/getting-started/get-started.html
text: 开始使用
secondaryCta:
url: /usage/href.html
url: /<%= pageData.language %>/usage/href.html
text: 了解更多

features:
Expand Down Expand Up @@ -59,6 +59,6 @@ cta:
title: 准备好开始了吗?
description: 立即加入我们,开启您的技术创新之旅
button:
url: /getting-started/get-started.html
url: /<%= pageData.language %>/getting-started/get-started.html
text: 立即注册
---
66 changes: 63 additions & 3 deletions lib/file-processor.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require('fs');
const path = require('path');
const matter = require('gray-matter');
const ejs = require('ejs');
const marked = require('./extension/marked/pageforge-marked');
const formatTOC = require('./extension/marked/pageforge-toc');
const {
Expand All @@ -27,24 +28,83 @@ class FileProcessor {
this.templateEngine = new TemplateEngine(config.templatePath);
}

// 解析元数据中的 EJS 模板
parseMetadataTemplates(data, context) {
const processed = {...data};

// 检查字符串是否包含 EJS 模板语法
const hasEjsTemplate = (str) => {
return str.includes('<%') && str.includes('%>');
};

// 递归处理对象
const processValue = (value) => {
if (typeof value === 'string') {
// 只有包含 EJS 语法的字符串才进行模板解析
if (hasEjsTemplate(value)) {
try {
return ejs.render(value, context, {
async: false,
cache: false,
filename: context.filename
});
}
catch (error) {
console.warn(`Template parsing error for value "${value}":`, error);
return value;
}
}
// 普通字符串直接返回
return value;
}
else if (Array.isArray(value)) {
return value.map(item => processValue(item));
}
else if (typeof value === 'object' && value !== null) {
const result = {};
for (const [key, val] of Object.entries(value)) {
result[key] = processValue(val);
}
return result;
}
return value;
};

// 处理每个顶层属性
for (const [key, value] of Object.entries(processed)) {
processed[key] = processValue(value);
}

return processed;
}

// 读取并解析 Markdown 文件
processMarkdown(filePath, locale, relativePath) {
const content = fs.readFileSync(filePath, 'utf-8');

const {data, content: markdownContent} = matter(content);
const {data: rawData, content: markdownContent} = matter(content);

const forwardPath = this.getPath(relativePath, locale)
const context = {
pageData: {
...data,
locale: locale,
...rawData,
language: locale,
...forwardPath
},
siteData: {
nav: this.config.nav
}
};

// 解析元数据中的模板
const data = this.parseMetadataTemplates(rawData, context);

// 更新上下文
context.pageData = {
...context.pageData,
...data
};

setContext(context);

const html = marked.parse(markdownContent);
Expand Down

0 comments on commit 9b2487a

Please sign in to comment.