capturing groups to regexes, and add code block regex

merge-requests/208/head
fatchan 5 years ago
parent d5870187bc
commit 54fac87e99
  1. 34
      helpers/markdown.js

@ -1,25 +1,24 @@
'use strict'; 'use strict';
const Posts = require(__dirname+'/../db/posts.js') const Posts = require(__dirname+'/../db/posts.js')
, greentextRegex = /^>[^>].+/gm , greentextRegex = /^>([^>].+)/gm
, redtextRegex = /^<[^<].+/gm , redtextRegex = /^<([^<].+)/gm
, boldRegex = /==.+==/gm , boldRegex = /==(.+)==/gm
, italicRegex = /__.+__/gm , italicRegex = /__(.+)__/gm
, linkRegex = /https?\:\/\/[^\s]+/g , linkRegex = /https?\:\/\/[^\s]+/g
, spoilerRegex = /\|.+\|/gm; , spoilerRegex = /\|\|(.+)\|\|/gm
, codeRegex = /^```\s([\s\S]+)\s```/gm;
module.exports = (board, thread, text) => { module.exports = (board, thread, text) => {
//redtext //redtext
text = text.replace(redtextRegex, (match) => { text = text.replace(redtextRegex, (match, redtext) => {
const red = match.substring(1); return `<span class='redtext'>&lt;${redtext}</span>`;
return `<span class='redtext'>&lt;${red}</span>`;
}); });
//greentext //greentext
text = text.replace(greentextRegex, (match) => { text = text.replace(greentextRegex, (match, greentext) => {
const green = match.substring(1); return `<span class='greentext'>&gt;${greentext}</span>`;
return `<span class='greentext'>&gt;${green}</span>`;
}); });
//links //links
@ -28,23 +27,24 @@ module.exports = (board, thread, text) => {
}); });
//bold //bold
text = text.replace(boldRegex, (match) => { text = text.replace(boldRegex, (match, bold) => {
const bold = match.substring(2, match.length-2);
return `<strong>${bold}</strong>`; return `<strong>${bold}</strong>`;
}); });
//italic //italic
text = text.replace(italicRegex, (match) => { text = text.replace(italicRegex, (match, italic) => {
const italic = match.substring(2, match.length-2);
return `<em>${italic}</em>`; return `<em>${italic}</em>`;
}); });
//spoilers //spoilers
text = text.replace(spoilerRegex, (match) => { text = text.replace(spoilerRegex, (match, spoiler) => {
const spoiler = match.substring(1, match.length-1);
return `<span class='spoiler'>${spoiler}</span>`; return `<span class='spoiler'>${spoiler}</span>`;
}); });
text = text.replace(codeRegex, (match, code) => {
return `<span class='code'>${code.trim()}</span>`;
});
return text; return text;
} }

Loading…
Cancel
Save