1+ const fs = require ( 'fs' ) ;
2+
3+ // Read the input JSON from dead_links.json file
4+ fs . readFile ( 'dead_links.json' , 'utf8' , ( err , data ) => {
5+ if ( err ) {
6+ console . error ( 'Error reading the file:' , err ) ;
7+ return ;
8+ }
9+
10+ // Parse the JSON data
11+ let input ;
12+ try {
13+ input = JSON . parse ( data ) ;
14+ } catch ( parseErr ) {
15+ console . error ( 'Error parsing JSON:' , parseErr ) ;
16+ return ;
17+ }
18+
19+ // Group todos by category
20+ const categories = { } ;
21+
22+ // Iterate over each item to classify them by category
23+ input . forEach ( item => {
24+ const categoryMatch = item . file . match ( / \. \/ c o n t e n t \/ ( \w + ) \/ / ) ;
25+ let category = categoryMatch ? categoryMatch [ 1 ] : 'other' ;
26+
27+ if ( ! categories [ category ] ) {
28+ categories [ category ] = [ ] ;
29+ }
30+
31+ // Add the todo to the corresponding category
32+ categories [ category ] . push ( `- [ ] [${ item . status_code } ] ${ item . link } in ${ item . file } ` ) ;
33+ } ) ;
34+
35+ // Create the output text
36+ let output = '' ;
37+
38+ Object . keys ( categories ) . forEach ( category => {
39+ output += `## ${ category } \n\n` ;
40+ output += categories [ category ] . join ( '\n' ) + '\n\n' ;
41+ } ) ;
42+
43+ // Write output to a text file
44+ fs . writeFile ( 'todos.md' , output , writeErr => {
45+ if ( writeErr ) {
46+ console . error ( 'Error writing the file:' , writeErr ) ;
47+ return ;
48+ }
49+
50+ console . log ( 'Todos have been successfully written to todos.md' ) ;
51+ } ) ;
52+
53+ // Output the result in the console as well
54+ console . log ( output ) ;
55+ } ) ;
0 commit comments