diff --git a/package.json b/package.json index 02d5ab8..1b9005b 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ "postinstall": "npm run build", "start": "node server.js", "test": "mocha test", - "test:dev": "nodemon -x mocha test || exit 0" + "test:dev": "nodemon -x mocha test || exit 0", + "test:markdown": "nodemon -x mocha test/markdown.test.js || exit 0" }, "author": "stolksdorf", "license": "MIT", diff --git a/shared/homebrewery/markdown.new.js b/shared/homebrewery/markdown.new.js new file mode 100644 index 0000000..fcb8d6e --- /dev/null +++ b/shared/homebrewery/markdown.new.js @@ -0,0 +1,82 @@ +var _ = require('lodash'); +var Markdown = require('marked'); +var renderer = new Markdown.Renderer(); + +//Processes the markdown within an HTML block if it's just a class-wrapper +renderer.html = function (html) { + if(_.startsWith(_.trim(html), '')){ + var openTag = html.substring(0, html.indexOf('>')+1); + html = html.substring(html.indexOf('>')+1); + html = html.substring(0, html.lastIndexOf('')); + return `${openTag} ${Markdown(html)} `; + } + return html; +}; + + +const tagTypes = ['div', 'span', 'a']; +const tagRegex = new RegExp('(' + + _.map(tagTypes, (type)=>{ + return `\\<${type}|\\`; + }).join('|') + ')', 'g'); + + +module.exports = { + marked : Markdown, + render : (rawBrewText)=>{ + return Markdown(rawBrewText, {renderer : renderer}) + }, + + validate : (rawBrewText) => { + var errors = []; + var leftovers = _.reduce(rawBrewText.split('\n'), (acc, line, _lineNumber) => { + var lineNumber = _lineNumber + 1; + var matches = line.match(tagRegex); + if(!matches || !matches.length) return acc; + + _.each(matches, (match)=>{ + _.each(tagTypes, (type)=>{ + if(match == `<${type}`){ + acc.push({ + type : type, + line : lineNumber + }); + } + if(match === ``){ + if(!acc.length){ + errors.push({ + line : lineNumber, + type : type, + text : 'Unmatched closing tag', + id : 'CLOSE' + }); + }else if(_.last(acc).type == type){ + acc.pop(); + }else{ + errors.push({ + line : _.last(acc).line + ' to ' + lineNumber, + type : type, + text : 'Type mismatch on closing tag', + id : 'MISMATCH' + }); + acc.pop(); + } + } + }); + }); + return acc; + }, []); + + _.each(leftovers, (unmatched)=>{ + errors.push({ + line : unmatched.line, + type : unmatched.type, + text : "Unmatched opening tag", + id : 'OPEN' + }) + }); + + return errors; + }, +}; + diff --git a/test/markdown.test.js b/test/markdown.test.js index e69de29..7ab0f8d 100644 --- a/test/markdown.test.js +++ b/test/markdown.test.js @@ -0,0 +1,20 @@ +const testing = require('./test.init.js'); + +const Markdown = require('../shared/homebrewery/markdown.new.js'); + +describe('Markdown', ()=>{ + + it('should do a thing', ()=>{ + + const res = Markdown.render(` + test +
cool stuff
+ test2 + `) + console.log(res); + + + + }); + +}); \ No newline at end of file