1
1
import fs from 'node:fs'
2
2
import { removeDomTags } from '../functions/removeDomTags.js'
3
3
import { getTestCase } from './getTestCase.js'
4
- import { getQuestionUrl } from './getQuestionUrl.js'
5
4
import { createMarkdown } from './createMarkdown.js'
6
5
import { getQuestionChineseName } from '#common/utils/question-handler/getQuestionChineseName.js'
6
+ import { getConsoleText } from '#common/utils/question-handler/getConsoleText.js'
7
+ import { template } from '#resources/template/template.js'
8
+ import { setBlockComment } from '#common/utils/question-handler/questionLanguage.js'
7
9
8
10
/**
9
11
* @typedef {object } Question
10
12
* @property {string } title
13
+ * @property {string } title
11
14
* @property {number } date
12
15
* @property {string } detail
16
+ * @property {string } lang
17
+ * @property {string } code
13
18
*/
14
19
15
20
/**
@@ -22,42 +27,34 @@ import { getQuestionChineseName } from '#common/utils/question-handler/getQuesti
22
27
* @param {Question } question
23
28
*
24
29
*/
25
- export function generateTemplateContent ( data , question ) {
26
- return data
27
- . replace (
28
- '@题目' ,
29
- `${ getQuestionChineseName ( question ) } ${ question . date ? `[${ question . date } ]` : '' } ` ,
30
- )
31
- . replace (
32
- '@描述' ,
33
- removeDomTags ( question . detail )
34
- . replace ( '@url' , question . url )
35
- . replace ( / \n + / g, '\n' )
36
- . replaceAll ( '\n' , '\n * ' ) ,
37
- )
38
- . replace ( '// @Function' , question . code )
39
- . replace ( '// @TestCase' , getTestCase ( question ) )
40
- . replace ( '@url' , getQuestionUrl ( question . slug ) )
30
+ export function generateTemplateContent ( question ) {
31
+ const title = `${ getQuestionChineseName ( question ) } ${ question . date ? `[${ question . date } ]` : '' } \n`
32
+ const describe = removeDomTags ( question . detail ) . replace ( / \n + / g, '\n' )
33
+ const code = question . code
34
+ return template
35
+ . replace ( '@Title' , setBlockComment ( question . lang , title + describe ) )
36
+ . replace ( '@Describe' , '' )
37
+ . replace ( '@Function' , code )
38
+ . replace ( '@TestCase' , getTestCase ( question ) )
39
+ . replace ( '@Console' , getConsoleText ( question ) )
41
40
}
42
41
/**
43
- * 填充模板文件
42
+ * 模板文件内容替换并生成文件
44
43
* @param questionPath
45
44
* @param question
46
45
*/
47
46
export function fulfillQuestion ( questionPath , question ) {
48
47
return new Promise ( ( resolve ) => {
48
+ // 创建描述文件 md
49
+ createMarkdown ( question . detail , questionPath )
50
+ // 创建填充内容并创建
49
51
// 开始填充内容
50
- fs . readFile ( questionPath , 'utf8' , ( err , data ) => {
52
+ const newData = generateTemplateContent ( question )
53
+ // 创建文件
54
+ fs . writeFile ( questionPath , newData , ( err ) => {
51
55
if ( err )
52
56
throw err
53
- // 修改文件内容
54
- const newData = generateTemplateContent ( data , question )
55
- createMarkdown ( question . detail , questionPath )
56
- fs . writeFile ( questionPath , newData , ( err ) => {
57
- if ( err )
58
- throw err
59
- resolve ( )
60
- } )
57
+ resolve ( )
61
58
} )
62
59
} )
63
60
}
0 commit comments