summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-04-27 15:42:55 +0200
committerSamy Pessé <samypesse@gmail.com>2016-04-27 15:42:55 +0200
commit999882e72327e06dd2fd346ca13eccb1c7e8781f (patch)
tree6a9ff7102871dfb4106d47e35c5d3031f787b90b /lib
parentd68dc82159555833b364a1571fff4b630c6a5bd4 (diff)
downloadgitbook-999882e72327e06dd2fd346ca13eccb1c7e8781f.zip
gitbook-999882e72327e06dd2fd346ca13eccb1c7e8781f.tar.gz
gitbook-999882e72327e06dd2fd346ca13eccb1c7e8781f.tar.bz2
Add test for highlightCode html modifier
Diffstat (limited to 'lib')
-rw-r--r--lib/output/modifiers/__tests__/highlightCode.js63
-rw-r--r--lib/output/modifiers/editHTMLElement.js2
-rw-r--r--lib/output/modifiers/highlightCode.js44
-rw-r--r--lib/output/modifiers/index.js3
4 files changed, 110 insertions, 2 deletions
diff --git a/lib/output/modifiers/__tests__/highlightCode.js b/lib/output/modifiers/__tests__/highlightCode.js
new file mode 100644
index 0000000..bd7d422
--- /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('<p>This is a <code>test</code></p>');
+
+ return highlightCode(doHighlight, $)
+ .then(function() {
+ var $code = $('code');
+ expect($code.text()).toBe('$test');
+ });
+ });
+
+ pit('should call it for markdown code block', function() {
+ var $ = cheerio.load('<pre><code class="lang-js">test</code></pre>');
+
+ 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('<pre><code class="language-python">test</code></pre>');
+
+ return highlightCode(doHighlight, $)
+ .then(function() {
+ var $code = $('code');
+ expect($code.text()).toBe('python$test');
+ });
+ });
+
+ pit('should accept async highlighter', function() {
+ var $ = cheerio.load('<pre><code class="language-python">test</code></pre>');
+
+ 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 9897dcc..755598e 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 0000000..deabd68
--- /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 75b5927..4cdb01b 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')
};