File size: 4,233 Bytes
e903a32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env node

import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import { copyFileSync } from 'fs';
import { convertLatexToMarkdown } from './latex-converter.mjs';
import { convertToMdx } from './mdx-converter.mjs';
import { cleanBibliography } from './bib-cleaner.mjs';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

// Default configuration
const DEFAULT_INPUT = join(__dirname, 'input', 'main.tex');
const DEFAULT_OUTPUT = join(__dirname, 'output');
const ASTRO_CONTENT_PATH = join(__dirname, '..', '..', 'src', 'content', 'article.mdx');

function parseArgs() {
    const args = process.argv.slice(2);
    const config = {
        input: DEFAULT_INPUT,
        output: DEFAULT_OUTPUT,
        clean: false,
        bibOnly: false,
        convertOnly: false,
        mdx: false,
    };

    for (const arg of args) {
        if (arg.startsWith('--input=')) {
            config.input = arg.split('=')[1];
        } else if (arg.startsWith('--output=')) {
            config.output = arg.split('=')[1];
        } else if (arg === '--clean') {
            config.clean = true;
        } else if (arg === '--bib-only') {
            config.bibOnly = true;
        } else if (arg === '--convert-only') {
            config.convertOnly = true;
        }
    }

    return config;
}

function showHelp() {
    console.log(`
πŸš€ LaTeX to Markdown Toolkit

Usage:
  node index.mjs [options]

Options:
  --input=PATH      Input LaTeX file (default: input/main.tex)
  --output=PATH     Output directory (default: output/)
  --clean           Clean output directory before processing
  --bib-only        Only clean bibliography file
  --convert-only    Only convert LaTeX to Markdown (skip bib cleaning)
  --help, -h        Show this help

Examples:
  # Full conversion with bibliography cleaning
  node index.mjs --clean

  # Only clean bibliography
  node index.mjs --bib-only --input=paper.tex --output=clean/

  # Only convert LaTeX (use existing clean bibliography)
  node index.mjs --convert-only

  # Custom paths
  node index.mjs --input=../paper/main.tex --output=../results/ --clean
`);
}

function main() {
    const args = process.argv.slice(2);

    if (args.includes('--help') || args.includes('-h')) {
        showHelp();
        process.exit(0);
    }

    const config = parseArgs();

    console.log('πŸš€ LaTeX to Markdown Toolkit');
    console.log('==============================');

    try {
        if (config.bibOnly) {
            // Only clean bibliography
            console.log('πŸ“š Bibliography cleaning mode');
            const bibInput = config.input.replace('.tex', '.bib');
            const bibOutput = join(config.output, 'main.bib');

            cleanBibliography(bibInput, bibOutput);
            console.log('πŸŽ‰ Bibliography cleaning completed!');

        } else if (config.convertOnly) {
            // Only convert LaTeX
            console.log('πŸ“„ Conversion only mode');
            convertLatexToMarkdown(config.input, config.output);

        } else {
            // Full workflow
            console.log('πŸ”„ Full conversion workflow');
            convertLatexToMarkdown(config.input, config.output);

            // Convert to MDX if requested
            const markdownFile = join(config.output, 'main.md');
            const mdxFile = join(config.output, 'main.mdx');

            console.log('πŸ“ Converting Markdown to MDX...');
            convertToMdx(markdownFile, mdxFile);

            // Copy MDX to Astro content directory
            console.log('πŸ“‹ Copying MDX to Astro content directory...');
            try {
                copyFileSync(mdxFile, ASTRO_CONTENT_PATH);
                console.log(`    βœ… Copied to ${ASTRO_CONTENT_PATH}`);
            } catch (error) {
                console.warn(`    ⚠️  Failed to copy MDX to Astro: ${error.message}`);
            }
        }

    } catch (error) {
        console.error('❌ Error:', error.message);
        process.exit(1);
    }
}

// Export functions for use as module
export { convertLatexToMarkdown, cleanBibliography };

// Run CLI if called directly
if (import.meta.url === `file://${process.argv[1]}`) {
    main();
}