-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathfulfillQuestion.js
63 lines (60 loc) · 1.6 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
51
52
53
54
55
56
57
58
59
60
61
62
63
import fs from 'node:fs'
import { removeDomTags } from '../functions/removeDomTags.js'
import { getTestCase } from './getTestCase.js'
import { getQuestionUrl } from './getQuestionUrl.js'
import { createMarkdown } from './createMarkdown.js'
import { getQuestionChineseName } from '#common/utils/question-handler/getQuestionChineseName.js'
/**
* @typedef {object} Question
* @property {string} title
* @property {number} date
* @property {string} detail
*/
/**
* @type {Question}
*/
/**
*
* @param {string} data
* @param {Question} question
*
*/
export function generateTemplateContent(data, question) {
return data
.replace(
'@题目',
`${getQuestionChineseName(question)} ${question.date ? `[${question.date}]` : ''}`,
)
.replace(
'@描述',
removeDomTags(question.detail)
.replace('@url', question.url)
.replace(/\n+/g, '\n')
.replaceAll('\n', '\n * '),
)
.replace('// @Function', question.code)
.replace('// @TestCase', getTestCase(question))
.replace('@url', getQuestionUrl(question.slug))
}
/**
* 填充模板文件
* @param questionPath
* @param question
*/
export function fulfillQuestion(questionPath, question) {
return new Promise((resolve) => {
// 开始填充内容
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
resolve()
})
})
})
}