diff --git a/lib/output/modifiers/__tests__/highlightCode.js b/lib/output/modifiers/__tests__/highlightCode.js new file mode 100644 index 000000000..bd7d42202 --- /dev/null +++ b/lib/output/modifiers/__tests__/highlightCode.js @@ -0,0 +1,63 @@ +jest.autoMockOff(); + +var cheerio = require('cheerio'); +var Promise = require('../../../utils/promise'); + +describe('highlightCode', function() { + var highlightCode = require('../highlightCode'); + + function doHighlight(lang, code) { + return { + text: '' + (lang || '') + '$' + code + }; + } + + function doHighlightAsync(lang, code) { + return Promise() + .then(function() { + return doHighlight(lang, code); + }); + } + + pit('should call it for normal code element', function() { + var $ = cheerio.load('
This is a test
test');
+
+ return highlightCode(doHighlight, $)
+ .then(function() {
+ var $code = $('code');
+ expect($code.text()).toBe('js$test');
+ });
+ });
+
+ pit('should call it for asciidoc code block', function() {
+ var $ = cheerio.load('test');
+
+ return highlightCode(doHighlight, $)
+ .then(function() {
+ var $code = $('code');
+ expect($code.text()).toBe('python$test');
+ });
+ });
+
+ pit('should accept async highlighter', function() {
+ var $ = cheerio.load('test');
+
+ return highlightCode(doHighlightAsync, $)
+ .then(function() {
+ var $code = $('code');
+ expect($code.text()).toBe('python$test');
+ });
+ });
+});
+
+
diff --git a/lib/output/modifiers/editHTMLElement.js b/lib/output/modifiers/editHTMLElement.js
index 9897dcc7e..755598e8c 100644
--- a/lib/output/modifiers/editHTMLElement.js
+++ b/lib/output/modifiers/editHTMLElement.js
@@ -8,7 +8,7 @@ function editHTMLElement($, selector, fn) {
return Promise.forEach($elements, function(el) {
var $el = $(el);
- fn($el);
+ return fn($el);
});
}
diff --git a/lib/output/modifiers/highlightCode.js b/lib/output/modifiers/highlightCode.js
new file mode 100644
index 000000000..deabd6817
--- /dev/null
+++ b/lib/output/modifiers/highlightCode.js
@@ -0,0 +1,44 @@
+var Promise = require('../../utils/promise');
+var editHTMLElement = require('./editHTMLElement');
+
+/**
+ Highlight all code elements
+
+ @param {Function(lang, body) -> String} highlight
+ @param {HTMLDom} $
+ @return {Promise}
+*/
+function highlightCode(highlight, $) {
+ return editHTMLElement($, 'code', function($code) {
+ var classNames = ($code.attr('class') || '').split(' ');
+ var lang = classNames
+ .map(function(cl) {
+ // Markdown
+ if (cl.search('lang-') === 0) {
+ return cl.slice('lang-'.length);
+ }
+
+ // Asciidoc
+ if (cl.search('language-') === 0) {
+ return cl.slice('language-'.length);
+ }
+
+ return null;
+ })
+ .find(function(cl) {
+ return Boolean(cl);
+ });
+ var source = $code.text();
+
+ return Promise(highlight(lang, source))
+ .then(function(r) {
+ if (r.html) {
+ $code.html(r.html);
+ } else {
+ $code.text(r.text);
+ }
+ });
+ });
+}
+
+module.exports = highlightCode;
diff --git a/lib/output/modifiers/index.js b/lib/output/modifiers/index.js
index 75b59274a..4cdb01bce 100644
--- a/lib/output/modifiers/index.js
+++ b/lib/output/modifiers/index.js
@@ -9,5 +9,6 @@ module.exports = {
fetchRemoteImages: require('./fetchRemoteImages'),
svgToPng: require('./svgToPng'),
resolveLinks: require('./resolveLinks'),
- annotateText: require('./annotateText')
+ annotateText: require('./annotateText'),
+ highlightCode: require('./highlightCode')
};