Skip to content

Instantly share code, notes, and snippets.

@PogiNate
Last active October 25, 2021 20:52
Show Gist options
  • Save PogiNate/9d29aa266f40861a817956d7a386d9b2 to your computer and use it in GitHub Desktop.
Save PogiNate/9d29aa266f40861a817956d7a386d9b2 to your computer and use it in GitHub Desktop.
Some simple markdown to BBCode, in TextBuddy
function pre(text){
// Headings
const h1m = /^#\s(.*\b)\s?(#)?$/gm;
const h1b = `[h1]$1[/h1]`;
text = text.replace(h1m,h1b);
const h2m = /^#{2}\s(.*\b)\s?(#{2})?$/gm;
const h2b = `[h2]$1[/h2]`;
text = text.replace(h2m,h2b);
const h3m = /^#{3}\s(.*\b)\s?(#{3})?$/gm;
const h3b = `[h3]$1[/h3]`;
text = text.replace(h3m,h3b);
// bold, italic, strikethrough
//Do Bold first because I'm lazy.
const boldStar = /\*\*(.*?)\*\*/gm;
const boldUnder = /_{2}(.*?)_{2}/gm;
const boldReplace = `[b]$1[/b]`;
text = text.replace(boldStar,boldReplace);
text = text.replace(boldUnder,boldReplace);
//do italics second because then bolds are all out of the game.
const italicStar = /\*{1}(.*?)\b\*{1}/gm;
const italicUnder = /_(.*?)_/gm;
const italicReplace = `[i]$1[/i]`;
text = text.replace(italicStar,italicReplace);
text = text.replace(italicUnder,italicReplace);
const strike = /~~(.*?)~~/gm;
const strikeReplace = `[s]$1[/s]`;
text = text.replace(strike,strikeReplace);
//Quote
const quote = /^>(.*$)/gm;
text = text.replace(quote,`[quote]$1[/quote]`);
//link
const link = /\[(.*)\]\((.*)\)/gm;
const linkRep = `[url:$2|tab]$1[/url]`;
text = text.replace(link,linkRep);
//TODO: ordered lists and unordered lists. But I don't want to.
return text;
}
@PogiNate
Copy link
Author

Updated the link to open links in a new tab.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment