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";
|
|
|
|
|
2016-08-18 08:07:06 +00:00
|
|
|
var TYPE_MAPPINGS_BASE = "$:/config/HighlightPlugin/TypeMappings/";
|
|
|
|
|
2014-02-10 13:51:38 +00:00
|
|
|
var CodeBlockWidget = require("$:/core/modules/widgets/codeblock.js").codeblock;
|
2015-09-11 18:53:06 +00:00
|
|
|
|
|
|
|
var hljs = require("$:/plugins/tiddlywiki/highlight/highlight.js");
|
|
|
|
|
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() {
|
2016-08-18 08:07:06 +00:00
|
|
|
var domNode = this.domNodes[0],
|
|
|
|
language = this.language,
|
|
|
|
tiddler = this.wiki.getTiddler(TYPE_MAPPINGS_BASE + language);
|
2016-08-18 09:32:21 +00:00
|
|
|
if(tiddler) {
|
|
|
|
language = tiddler.fields.text || "";
|
2016-08-18 08:07:06 +00:00
|
|
|
}
|
2017-03-02 09:17:48 +00:00
|
|
|
if(language && hljs.listLanguages().indexOf(language) !== -1) {
|
2017-02-21 13:09:32 +00:00
|
|
|
domNode.className = language.toLowerCase() + " hljs";
|
|
|
|
if($tw.browser && !domNode.isTiddlyWikiFakeDom) {
|
|
|
|
hljs.highlightBlock(domNode);
|
|
|
|
} else {
|
|
|
|
var text = domNode.textContent;
|
|
|
|
domNode.children[0].innerHTML = hljs.fixMarkup(hljs.highlight(language,text).value);
|
|
|
|
// If we're using the fakedom then specially save the original raw text
|
|
|
|
if(domNode.isTiddlyWikiFakeDom) {
|
|
|
|
domNode.children[0].textInnerHTML = text;
|
|
|
|
}
|
2015-01-02 17:00:08 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 13:51:38 +00:00
|
|
|
};
|
2014-01-05 09:58:01 +00:00
|
|
|
|
|
|
|
})();
|