From 54fac87e99c63a0b047fb4d3b5dd25099f98a253 Mon Sep 17 00:00:00 2001 From: fatchan Date: Tue, 16 Apr 2019 16:34:12 +0000 Subject: [PATCH] capturing groups to regexes, and add code block regex --- helpers/markdown.js | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/helpers/markdown.js b/helpers/markdown.js index 8e9435d9..e056568b 100644 --- a/helpers/markdown.js +++ b/helpers/markdown.js @@ -1,25 +1,24 @@ 'use strict'; const Posts = require(__dirname+'/../db/posts.js') - , greentextRegex = /^>[^>].+/gm - , redtextRegex = /^<[^<].+/gm - , boldRegex = /==.+==/gm - , italicRegex = /__.+__/gm + , greentextRegex = /^>([^>].+)/gm + , redtextRegex = /^<([^<].+)/gm + , boldRegex = /==(.+)==/gm + , italicRegex = /__(.+)__/gm , linkRegex = /https?\:\/\/[^\s]+/g - , spoilerRegex = /\|.+\|/gm; + , spoilerRegex = /\|\|(.+)\|\|/gm + , codeRegex = /^```\s([\s\S]+)\s```/gm; module.exports = (board, thread, text) => { //redtext - text = text.replace(redtextRegex, (match) => { - const red = match.substring(1); - return `<${red}`; + text = text.replace(redtextRegex, (match, redtext) => { + return `<${redtext}`; }); //greentext - text = text.replace(greentextRegex, (match) => { - const green = match.substring(1); - return `>${green}`; + text = text.replace(greentextRegex, (match, greentext) => { + return `>${greentext}`; }); //links @@ -28,23 +27,24 @@ module.exports = (board, thread, text) => { }); //bold - text = text.replace(boldRegex, (match) => { - const bold = match.substring(2, match.length-2); + text = text.replace(boldRegex, (match, bold) => { return `${bold}`; }); //italic - text = text.replace(italicRegex, (match) => { - const italic = match.substring(2, match.length-2); + text = text.replace(italicRegex, (match, italic) => { return `${italic}`; }); //spoilers - text = text.replace(spoilerRegex, (match) => { - const spoiler = match.substring(1, match.length-1); + text = text.replace(spoilerRegex, (match, spoiler) => { return `${spoiler}`; }); + text = text.replace(codeRegex, (match, code) => { + return `${code.trim()}`; + }); + return text; }