From beb239afbb9c735bcb2cb6707a16c36a0f0d543f Mon Sep 17 00:00:00 2001 From: Jared Wray Date: Thu, 25 Apr 2024 06:51:58 -0700 Subject: [PATCH] adding in unit tests --- lib/markdown.js | 24 ++++++++---------------- test/markdown.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/lib/markdown.js b/lib/markdown.js index f2d0135..cdb6ee0 100644 --- a/lib/markdown.js +++ b/lib/markdown.js @@ -37,7 +37,7 @@ Object.defineProperty(helpers, 'markdown', { get: function() { // this is defined as a getter to avoid calling this function // unless the helper is actually used - return markdown || (markdown = helpersForMarkdown()); + return markdown || (markdown = this.helpersForMarkdown()); } }); @@ -58,8 +58,8 @@ Object.defineProperty(helpers, 'markdown', { helpers.md = require('helper-md'); -function helpersForMarkdown(config) { - const defaults = { html: true, breaks: true, highlight: highlight }; +helpers.helpersForMarkdown = function(config) { + const defaults = { html: true, breaks: true, highlight: helpers.highlightFormMarkdown }; if (typeof config === 'string' || utils.isOptions(config)) { return markdown.apply(defaults, arguments); } @@ -91,17 +91,9 @@ function helpersForMarkdown(config) { return markdown; }; -function highlight(code, lang) { - try { - try { - return hljs.highlight(lang, code).value; - } catch (err) { - if (!/Unknown language/i.test(err.message)) { - throw err; - } - return hljs.highlightAuto(code).value; - } - } catch (err) { - return code; - } +helpers.highlightFormMarkdown = function(code, lang) { + if(lang) { + return hljs.highlight(code, {language: lang}).value; + } + return hljs.highlightAuto(code).value; } diff --git a/test/markdown.js b/test/markdown.js index 960e93b..a9be93b 100644 --- a/test/markdown.js +++ b/test/markdown.js @@ -17,6 +17,35 @@ describe('markdown', function() { it('should define the object', function() { assert.equal(typeof markdownHelper.markdown, 'function'); }); + it('should set the object', function() { + markdownHelper.markdown = function() {}; + assert.equal(typeof markdownHelper.markdown, 'function'); + }); + }); + + describe('helpers for markdown', function() { + it('should apply the correct config', function() { + var markdown = markdownHelper.helpersForMarkdown({html: true, breaks: true, highlight: function() {}}); + assert.equal(typeof markdown, 'function'); + var markdownAgain = markdownHelper.helpersForMarkdown('foo'); + assert.equal(typeof markdownAgain, 'string'); + }); + it('should the lang property', function() { + var markdown = markdownHelper.helpersForMarkdown({html: true, breaks: true, highlight: function() {}}); + var md = '## foo\n\n```js\nvar foo = "bar";\n```'; + var locals = {}; + var options = {lang: 'en'}; + markdown(md, locals, options); + assert.equal(typeof markdown, 'function'); + }); + it('test that highlighjs renders', function() { + var result = markdownHelper.highlightFormMarkdown('var foo = "bar";', 'js'); + assert.equal(result, 'var foo = "bar";'); + }); + it('test that highlighjs no lang', function() { + var result = markdownHelper.highlightFormMarkdown('var foo = "bar";'); + assert.equal(result, 'var foo = "bar";'); + }); }); describe('md', function() {