-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathfulfillQuestion.js
50 lines (48 loc) · 1.37 KB
/
fulfillQuestion.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import fs from 'fs';
import { removeDomTags } from '../functions/removeDomTags.js';
import { getTestCase } from './getTestCase.js';
import { getQuestionUrl } from './getQuestionUrl.js';
import createMarkdown from './createMarkdown.js';
import path from "path";
/**
* @typedef {Object} Question
* @property {string} title
* @property {number} date
* @property {string} detail
*/
/**
* @type {Question}
*/
/**
*
* @param {string} data
* @param {Question} question
*
*/
export const generateTemplateContent = (data, question) =>
data
.replace('@题目', `${question.id}.${question.title} ${question.date ? `[${question.date}]` : ''}`)
.replace('@描述', removeDomTags(question.detail)
.replace('@url', question.url)
.replace(/\n+/g, '\n')
.replaceAll('\n', '\n * '))
.replace('// @Function', question.jsCode)
.replace('// @TestCase', getTestCase(question))
.replace('@url', getQuestionUrl(question.slug));
/**
* 填充模板文件
* @param questionPath
* @param question
*/
export const fulfillQuestion = (questionPath, question) => {
// 开始填充内容
fs.readFile(questionPath, 'utf8', (err, data) => {
if (err) throw err;
// 修改文件内容
const newData = generateTemplateContent(data, question);
createMarkdown(question.detail, questionPath);
fs.writeFile(questionPath, newData, (err) => {
if (err) throw err;
});
});
};