Last active
October 25, 2021 20:52
-
-
Save PogiNate/9d29aa266f40861a817956d7a386d9b2 to your computer and use it in GitHub Desktop.
Some simple markdown to BBCode, in TextBuddy
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
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; | |
} |
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
Updated to be less greedy, which allows for multiple matches on a single line.
Regexps!