Last active
November 25, 2023 17:28
-
-
Save wolfram77/5fed01d177dc09baa458be58675a3abc to your computer and use it in GitHub Desktop.
Convert LaTeX code to Markdown : SCRIPT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const os = require('os'); | |
const fs = require('fs'); | |
// Read a file. | |
function readFile(pth) { | |
var txt = fs.readFileSync(pth, 'utf8'); | |
return txt.replace(/\r\n?/g, '\n'); | |
} | |
// Write a file. | |
function writeFile(pth, txt) { | |
var txt = txt.replace(/\r\n?/g, os.EOL); | |
fs.writeFileSync(pth, txt); | |
} | |
// Convert a LaTeX file to Markdown. | |
function latexToMarkdown(txt) { | |
var txt = txt.replace(/\\href\{(.+?)\}\{(.+?)\}/g, '[$2]($1)'); | |
var txt = txt.replace(/\\textbf\{(.+?)\}/g, '**$1**'); | |
var txt = txt.replace(/\\textit\{(.+?)\}/g, '*$1*'); | |
var txt = txt.replace(/``(.+?)"/g, '"$1"'); | |
var txt = txt.replace(/\$(.+?)\$/g, '`$1`'); | |
return txt; | |
} | |
// Main function. | |
function main() { | |
var pth = process.argv[2]; | |
var txt = readFile(pth); | |
var txt = latexToMarkdown(txt); | |
writeFile(pth, txt); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment