2014-01-05 09:58:01 +00:00
|
|
|
/*\
|
|
|
|
title: $:/plugins/tiddlywiki/highlight/highlightblock.js
|
|
|
|
type: application/javascript
|
2014-01-07 22:57:46 +00:00
|
|
|
module-type: widget
|
2014-01-05 09:58:01 +00:00
|
|
|
|
|
|
|
Wraps up the fenced code blocks parser for highlight and use in TiddlyWiki5
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function() {
|
|
|
|
|
2014-02-10 13:51:38 +00:00
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var CodeBlockWidget = require("$:/core/modules/widgets/codeblock.js").codeblock;
|
2015-01-09 23:02:54 +00:00
|
|
|
//register languages requires hljs for us
|
|
|
|
var hljs = require("$:/plugins/tiddlywiki/highlight/registerlanguages.js").hljs;
|
2015-01-02 17:00:08 +00:00
|
|
|
hljs.configure({tabReplace: " "});
|
2014-11-06 13:27:24 +00:00
|
|
|
|
2014-02-10 13:51:38 +00:00
|
|
|
CodeBlockWidget.prototype.postRender = function() {
|
|
|
|
var domNode = this.domNodes[0];
|
|
|
|
if($tw.browser && this.document !== $tw.fakeDocument && this.language) {
|
2014-03-02 19:42:40 +00:00
|
|
|
domNode.className = this.language.toLowerCase();
|
2014-02-10 13:51:38 +00:00
|
|
|
hljs.highlightBlock(domNode);
|
|
|
|
}
|
2015-01-02 17:00:08 +00:00
|
|
|
else if(!$tw.browser && this.language && this.language.indexOf("/") == -1 ){
|
|
|
|
try{
|
|
|
|
domNode.className = this.language.toLowerCase() + " hljs";
|
|
|
|
domNode.children[0].innerHTML = hljs.fixMarkup(hljs.highlight(this.language, this.getAttribute("code")).value);
|
|
|
|
}
|
|
|
|
catch(err) {
|
2015-01-09 23:02:54 +00:00
|
|
|
//can't easily tell if a language is registered or not in the packed version of hightlight.js
|
|
|
|
//so we silently fail and the codeblock remains unchanged
|
2015-01-02 17:00:08 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 13:51:38 +00:00
|
|
|
};
|
2014-01-05 09:58:01 +00:00
|
|
|
|
|
|
|
})();
|