79 lines
2.2 KiB
JavaScript
79 lines
2.2 KiB
JavaScript
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,'<a href="$1" target="_blank" class="insertedlink">$1</a> ')
|
|
}
|
|
|
|
const newlregex = /(\n)/gi
|
|
function newlineify(text) {
|
|
return text.replace(newlregex,' <br>')
|
|
}
|
|
|
|
const crossregex = /~([^~]*)~/gi
|
|
function crossout(text) {
|
|
return text.replace(crossregex,'<span class="crossout">$1</span>')
|
|
}
|
|
|
|
const italicregex = /\*([^\*]*)\*/gi
|
|
function italicify(text) {
|
|
return text.replace(italicregex,'<i>$1</i> ')
|
|
}
|
|
|
|
const boldregex = /\*\*([^\*]*)\*\*/gi
|
|
function boldify(text) {
|
|
return text.replace(boldregex,'<b>$1</b> ')
|
|
}
|
|
|
|
const mentionregex = /@([^\s]*)/gi
|
|
function filterMentions(text) {
|
|
return text.replace(mentionregex,`<span><a href="/users/$1" class="mention">$1</a></span> `)
|
|
}
|
|
|
|
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"
|
|
out = newlineify(out)
|
|
return `<code class="ovfl-bw">${out}</code>`
|
|
}
|
|
|
|
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
|
|
}
|