Skip to content

Commit

Permalink
Merge pull request #44 from jaredwray/removing-helper-for-markdown
Browse files Browse the repository at this point in the history
removing helper-for-markdown
  • Loading branch information
jaredwray committed Apr 25, 2024
2 parents 26025ee + 23369b6 commit a0affd6
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 5 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,6 @@ Visit the: [code](lib/logging.js) | [unit tests](test/logging.js) | [issues](htt

### [markdown helpers](#markdown)

Visit the: [code](lib/markdown.js) | [unit tests](test/markdown.js) | [issues](https://github.com/jonathas/handlebars-helpers/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+markdown+helpers))

* **[markdown](#markdown)** ([code](lib/markdown.js#Lundefined) | [tests](test/markdown.js#L10))
* **[md](#md)** ([code](lib/markdown.js#L55) | [tests](test/markdown.js#L18))

Expand Down
46 changes: 45 additions & 1 deletion lib/markdown.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
'use strict';
const hljs = require('highlight.js');
const utils = require('handlebars-utils');
const Remarkable = require('remarkable');

/**
* Expose markdown `helpers` (for performance we're using getters so
Expand Down Expand Up @@ -34,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 = require('helper-for-markdown')());
return markdown || (markdown = this.helpersForMarkdown());
}
});

Expand All @@ -53,3 +56,44 @@ Object.defineProperty(helpers, 'markdown', {
*/

helpers.md = require('helper-md');


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);
}

function markdown(str, locals, options) {
if (typeof str !== 'string') {
options = locals;
locals = str;
str = true;
}

if (utils.isOptions(locals)) {
options = locals;
locals = {};
}

const ctx = utils.context(this, locals, options);
let opts = utils.options(this, locals, options);
opts = Object.assign({}, defaults, config, opts);

if (opts.hasOwnProperty('lang')) {
opts.langPrefix = opts.lang;
}

const md = new Remarkable(opts);
const val = utils.value(str, ctx, options);
return md.render(val);
}
return markdown;
};

helpers.highlightFormMarkdown = function(code, lang) {
if(lang) {
return hljs.highlight(code, {language: lang}).value;
}
return hljs.highlightAuto(code).value;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@
"handlebars-utils": "^1.0.6",
"has-value": "^2.0.2",
"helper-date": "^1.0.1",
"helper-for-markdown": "^1.0.2",
"helper-md": "^0.2.2",
"highlight.js": "^11.9.0",
"html-tag": "^2.0.0",
"is-even": "^1.0.0",
"is-glob": "^4.0.3",
Expand Down
30 changes: 29 additions & 1 deletion test/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,37 @@ describe('markdown', function() {
assert.equal(template({title: 'Markdown Test'}), '<h2>Markdown Test</h2>\n');
});
it('should define the object', function() {
markdownHelper.markdown = require('helper-for-markdown')();
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 a0affd6

Please sign in to comment.