0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-13 15:12:43 +00:00

Getting both renderers to play nice

This commit is contained in:
Scott Tolksdorf
2017-02-12 23:35:19 -05:00
parent b40e5bc4c4
commit 304cd0ffcd
22 changed files with 136 additions and 82 deletions

View File

@@ -2,41 +2,43 @@ const _ = require('lodash');
const Markdown = require('marked');
const renderer = new Markdown.Renderer();
let blockCount = 0;
renderer.paragraph = function(text){
const blockReg = /{{[\w|,]+|}}/g;
const matches = text.match(blockReg);
if(!matches) return `\n<p>${text}</p>\n`;
let matchIndex = 0;
const res = _.reduce(text.split(blockReg), (r, text) => {
if(text) r.push(`\n<p>${text}</p>\n`);
const block = matches[matchIndex];
if(block && _.startsWith(block, '{{')){
r.push(`\n\n<div class="${block.substring(2).split(',').join(' ')}">`);
blockCount++;
}
if(block == '}}' && blockCount !== 0){
r.push('</div>\n\n');
blockCount--;
}
matchIndex++;
return r;
}, []).join('\n');
return res;
};
module.exports = {
marked : Markdown,
render : (rawBrewText)=>{
//Adds in the new div block syntax
let count = 0;
let blockReg = /{{[\w|,]+|}}/g;
const renderer = new Markdown.Renderer();
renderer.paragraph = function (text) {
const matches = text.match(blockReg);
if(!matches) return `\n<p>${text}</p>\n`;
let matchIndex = 0;
const res = _.reduce(text.split(blockReg), (r, text) => {
if(text) r.push(`\n<p>${text}</p>\n`);
const block = matches[matchIndex];
if(block && _.startsWith(block, '{{')){
r.push(`\n\n<div class="${block.substring(2).split(',').join(' ')}">`);
count++;
}
if(block == '}}' && count !== 0){
r.push('</div>\n\n');
count--;
}
matchIndex++;
return r;
}, []).join('\n');
return res;
};
blockCount = 0;
let html = Markdown(rawBrewText, {renderer : renderer, sanitize: true});
html += _.times(count, ()=>{return '</div>'}).join('\n');
//Close all hanging block tags
html += _.times(blockCount, ()=>{return '</div>'}).join('\n');
return html;
},
validate : (rawBrewText) => {
return [];
},
};