diff --git a/helpers/posting/linkmatch.js b/helpers/posting/linkmatch.js new file mode 100644 index 00000000..757791b8 --- /dev/null +++ b/helpers/posting/linkmatch.js @@ -0,0 +1,23 @@ +'use strict'; + +const parenPairRegex = /\((?:[^)(]+|\((?:[^)(]+|\([^)(]\))*\))*\)/g + +module.exports = (match) => { + let trimmedMatch; + let excess = ''; + const parensPairs = match.match(parenPairRegex); + //naive solution + if (parensPairs) { + const lastMatch = parensPairs[parensPairs.length-1]; + const lastIndex = match.lastIndexOf(lastMatch); + trimmedMatch = match.substring(0, lastIndex+lastMatch.length); + excess = match.substring(lastIndex+lastMatch.length); + } else { + trimmedMatch = match.substring(0, match.indexOf(')')); + excess = match.substring(match.indexOf(')')); + } + trimmedMatch = trimmedMatch + .replace(/\(/g, '%28') + .replace(/\)/g, '%29'); + return `${trimmedMatch}${excess}`; +}; diff --git a/helpers/posting/markdown.js b/helpers/posting/markdown.js index 50578bbc..4438bfc9 100644 --- a/helpers/posting/markdown.js +++ b/helpers/posting/markdown.js @@ -22,16 +22,16 @@ const greentextRegex = /^>((?!>\d+|>>/\w+(/\d*)?).*)/gm , replacements = [ { regex: pinktextRegex, cb: (match, pinktext) => `<${pinktext}` }, { regex: greentextRegex, cb: (match, greentext) => `>${greentext}` }, - { regex: boldRegex, cb: (match, bold) => `${bold}` }, + { regex: boldRegex, cb: (match, bold) => `${bold}` }, { regex: underlineRegex, cb: (match, underline) => `${underline}` }, - { regex: strikeRegex, cb: (match, strike) => `${strike}` }, - { regex: titleRegex, cb: (match, title) => `${title}` }, - { regex: italicRegex, cb: (match, italic) => `${italic}` }, + { regex: strikeRegex, cb: (match, strike) => `${strike}` }, + { regex: titleRegex, cb: (match, title) => `${title}` }, + { regex: italicRegex, cb: (match, italic) => `${italic}` }, { regex: spoilerRegex, cb: (match, spoiler) => `${spoiler}` }, - { regex: monoRegex, cb: (match, mono) => `${mono}` }, + { regex: monoRegex, cb: (match, mono) => `${mono}` }, + { regex: linkRegex, cb: require(__dirname+'/linkmatch.js') }, { regex: detectedRegex, cb: (match, detected) => `${detected}` }, - { regex: linkRegex, cb: (match) => `${match}` }, - { regex: diceRegex, cb: require(__dirname+'/diceroll.js') }, + { regex: diceRegex, cb: require(__dirname+'/diceroll.js') }, ]; module.exports = {