1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-14 22:04:51 +00:00
TiddlyWiki5/plugins/tiddlywiki/highlight/highlightblock.js

42 lines
1.1 KiB
JavaScript
Raw Normal View History

/*\
title: $:/plugins/tiddlywiki/highlight/highlightblock.js
type: application/javascript
module-type: widget
Wraps up the fenced code blocks parser for highlight and use in TiddlyWiki5
\*/
(function() {
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
var TYPE_MAPPINGS_BASE = "$:/config/HighlightPlugin/TypeMappings/";
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: " "});
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) {
try {
domNode.className = language.toLowerCase() + " hljs";
hljs.highlightBlock(domNode);
} catch(err) {
// 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
}
}
};
})();