1
1
const fs = require ( 'fs' ) ;
2
2
const path = require ( 'path' ) ;
3
- const repo = '/Users/vitaliisemianchuk/Projects /javascript-questions-pro' ;
3
+ const repo = '../.. /javascript-questions-pro' ;
4
4
5
5
// Get the input file path from command-line arguments
6
6
const inputFilePath = process . argv [ 2 ] ;
@@ -38,11 +38,23 @@ function writeToFile(folderPath, fileName, content) {
38
38
fs . writeFileSync ( filePath , content , 'utf8' ) ;
39
39
}
40
40
41
+ // Sanitize names to replace spaces and special characters with underscores and convert to lowercase
42
+ function sanitizeName ( name ) {
43
+ return name . replace ( / [ ^ a - z 0 - 9 ] + / gi, '_' ) . toLowerCase ( ) ;
44
+ }
45
+
41
46
// Generate Markdown content for a question
42
47
function generateMarkdown ( obj ) {
43
48
const titleWithLink = obj . link ? `[${ obj . title } ](${ obj . link } )` : obj . title ;
44
- const tags = obj . level && obj . theme ? `**Tags**: ${ obj . level } , ${ obj . theme } ` : '' ;
49
+
50
+ // Create links for level and theme tags
51
+ const levelLink = obj . level ? `[${ obj . level } ](./level/${ sanitizeName ( obj . level ) } )` : '' ;
52
+ const themeLinks = obj . theme ? obj . theme . split ( ',' ) . map ( theme => `[${ theme . trim ( ) } ](./theme/${ sanitizeName ( theme . trim ( ) ) } )` ) . join ( ', ' ) : '' ;
53
+
54
+ const tags = ( levelLink || themeLinks ) ? `**Tags**: ${ levelLink } ${ levelLink && themeLinks ? ', ' : '' } ${ themeLinks } ` : '' ;
55
+
45
56
const url = obj . url ? `**URL**: [${ obj . url } ](${ obj . url } )` : '' ;
57
+
46
58
return `## ${ titleWithLink } \n\n${ obj . text } \n\n${ tags } \n\n${ url } \n` ;
47
59
}
48
60
@@ -53,10 +65,11 @@ clearFolder(levelsFolder);
53
65
const uniqueLevels = new Set ( ) ;
54
66
data . forEach ( obj => {
55
67
if ( obj . level ) {
56
- uniqueLevels . add ( obj . level ) ;
57
- const levelFolderPath = path . join ( levelsFolder , obj . level ) ;
68
+ const sanitizedLevel = sanitizeName ( obj . level ) ; // Ensure level is lowercase
69
+ uniqueLevels . add ( sanitizedLevel ) ;
70
+ const levelFolderPath = path . join ( levelsFolder , sanitizedLevel ) ; // Ensure folder path is lowercase
58
71
const content = generateMarkdown ( obj ) ;
59
- writeToFile ( levelFolderPath , `${ obj . title . replace ( / [ ^ a - z 0 - 9 ] + / gi , '_' ) } .md` , content ) ;
72
+ writeToFile ( levelFolderPath , `${ sanitizeName ( obj . title ) } .md` , content ) ; // Ensure file name is lowercase
60
73
}
61
74
} ) ;
62
75
@@ -67,12 +80,12 @@ clearFolder(themesFolder);
67
80
const uniqueThemes = new Set ( ) ;
68
81
data . forEach ( obj => {
69
82
if ( obj . theme ) {
70
- const themes = obj . theme . split ( ',' ) . map ( theme => theme . trim ( ) ) ;
83
+ const themes = obj . theme . split ( ',' ) . map ( theme => sanitizeName ( theme . trim ( ) ) ) ; // Ensure theme is lowercase
71
84
themes . forEach ( theme => {
72
85
uniqueThemes . add ( theme ) ;
73
- const themeFolderPath = path . join ( themesFolder , theme ) ;
86
+ const themeFolderPath = path . join ( themesFolder , theme ) ; // Ensure folder path is lowercase
74
87
const content = generateMarkdown ( obj ) ;
75
- writeToFile ( themeFolderPath , `${ obj . title . replace ( / [ ^ a - z 0 - 9 ] + / gi , '_' ) } .md` , content ) ;
88
+ writeToFile ( themeFolderPath , `${ sanitizeName ( obj . title ) } .md` , content ) ; // Ensure file name is lowercase
76
89
} ) ;
77
90
}
78
91
} ) ;
@@ -86,17 +99,17 @@ data.forEach(obj => {
86
99
if ( obj . url && obj . url . includes ( 'tiktok' ) ) {
87
100
videoQuestions . push ( obj ) ;
88
101
const content = generateMarkdown ( obj ) ;
89
- writeToFile ( videoFolder , `${ obj . title . replace ( / [ ^ a - z 0 - 9 ] + / gi , '_' ) } .md` , content ) ;
102
+ writeToFile ( videoFolder , `${ sanitizeName ( obj . title ) } .md` , content ) ; // Ensure file name is lowercase
90
103
}
91
104
} ) ;
92
105
93
106
// Generate the README header
94
107
const totalQuestions = data . length ;
95
108
const levelsLinks = Array . from ( uniqueLevels )
96
- . map ( level => `- [${ level } ](./level/${ level } )` )
109
+ . map ( level => `- [${ level . replace ( / _ / g , ' ' ) } ](./level/${ level } )` )
97
110
. join ( '\n' ) ;
98
111
const themesLinks = Array . from ( uniqueThemes )
99
- . map ( theme => `- [${ theme } ](./theme/${ theme } )` )
112
+ . map ( theme => `- [${ theme . replace ( / _ / g , ' ' ) } ](./theme/${ theme } )` )
100
113
. join ( '\n' ) ;
101
114
const videosLinks = videoQuestions
102
115
. map ( q => `- [${ q . title } ](${ q . url } )` )
@@ -105,9 +118,16 @@ const videosLinks = videoQuestions
105
118
// Convert the array of objects to Markdown format
106
119
const markdownContent = data . map ( obj => {
107
120
const titleWithLink = obj . link ? `[${ obj . title } ](${ obj . link } )` : obj . title ;
108
- const tags = obj . level && obj . theme ? `**Tags**: ${ obj . level } , ${ obj . theme } ` : '' ;
121
+
122
+ // Create links for level and theme tags
123
+ const levelLink = obj . level ? `[${ obj . level } ](./level/${ sanitizeName ( obj . level ) } )` : '' ;
124
+ const themeLinks = obj . theme ? obj . theme . split ( ',' ) . map ( theme => `[${ theme . trim ( ) } ](./theme/${ sanitizeName ( theme . trim ( ) ) } )` ) . join ( ', ' ) : '' ;
125
+
126
+ const tags = ( levelLink || themeLinks ) ? `**Tags**: ${ levelLink } ${ levelLink && themeLinks ? ', ' : '' } ${ themeLinks } ` : '' ;
127
+
109
128
const url = obj . url ? `**URL**: [${ obj . url } ](${ obj . url } )` : '' ;
110
- return `## ${ titleWithLink } \n\n${ obj . text } \n\n${ tags } \n\n${ url } \n` ;
129
+
130
+ return `${ obj . text } \n\n${ tags } \n\n${ url } \n` ;
111
131
} ) . join ( '\n---\n\n' ) ;
112
132
113
133
const readmeContent = `
0 commit comments