-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateMarkdown.js
55 lines (49 loc) · 1.85 KB
/
generateMarkdown.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
import fs from 'fs';
import path from 'path';
// Function to generate Markdown content from JSON
function generateMarkdown(json) {
let markdownContent = "# MongoDB Documentation Links\n\n";
for (const key in json.links) {
if (json.links.hasOwnProperty(key)) {
const link = json.links[key];
markdownContent += `## [${key}](${key})\n`;
markdownContent += `**Status:** ${link.status}\n\n`;
// Check if content exists and is not undefined
if (link.content) {
// Split content by new lines and add Markdown formatting
const contentLines = link.content.split('\n').map(line => line.trim()).filter(line => line.length > 0);
contentLines.forEach(line => {
markdownContent += `- ${line}\n`;
});
} else {
markdownContent += `No content available.\n`;
}
markdownContent += '\n';
}
}
return markdownContent;
}
// Read the JSON file from a given path
const filename = process.argv[2];
if (!filename) {
console.error('Please provide a filename as an argument.');
process.exit(1);
}
fs.readFile(filename, 'utf8', (err, data) => {
if (err) {
console.error('Error reading the file:', err);
return;
}
const json = JSON.parse(data);
const markdownContent = generateMarkdown(json);
// Construct new filename with ".md" extension
const newFilename = path.join(path.dirname(filename), path.basename(filename, '.json') + '.md');
// Write the Markdown content to a new file
fs.writeFile(newFilename, markdownContent, (err) => {
if (err) {
console.error('Error writing the Markdown file:', err);
} else {
console.log(`Markdown file has been saved as ${newFilename}.`);
}
});
});