summaryrefslogtreecommitdiffstats
path: root/lib/output/modifiers/highlightCode.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/output/modifiers/highlightCode.js')
-rw-r--r--lib/output/modifiers/highlightCode.js48
1 files changed, 30 insertions, 18 deletions
diff --git a/lib/output/modifiers/highlightCode.js b/lib/output/modifiers/highlightCode.js
index deabd68..dcd9d24 100644
--- a/lib/output/modifiers/highlightCode.js
+++ b/lib/output/modifiers/highlightCode.js
@@ -1,7 +1,35 @@
+var is = require('is');
var Promise = require('../../utils/promise');
var editHTMLElement = require('./editHTMLElement');
/**
+ Return language for a code blocks from a list of class names
+
+ @param {Array<String>}
+ @return {String}
+*/
+function getLanguageForClass(classNames) {
+ return 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);
+ });
+}
+
+
+/**
Highlight all code elements
@param {Function(lang, body) -> String} highlight
@@ -11,28 +39,12 @@ var editHTMLElement = require('./editHTMLElement');
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 lang = getLanguageForClass(classNames);
var source = $code.text();
return Promise(highlight(lang, source))
.then(function(r) {
- if (r.html) {
+ if (is.string(r.html)) {
$code.html(r.html);
} else {
$code.text(r.text);