Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1009 from eyworldwide/main
Browse files Browse the repository at this point in the history
fix: excel script links
  • Loading branch information
eyworldwide authored Apr 23, 2024
2 parents 763d5d4 + b43b717 commit ba0d06c
Showing 1 changed file with 52 additions and 17 deletions.
69 changes: 52 additions & 17 deletions scripts/md2excel.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,70 @@ 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 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) {
console.error('Error reading directory:', err);
}
}

async function readMarkdownFile(file, filePath) {
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,
directory: '官方文档',
title,
keywords: '文档$教程',
extends: '',
refs: '',
format: 'Markdown',
content,
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) {
// 中英文表头映射
Expand Down Expand Up @@ -68,7 +102,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!');
}
Expand Down

0 comments on commit ba0d06c

Please sign in to comment.