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';
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 `<span class='redtext'>&lt;${red}</span>`;
text = text.replace(redtextRegex, (match, redtext) => {
return `<span class='redtext'>&lt;${redtext}</span>`;
});
//greentext
text = text.replace(greentextRegex, (match) => {
const green = match.substring(1);
return `<span class='greentext'>&gt;${green}</span>`;
text = text.replace(greentextRegex, (match, greentext) => {
return `<span class='greentext'>&gt;${greentext}</span>`;
});
//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 `<strong>${bold}</strong>`;
});
//italic
text = text.replace(italicRegex, (match) => {
const italic = match.substring(2, match.length-2);
text = text.replace(italicRegex, (match, italic) => {
return `<em>${italic}</em>`;
});
//spoilers
text = text.replace(spoilerRegex, (match) => {
const spoiler = match.substring(1, match.length-1);
text = text.replace(spoilerRegex, (match, spoiler) => {
return `<span class='spoiler'>${spoiler}</span>`;
});
text = text.replace(codeRegex, (match, code) => {
return `<span class='code'>${code.trim()}</span>`;
});
return text;
}

Loading…
Cancel
Save