const urlregex =
/(([a-z]+:\/\/)(([a-z0-9\-]+\.)+([a-z]{2}|aero|arpa|app|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel|local|internal|tk|rocks|ga|to))(:[0-9]{1,5})?(\/[a-z0-9_\-\.~]+)*(\/([a-z0-9_\-\.]*)(\?[a-z0-9+_\-\.%=&]*)?)?(#[a-zA-Z0-9!$&'()*+.=-_~:@/?]*)?)(\s+|$)/gi
function urlify(text) {
return text.replace(
urlregex,
'$1 '
)
}
const newlregex = /(\n)/gi
function newlineify(text) {
return text.replace(newlregex, '
')
}
const crossregex = /~([^~]*)~/gi
function crossout(text) {
return text.replace(crossregex, '$1')
}
const italicregex = /\*([^\*]*)\*/gi
function italicify(text) {
return text.replace(italicregex, '$1 ')
}
const boldregex = /\*\*([^\*]*)\*\*/gi
function boldify(text) {
return text.replace(boldregex, '$1 ')
}
const mentionregex = /@([^\s]*)/gi
function filterMentions(text) {
return text.replace(
mentionregex,
`$1 `
)
}
const emojiregex = /:([^:\s]*):/gi
function emojify(text) {
return text.replace(
emojiregex,
""
)
}
function unemojify(text) {
text = text.replace(/\u{1F5FF}/gu, ':moyai:')
text = text.replace(/\u{1F440}/gu, ':eyes:')
return text
}
const allregex =
/(```([^```]*)```)|(\n)|(~([^~]*)~)|(\*\*([^\*]*)\*\*)|(\*([^\*]*)\*)|(@[^\s]*)|(:([^:\s]*):)/gi
const cdblregex = /```([^```]*)```/gi
/**
* filter out html, as well as render some markdown into html
* @param {string} text text to filter/format
* @return {string} html that represents the filtered text
*/
function filterPost(text) {
text = unemojify(text)
let result = htmlesc(text).replace(allregex, function (match) {
let out = match
if (cdblregex.test(match)) {
let paddlen = 3
out = out.substring(paddlen, out.length - paddlen).trim() + '\n'
out = newlineify(out)
return `
${out}