update markdown regex

This commit is contained in:
Mystikfluu 2022-08-26 19:31:50 +02:00
parent 50ec37884a
commit 0fdfbe1dae

View File

@ -1,53 +1,63 @@
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) { function urlify(text) {
let textregex = /(([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+_\-\.%=&amp;]*)?)?(#[a-zA-Z0-9!$&'()*+.=-_~:@/?]*)?)(\s+|$)/gi return text.replace(urlregex,'<a href="$1" target="_blank" class="insertedlink">$1</a> ')
return text.replace(textregex,'<a href="$1" target="_blank" class="insertedlink">$1</a> ')
} }
const newlregex = /(\n)/gi
function newlineify(text) { function newlineify(text) {
let textregex = /(\n)/gi return text.replace(newlregex,' <br>')
return text.replace(textregex,' <br>')
} }
const crossregex = /~([^~]*)~/gi
function crossout(text) { function crossout(text) {
let textregex = /~([^~]*)~/gi return text.replace(crossregex,'<span class="crossout">$1</span>')
return text.replace(textregex,'<span class="crossout">$1</span>')
} }
const italicregex = /\*([^\*]*)\*/gi
function italicify(text) { function italicify(text) {
let textregex = /\*([^\*]*)\*/gi return text.replace(italicregex,'<i>$1</i> ')
return text.replace(textregex,'<i>$1</i> ')
} }
const boldregex = /\*\*([^\*]*)\*\*/gi
function boldify(text) { function boldify(text) {
let textregex = /\*\*([^\*]*)\*\*/gi return text.replace(boldregex,'<b>$1</b> ')
return text.replace(textregex,'<b>$1</b> ')
} }
const mentionregex = /@([^\s]*)/gi
function filterMentions(text) { function filterMentions(text) {
let textregex = /(@[^\s]*)/gi //if you find an "@" select everything until you find a whitespace (and save as $1) return text.replace(mentionregex,`<span><a href="/users/$1" class="mention">$1</a></span> `)
return text.replace(textregex,`<span><a href="/users/$1" class="mention">$1</a></span> `)
}
function filterReplies(text) {
let textregex = /_@_([^\s]*)/gi
return text.replace(textregex,`<span><a href="/users/$1" class="reply" style="color: pink;">$1</a></span> `)
} }
const allregex = /(```([^```]*)```)|(\n)|(~([^~]*)~)|(\*\*([^\*]*)\*\*)|(\*([^\*]*)\*)|(@[^\s]*)/gi
const cdblregex = /```([^```]*)```/gi
/** /**
* filter out html, as well as render some markdown into html * filter out html, as well as render some markdown into html
* @param {string} text text to filter/format * @param {string} text text to filter/format
* @return {string} html that represents the filtered text * @return {string} html that represents the filtered text
*/ */
function filterPost(text) { function filterPost(text){
text = htmlesc(text) let result = htmlesc(text).replace(allregex, function (match) {
text = newlineify(text) let out = match
text = urlify(text) if(cdblregex.test(match)) {
//text = filterReplies(text) let paddlen = 3
text = filterMentions(text) out = out.substring(paddlen,out.length-paddlen).trim()+"\n"
text = crossout(text) return `<pre><code>${out}</code></pre>`
text = boldify(text) }
text = italicify(text)
return text out = newlineify(out)
out = urlify(out)
out = filterMentions(out)
out = crossout(out)
out = boldify(out)
out = italicify(out)
return out
});
return result
} }
/** /**