feat: 支持自动生成 Markdown
This commit is contained in:
19
scripts/README.md
Normal file
19
scripts/README.md
Normal file
@@ -0,0 +1,19 @@
|
||||
# 脚本
|
||||
|
||||
## 注意事项
|
||||
|
||||
- 需要安装 Node.js 和 NPM。
|
||||
|
||||
## 相关命令
|
||||
|
||||
初始化
|
||||
|
||||
```text
|
||||
npm i
|
||||
```
|
||||
|
||||
开始处理
|
||||
|
||||
```text
|
||||
npm run start
|
||||
```
|
||||
6
scripts/config.js
Normal file
6
scripts/config.js
Normal file
@@ -0,0 +1,6 @@
|
||||
import path from 'path';
|
||||
import process from 'process';
|
||||
|
||||
export const CWD = process.cwd();
|
||||
export const PATH_JSON_FILE = path.join(CWD, '../data.json');
|
||||
export const PATH_MD_FILE = path.join(CWD, '../data.md');
|
||||
62
scripts/generate.js
Normal file
62
scripts/generate.js
Normal file
@@ -0,0 +1,62 @@
|
||||
import { PATH_JSON_FILE, PATH_MD_FILE } from './config.js';
|
||||
|
||||
import fs from 'fs';
|
||||
import json2md from 'json2md';
|
||||
|
||||
/** JSON 转 Markdown */
|
||||
export function generateMarkdown() {
|
||||
try {
|
||||
|
||||
console.info('开始生成 Markdown');
|
||||
|
||||
let jsonContent = fs.readFileSync(PATH_JSON_FILE, {
|
||||
encoding: 'utf-8',
|
||||
});
|
||||
let jsonParsed = JSON.parse(jsonContent);
|
||||
|
||||
/** @type {json2md.DataObject[]} */
|
||||
let jsonMarkdown = [{
|
||||
table: {
|
||||
headers: [
|
||||
'开始时间',
|
||||
'结束时间',
|
||||
'直播内容',
|
||||
],
|
||||
rows: [],
|
||||
},
|
||||
}];
|
||||
|
||||
let tableRows = jsonMarkdown[0].table.rows;
|
||||
let result = ''
|
||||
|
||||
jsonParsed.forEach((data) => {
|
||||
tableRows.push([
|
||||
data.startTime || '',
|
||||
data.endTime || '',
|
||||
data.content || '',
|
||||
]);
|
||||
});
|
||||
|
||||
result = json2md(jsonMarkdown);
|
||||
|
||||
if (result) {
|
||||
fs.writeFileSync(PATH_MD_FILE, result, {
|
||||
encoding: 'utf-8',
|
||||
flag: 'w',
|
||||
});
|
||||
console.info('文件生成完毕');
|
||||
return true;
|
||||
} else {
|
||||
console.error('文件生成失败');
|
||||
return false;
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
|
||||
console.error('文件生成出错:');
|
||||
console.error(error);
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
}
|
||||
4
scripts/index.js
Normal file
4
scripts/index.js
Normal file
@@ -0,0 +1,4 @@
|
||||
import { generateMarkdown } from './generate.js';
|
||||
|
||||
console.clear();
|
||||
generateMarkdown();
|
||||
29
scripts/package-lock.json
generated
Normal file
29
scripts/package-lock.json
generated
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "zimin-live-streaming-timeline-scripts",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "zimin-live-streaming-timeline-scripts",
|
||||
"version": "1.0.0",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"json2md": "^2.0.1"
|
||||
}
|
||||
},
|
||||
"node_modules/indento": {
|
||||
"version": "1.1.13",
|
||||
"resolved": "http://mirrors.tencentyun.com/npm/indento/-/indento-1.1.13.tgz",
|
||||
"integrity": "sha512-YZWk3mreBEM7sBPddsiQnW9Z8SGg/gNpFfscJq00HCDS7pxcQWWWMSVKJU7YkTRyDu1Zv2s8zaK8gQWKmCXHlg=="
|
||||
},
|
||||
"node_modules/json2md": {
|
||||
"version": "2.0.1",
|
||||
"resolved": "http://mirrors.tencentyun.com/npm/json2md/-/json2md-2.0.1.tgz",
|
||||
"integrity": "sha512-VbwmZ83qmUfKBS2pUOHlzNKEZFPBeJSbzEok3trMYyboZUgdHNn1XZfc1uT8UZs1GHCrmRUBXCfqw4YmmQuOhw==",
|
||||
"dependencies": {
|
||||
"indento": "^1.1.13"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
19
scripts/package.json
Normal file
19
scripts/package.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "zimin-live-streaming-timeline-scripts",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "node index.js"
|
||||
},
|
||||
"type": "module",
|
||||
"keywords": [
|
||||
"auto",
|
||||
"script"
|
||||
],
|
||||
"author": "Frost-ZX",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"json2md": "^2.0.1"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user