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|ga|xxx|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 allregex = /(```([^```]*)```)|(\n)|(~([^~]*)~)|(\*\*([^\*]*)\*\*)|(\*([^\*]*)\*)|(@[^\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){
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"
return `
${out}
`
}
out = newlineify(out)
out = urlify(out)
out = filterMentions(out)
out = crossout(out)
out = boldify(out)
out = italicify(out)
return out
});
return result
}
/**
* filter out html, as well as render some markdown into html, but without mentions
* @param {string} text text to filter/format
* @return {string} html that represents the filtered text
*/
function filterReply(text) {
text = htmlesc(text)
text = newlineify(text)
text = urlify(text)
text = crossout(text)
text = boldify(text)
text = italicify(text)
return text
}