Skip to content

Commit

Permalink
adding in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredwray committed Apr 25, 2024
1 parent aeab252 commit beb239a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
24 changes: 8 additions & 16 deletions lib/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
});

Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
}
29 changes: 29 additions & 0 deletions test/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '<span class="hljs-keyword">var</span> foo = <span class="hljs-string">&quot;bar&quot;</span>;');
});
it('test that highlighjs no lang', function() {
var result = markdownHelper.highlightFormMarkdown('var foo = "bar";');
assert.equal(result, 'var foo <span class="hljs-operator">=</span> <span class="hljs-string">&quot;bar&quot;</span><span class="hljs-comment">;</span>');
});
});

describe('md', function() {
Expand Down

0 comments on commit beb239a

Please sign in to comment.