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");
|
|
|
|
|
2022-02-21 15:35:13 +00:00
|
|
|
if(hljs.getLanguage !== undefined) {
|
|
|
|
// load language definitions
|
|
|
|
$tw.utils.each($tw.modules.types["highlight"],function(moduleInfo,moduleName) {
|
|
|
|
$tw.utils.evalSandboxed(moduleInfo.definition,{hljs:hljs, exports:{}},moduleName);
|
|
|
|
});
|
|
|
|
|
|
|
|
CodeBlockWidget.prototype.postRender = function() {
|
|
|
|
var domNode = this.domNodes[0],
|
|
|
|
language = this.language,
|
|
|
|
tiddler = this.wiki.getTiddler(TYPE_MAPPINGS_BASE + language);
|
|
|
|
if(tiddler) {
|
|
|
|
language = tiddler.fields.text || "";
|
|
|
|
}
|
|
|
|
if(language && hljs.getLanguage(language)) {
|
|
|
|
domNode.className = "hljs";
|
|
|
|
domNode.children[0].className = language.toLowerCase() + " hljs";
|
|
|
|
if($tw.browser && !domNode.isTiddlyWikiFakeDom) {
|
|
|
|
hljs.highlightElement(domNode.children[0]);
|
|
|
|
} else {
|
|
|
|
var text = domNode.textContent;
|
|
|
|
domNode.children[0].innerHTML = hljs.highlight(text,{language: language, ignoreIllegals: true}).value;
|
|
|
|
// If we're using the fakedom then specially save the original raw text
|
|
|
|
if(domNode.isTiddlyWikiFakeDom) {
|
|
|
|
domNode.children[0].textInnerHTML = text;
|
|
|
|
}
|
2017-02-21 13:09:32 +00:00
|
|
|
}
|
2015-01-02 17:00:08 +00:00
|
|
|
}
|
2022-02-21 15:35:13 +00:00
|
|
|
};
|
|
|
|
}
|
2014-01-05 09:58:01 +00:00
|
|
|
|
|
|
|
})();
|