From 6f55c933a25e153cf6f53cfdd4cb7aa41b1fc62b Mon Sep 17 00:00:00 2001 From: eyworldwide Date: Tue, 23 Apr 2024 15:01:15 +0800 Subject: [PATCH 1/2] feat: support example to excel --- scripts/md2excel.js | 56 ++++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/scripts/md2excel.js b/scripts/md2excel.js index 97fc097e8..561e9fb2e 100644 --- a/scripts/md2excel.js +++ b/scripts/md2excel.js @@ -7,27 +7,16 @@ const data = []; const reg = /---\n([\s\S]*?)\n---/; -async function readMarkdownFiles(dir) { +async function readFiles(dir) { try { const files = await fs.readdir(dir); for (const file of files) { const filePath = path.join(dir, file); const fileStat = await fs.stat(filePath); if (fileStat.isFile() && /\.(md|markdown)$/i.test(path.extname(file))) { - const content = await fs.readFile(filePath, 'utf8'); - const yamlContent = content.match(reg)[1]; - - const { title, type, group } = yaml.load(yamlContent); - data.push({ - directory: group ? `${type}-${group}` : type, - title, - keywords: '', - extends: '', - refs: '', - format: 'markdown', - content: content.replace(reg, ''), - remarks: '' - }) + readMarkdownFile(file ,filePath) + } else if (fileStat.isFile() && /\.(ts)$/i.test(path.extname(file))) { + readTsFile(file, filePath) } } } catch (err) { @@ -35,6 +24,40 @@ async function readMarkdownFiles(dir) { } } +async function readMarkdownFile(file, filePath) { + const content = await fs.readFile(filePath, 'utf8'); + const yamlContent = content.match(reg)[1]; + + const { title, type, group } = yaml.load(yamlContent); + + data.push({ + // directory: group ? `${type}-${group}` : type, + directory: '官方文档', + title, + keywords: '文档$教程', + extends: '', + refs: '', + format: 'Markdown', + content: content.replace(reg, ''), + remarks: `官网文档: https://galacean.antgroup.com/engine/docs/latest/cn/${file.replace(/(\.zh-CN)?\.md$/, '')}` + }) +} + +async function readTsFile(file, filePath) { + const content = await fs.readFile(filePath, 'utf8'); + + data.push({ + directory: '官方文档', + title: file, + keywords: '示例$Demo$Example', + extends: '', + refs: '', + format: 'Markdown', + content, + remarks: `官网示例: https://galacean.antgroup.com/engine/examples/latest/${file.replace(/\.ts$/, '')}` + }) +} + function writeExcel(data) { // 中英文表头映射 @@ -68,7 +91,8 @@ function writeExcel(data) { async function main() { - await readMarkdownFiles('docs'); + await readFiles('docs'); + await readFiles('playground'); writeExcel(data); console.log('"docs.xlsx" has been successfully generated!'); } From b43b717f6c0bf6f74664ab3ad710e78529ec1da0 Mon Sep 17 00:00:00 2001 From: eyworldwide Date: Tue, 23 Apr 2024 15:22:06 +0800 Subject: [PATCH 2/2] fix: excel script links bug --- scripts/md2excel.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/scripts/md2excel.js b/scripts/md2excel.js index 561e9fb2e..8146e3ead 100644 --- a/scripts/md2excel.js +++ b/scripts/md2excel.js @@ -5,7 +5,11 @@ const XLSX = require('xlsx'); const data = []; -const reg = /---\n([\s\S]*?)\n---/; +// yaml content +const yamlReg = /---\n([\s\S]*?)\n---/; +const docLinkReg = /\${docs}/g; +const apiLinkReg = /\${api}/g; +const exampleLinkReg = /\${examples}/g; async function readFiles(dir) { try { @@ -25,10 +29,17 @@ async function readFiles(dir) { } async function readMarkdownFile(file, filePath) { - const content = await fs.readFile(filePath, 'utf8'); - const yamlContent = content.match(reg)[1]; + let content = await fs.readFile(filePath, 'utf8'); + const yamlContent = content.match(yamlReg)[1]; const { title, type, group } = yaml.load(yamlContent); + // remove yaml content + content = content.replace(yamlReg, ''); + + // replace links + content = content.replace(docLinkReg, 'https://galacean.antgroup.com/engine/docs/latest/cn/') + content = content.replace(apiLinkReg, 'https://galacean.antgroup.com/engine/api/latest/') + content = content.replace(exampleLinkReg, 'https://galacean.antgroup.com/engine/examples/latest/') data.push({ // directory: group ? `${type}-${group}` : type, @@ -38,7 +49,7 @@ async function readMarkdownFile(file, filePath) { extends: '', refs: '', format: 'Markdown', - content: content.replace(reg, ''), + content, remarks: `官网文档: https://galacean.antgroup.com/engine/docs/latest/cn/${file.replace(/(\.zh-CN)?\.md$/, '')}` }) }