Skip to content
This repository has been archived by the owner on May 5, 2021. It is now read-only.

Commit

Permalink
code highlight disabled if not necessary, close #42
Browse files Browse the repository at this point in the history
  • Loading branch information
demiurgosoft committed Sep 22, 2016
1 parent 0334be5 commit d1d1382
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 12 deletions.
1 change: 0 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"maxerr": 50,
"camelcase": true,
"curly": true,
"eqeqeq": true,
"esversion": 6,
"forin": true,
Expand Down
6 changes: 5 additions & 1 deletion app/parsers/md2html.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ const markdownitSup = require('markdown-it-sup');
const markdownitMark = require('markdown-it-mark');
const markdownitIns = require('markdown-it-ins');


let highlighted;
function highlightRenderer(str, lang) {
highlighted=true;
if (lang && Hljs.getLanguage(lang)) {
try {
return Hljs.highlight(lang, str).value;
Expand All @@ -25,6 +26,7 @@ function highlightRenderer(str, lang) {
}

module.exports = function(content, options, cb) {
highlighted=false;
let config = {
html: true,
linkify: true, //automatic links
Expand All @@ -36,5 +38,7 @@ module.exports = function(content, options, cb) {
.use(markdownitMark)
.use(markdownitIns);
let res = md.render(content);
if(!options.temp) options.temp={};
options.temp.requireHighlight=highlighted;
return cb(null, res);
};
3 changes: 2 additions & 1 deletion app/renderers/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ module.exports = class Renderer {

//Public
renderFile(file, done) {
this.options.temp={}; //resets temporal options
this.beforeLoad(file);
this.fileLoader(file, (err, rawContent) => {
if (err) return done(err);
Expand Down Expand Up @@ -114,7 +115,7 @@ module.exports = class Renderer {
setTemplateOptions() {
return {
styleFile: "github-markdown.css",
highlight: this.options.highlight,
highlight: this.options.highlight && this.options.temp.requireHighlight,
style: this.options.style,
resourcesPath: this.options.resourcesPath,
koala: this.options.koala,
Expand Down
37 changes: 34 additions & 3 deletions test/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ describe("Parsers", function() {
});
});
describe("mM2html", function() {
const rendererOptions = {
highlight: true
};
let rendererOptions;
beforeEach(function() {
rendererOptions = {
highlight: true
};
});

it("Basic test", function(done) {
md2html("example", rendererOptions, function(err, res) {
Expand All @@ -63,6 +66,34 @@ describe("Parsers", function() {
}
cb();
});

it("Highlight temporal option", function(done) {
md2html("example", rendererOptions, function(err, res) {
assert.notOk(err);
assert.ok(res);
assert.ok(rendererOptions.temp);
assert.strictEqual(rendererOptions.temp.requireHighlight, false);
md2html("```\ntest code\n```", rendererOptions, function(err, res) {
assert.notOk(err);
assert.ok(res);
assert.ok(rendererOptions.temp);
assert.strictEqual(rendererOptions.temp.requireHighlight, true);
md2html("example2", rendererOptions, function(err, res) {
assert.notOk(err);
assert.ok(res);
assert.ok(rendererOptions.temp);
assert.strictEqual(rendererOptions.temp.requireHighlight, false);
md2html("`inline code`", rendererOptions, function(err, res) {
assert.notOk(err);
assert.ok(res);
assert.ok(rendererOptions.temp);
assert.strictEqual(rendererOptions.temp.requireHighlight, false);
done();
});
});
});
});
});
});

describe("Html2pdf", function() {
Expand Down
22 changes: 16 additions & 6 deletions test/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,29 @@ describe("Renderers", function() {
assert.match(res, regex.html);
assert.match(res, regex.options.highlightjs);
assert.match(res, regex.options.highlightcss);
let renderer = new HtmlRenderer({
outputFilename: testDir + "/test",
highlight: false
});
renderer.renderFile(testDir + "/" + testFiles[0], function(err) {
renderer.renderFile(testDir + "/" + testFiles[1], function(err) {
assert.notOk(err);
fs.readFile(testDir + "/test.html", "utf8", function(err, res) {
assert.notOk(err);
assert.ok(res);
assert.match(res, regex.html);
assert.notMatch(res, regex.options.highlightjs);
assert.notMatch(res, regex.options.highlightcss);
done();
let renderer = new HtmlRenderer({
outputFilename: testDir + "/test",
highlight: false
});
renderer.renderFile(testDir + "/" + testFiles[0], function(err) {
assert.notOk(err);
fs.readFile(testDir + "/test.html", "utf8", function(err, res) {
assert.notOk(err);
assert.ok(res);
assert.match(res, regex.html);
assert.notMatch(res, regex.options.highlightjs);
assert.notMatch(res, regex.options.highlightcss);
done();
});
});
});
});
});
Expand Down

0 comments on commit d1d1382

Please sign in to comment.