From 0fdfbe1daefffe06215d46ba814d2cb6f3e015cb Mon Sep 17 00:00:00 2001 From: Mystikfluu Date: Fri, 26 Aug 2022 19:31:50 +0200 Subject: [PATCH] update markdown regex --- js/markdown.js | 62 +++++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/js/markdown.js b/js/markdown.js index 9130f0b..e6f63c0 100644 --- a/js/markdown.js +++ b/js/markdown.js @@ -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) { - 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+_\-\.%=&]*)?)?(#[a-zA-Z0-9!$&'()*+.=-_~:@/?]*)?)(\s+|$)/gi - return text.replace(textregex,'$1 ') + return text.replace(urlregex,'$1 ') } +const newlregex = /(\n)/gi function newlineify(text) { - let textregex = /(\n)/gi - return text.replace(textregex,'
') + return text.replace(newlregex,'
') } +const crossregex = /~([^~]*)~/gi function crossout(text) { - let textregex = /~([^~]*)~/gi - return text.replace(textregex,'$1') + return text.replace(crossregex,'$1') } +const italicregex = /\*([^\*]*)\*/gi function italicify(text) { - let textregex = /\*([^\*]*)\*/gi - return text.replace(textregex,'$1 ') + return text.replace(italicregex,'$1 ') } +const boldregex = /\*\*([^\*]*)\*\*/gi function boldify(text) { - let textregex = /\*\*([^\*]*)\*\*/gi - return text.replace(textregex,'$1 ') + return text.replace(boldregex,'$1 ') } +const mentionregex = /@([^\s]*)/gi 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(textregex,`$1 `) -} -function filterReplies(text) { - let textregex = /_@_([^\s]*)/gi - return text.replace(textregex,`$1 `) + 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) { - text = htmlesc(text) - text = newlineify(text) - text = urlify(text) - //text = filterReplies(text) - text = filterMentions(text) - text = crossout(text) - text = boldify(text) - text = italicify(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}
` + } - return text + out = newlineify(out) + out = urlify(out) + out = filterMentions(out) + out = crossout(out) + out = boldify(out) + out = italicify(out) + + return out + + }); + + return result } /**