File tree Expand file tree Collapse file tree 2 files changed +12638
-0
lines changed Expand file tree Collapse file tree 2 files changed +12638
-0
lines changed Original file line number Diff line number Diff line change 1+ const fs = require ( 'fs' ) ;
2+ const path = require ( 'path' ) ;
3+
4+ // Get the input file path from command-line arguments
5+ const inputFilePath = process . argv [ 2 ] ;
6+ if ( ! inputFilePath ) {
7+ console . error ( 'Please provide the path to the input JSON file as an argument.' ) ;
8+ process . exit ( 1 ) ;
9+ }
10+
11+ // Resolve the input file path
12+ const dataPath = path . resolve ( __dirname , inputFilePath ) ;
13+
14+ // Read and parse the JSON file
15+ let data ;
16+ try {
17+ data = require ( dataPath ) ;
18+ } catch ( err ) {
19+ console . error ( `Failed to read or parse the file at ${ dataPath } :` , err . message ) ;
20+ process . exit ( 1 ) ;
21+ }
22+
23+ // Convert the array of objects to Markdown format
24+ const markdownContent = data . map ( obj => {
25+ const titleWithLink = obj . link ? `[${ obj . title } ](${ obj . link } )` : obj . title ;
26+ const tags = obj . level && obj . theme ? `**Tags**: ${ obj . level } , ${ obj . theme } ` : '' ;
27+ const url = obj . url ? `**URL**: [${ obj . url } ](${ obj . url } )` : '' ;
28+ return `## ${ titleWithLink } \n\n${ obj . text } \n\n${ tags } \n\n${ url } \n` ;
29+ } ) . join ( '\n---\n\n' ) ;
30+
31+ // Save the Markdown content to a .md file
32+ const outputFilePath = path . resolve ( __dirname , 'output.md' ) ;
33+ try {
34+ fs . writeFileSync ( outputFilePath , markdownContent , 'utf8' ) ;
35+ console . log ( `Markdown file has been saved to ${ outputFilePath } ` ) ;
36+ } catch ( err ) {
37+ console . error ( `Failed to write to file at ${ outputFilePath } :` , err . message ) ;
38+ process . exit ( 1 ) ;
39+ }
You can’t perform that action at this time.
0 commit comments