Skip to content

Instantly share code, notes, and snippets.

@wolfram77
Last active November 25, 2023 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolfram77/5fed01d177dc09baa458be58675a3abc to your computer and use it in GitHub Desktop.
Save wolfram77/5fed01d177dc09baa458be58675a3abc to your computer and use it in GitHub Desktop.
Convert LaTeX code to Markdown : SCRIPT
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