-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
45 lines (37 loc) · 1.47 KB
/
index.js
File metadata and controls
45 lines (37 loc) · 1.47 KB
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
'use strict';
import fs from 'node:fs';
import { convertMarkdown } from './markdown-converter/markdown-converter.js';
const main = () => {
const args = process.argv.slice(2);
const inputPathIndex = args.indexOf('--in');
const outputPathIndex = args.indexOf('--out');
const formatIndex = args.findIndex((arg) => arg.startsWith('--format='));
let format = '';
if (formatIndex !== -1) {
format = args[formatIndex].split('=')[1].toLowerCase();
if (format !== 'html' && format !== 'ansi') {
console.error('Error: Invalid format specified. Please use "html" or "ansi" for --format=');
process.exit(1);
}
}
const inputPath = args[inputPathIndex + 1];
const outputPath = outputPathIndex !== -1 && args.length > outputPathIndex + 1 ? args[outputPathIndex + 1] : '';
if (outputPath && !format) format = 'html';
else if (!format && !outputPath) format = 'ansi';
if (inputPathIndex === -1 || args.length <= inputPathIndex + 1) {
console.error('Error: Input file path not provided');
process.exit(1);
}
try {
const markdownContent = fs.readFileSync(inputPath, 'utf8');
let outputContent;
if (format === 'ansi') outputContent = convertMarkdown(markdownContent);
else outputContent = convertMarkdown(markdownContent, { format: 'html' });
if (outputPath !== '') fs.writeFileSync(outputPath, outputContent, 'utf8');
else console.log(outputContent);
} catch (err) {
console.error(err);
process.exit(1);
}
};
main();