move diceroll to other file and fix markdown for image

merge-requests/208/head
fatchan 5 years ago
parent 7dbfbb4eac
commit 92ad72cb5c
  1. 31
      helpers/posting/diceroll.js
  2. 35
      helpers/posting/markdown.js
  3. 5
      helpers/posting/sanitizeoptions.js

@ -0,0 +1,31 @@
'use strict';
module.exports = (match, numdice, numsides, operator, modifier) => {
numdice = parseInt(numdice);
if (numdice > 100) {
numdice = 100;
} else if (numdice <= 0) {
numdice = 1;
}
numsides = parseInt(numsides);
if (numsides > 100) {
numsides = 100;
} else if (numsides <= 0) {
numsides = 1;
}
let sum = 0;
for (let i = 0; i < numdice; i++) {
const roll = Math.floor(Math.random() * numsides)+1;
sum += roll;
}
if (modifier && operator) {
modifier = parseInt(modifier);
//do i need to make sure it doesnt go negative or maybe give absolute value?
if (operator === '+') {
sum += modifier;
} else {
sum -= modifier;
}
}
return `\n<img src='/img/dice.png' height='16' width='16' /><span class='dice'>(${match}) Rolled ${numdice} dice with ${numsides} sides${modifier ? ' and modifier '+operator+modifier : '' } = ${sum}</span>\n`;
}

@ -13,6 +13,7 @@ const greentextRegex = /^&gt;((?!&gt;).+)/gm
, linkRegex = /https?\:&#x2F;&#x2F;[^\s<>\[\]{}|\\^]+/g
, codeRegex = /&#x60;&#x60;&#x60;([\s\S]+?)&#x60;&#x60;&#x60;/gm
, diceRegex = /##(?<numdice>\d+)d(?<numsides>\d+)(?:(?<operator>[+-])(?<modifier>\d+))?/gmi
, diceRoll = require(__dirname+'/diceroll.js')
module.exports = (text) => {
@ -76,39 +77,9 @@ module.exports = (text) => {
return `<span class='detected'>${detected}</span>`;
});
//detected
text = text.replace(diceRegex, (match, numdice, numsides, operator, modifier) => {
numdice = parseInt(numdice);
if (numdice > 100) {
numdice = 100;
} else if (numdice <= 0) {
numdice =1;
}
numsides = parseInt(numsides);
if (numsides > 100) {
numsides = 100;
} else if (numsides <= 0) {
numsides = 1;
}
let sum = 0;
for (let i = 0; i < numdice; i++) {
const roll = Math.floor(Math.random() * numsides)+1;
sum += roll;
}
if (modifier && operator) {
modifier = parseInt(modifier);
//do i need to make sure it doesnt go negative or maybe give absolute value?
if (operator === '+') {
sum += modifier;
} else {
sum -= modifier;
}
}
return `\n<span class='dice'>(${match}) Rolled ${numdice} dice with ${numsides} sides${modifier ? ' and modifier '+operator+modifier : '' } = ${sum}</span>\n`
});
//dice rolls
text = text.replace(diceRegex, diceRoll);
return text;
}

@ -8,10 +8,11 @@ module.exports = {
},
after: {
allowedTags: [ 'span', 'a', 'em', 'strong', 'small' ],
allowedTags: [ 'span', 'a', 'em', 'strong', 'img'],
allowedAttributes: {
'a': [ 'href', 'class', 'referrerpolicy', 'target' ],
'span': [ 'class' ]
'span': [ 'class' ],
'img': ['src', 'height', 'width']
}
}

Loading…
Cancel
Save