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 #1008 from eyworldwide/main
Browse files Browse the repository at this point in the history
feat: add a generating excel script
  • Loading branch information
eyworldwide authored Apr 19, 2024
2 parents ab7b109 + fa39945 commit 763d5d4
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 6 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ yarn.lock


# playground
playground/.dev/mpa
playground/.dev/mpa

# excel
docs.xlsx
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
"@babel/standalone": "^7.19.3",
"@galacean/editor-ui": "0.0.2",
"@galacean/engine": "^1.2.0-beta.0",
"@galacean/engine-xr": "^1.2.0-alpha",
"@galacean/engine-xr-webxr": "^1.2.0-alpha",
"@galacean/engine-lottie": "^1.1.0-beta.0",
"@galacean/engine-physics-lite": "^1.2.0-beta.0",
"@galacean/engine-physics-physx": "^1.2.0-beta.0",
"@galacean/engine-shader-lab": "^1.2.0-beta.0",
"@galacean/engine-spine": "^1.1.0-beta.21",
"@galacean/engine-toolkit": "^1.2.0-beta.0",
"@galacean/engine-toolkit-input-logger": "^1.2.0-beta.0",
"@galacean/engine-xr": "^1.2.0-alpha",
"@galacean/engine-xr-webxr": "^1.2.0-alpha",
"@galacean/tools-baker": "^1.2.0-beta.0",
"@galacean/engine-shader-lab": "^1.2.0-beta.0",
"@jsdevtools/rehype-toc": "^3.0.2",
"@radix-ui/colors": "^0.1.8",
"@radix-ui/react-icons": "^1.1.1",
Expand Down Expand Up @@ -78,7 +78,8 @@
"dev": "vite --host",
"build": "vite build && node ./scripts/build.js",
"deploy": "gh-pages -d dist",
"playground": "vite serve playground/.dev --config playground/.dev/vite.config.js"
"playground": "vite serve playground/.dev --config playground/.dev/vite.config.js",
"excel": "node ./scripts/md2excel.js"
},
"eslintConfig": {
"extends": [
Expand Down Expand Up @@ -109,8 +110,10 @@
"@types/react-syntax-highlighter": "^15.5.5",
"fs-extra": "^10.1.0",
"gh-pages": "^4.0.0",
"js-yaml": "^4.1.0",
"less": "^4.1.3",
"sass": "^1.55.0"
"sass": "^1.55.0",
"xlsx": "^0.18.5"
},
"babel": {
"presets": [
Expand Down
76 changes: 76 additions & 0 deletions scripts/md2excel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const fs = require('fs').promises;
const path = require('path');
const yaml = require('js-yaml');
const XLSX = require('xlsx');

const data = [];

const reg = /---\n([\s\S]*?)\n---/;

async function readMarkdownFiles(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: ''
})
}
}
} catch (err) {
console.error('Error reading directory:', err);
}
}


function writeExcel(data) {
// 中英文表头映射
const mapping = {
directory: '*类目',
title: '*标题',
keywords: '关键词',
extends: '扩展问法',
refs: '关联知识点',
format: '*知识点格式',
content: '*知识点内容',
remarks: '备注'
};

// 生成映射后的英文表头顺序
const englishHeaders = Object.keys(mapping);
// 生成中文表头数组
const chineseHeaders = englishHeaders.map(key => mapping[key]);

// 生成 JSON 数据表
const worksheet = XLSX.utils.json_to_sheet(data, { header: englishHeaders, skipHeader: true });

// 在工作表上的 'A1' 位置添加中文表头
XLSX.utils.sheet_add_aoa(worksheet, [chineseHeaders], { origin: 'A1' });

// 创建工作簿并添加工作表
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1");
XLSX.writeFile(workbook, 'docs.xlsx');
}


async function main() {
await readMarkdownFiles('docs');
writeExcel(data);
console.log('"docs.xlsx" has been successfully generated!');
}

main();

0 comments on commit 763d5d4

Please sign in to comment.