1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-11-05 18:13:17 +00:00

Fix problem with highlight plugin language brushes

TiddlyWiki passes the MIME type of the tiddler to highlight.js as the
"language brush", but it turns out that highlight.js doesn't actually
understand MIME types. This commit introduces a configuration mapping
between common MIME types and highlight.js language brushes

Fixes #2535
This commit is contained in:
Jermolene
2016-08-18 09:07:06 +01:00
parent ffae85140f
commit ee9d19d299
4 changed files with 32 additions and 10 deletions

View File

@@ -12,6 +12,8 @@ Wraps up the fenced code blocks parser for highlight and use in TiddlyWiki5
/*global $tw: false */
"use strict";
var TYPE_MAPPINGS_BASE = "$:/config/HighlightPlugin/TypeMappings/";
var CodeBlockWidget = require("$:/core/modules/widgets/codeblock.js").codeblock;
var hljs = require("$:/plugins/tiddlywiki/highlight/highlight.js");
@@ -19,14 +21,19 @@ var hljs = require("$:/plugins/tiddlywiki/highlight/highlight.js");
hljs.configure({tabReplace: " "});
CodeBlockWidget.prototype.postRender = function() {
var domNode = this.domNodes[0];
if($tw.browser && this.document !== $tw.fakeDocument && this.language) {
domNode.className = this.language.toLowerCase();
var domNode = this.domNodes[0],
language = this.language,
tiddler = this.wiki.getTiddler(TYPE_MAPPINGS_BASE + language);
if(tiddler && tiddler.fields.text) {
language = tiddler.fields.text;
}
if($tw.browser && this.document !== $tw.fakeDocument && language) {
domNode.className = language.toLowerCase();
hljs.highlightBlock(domNode);
} else if(!$tw.browser && this.language && this.language.indexOf("/") === -1 ){
} else if(!$tw.browser && language && language.indexOf("/") === -1 ){
try {
domNode.className = this.language.toLowerCase() + " hljs";
domNode.children[0].innerHTML = hljs.fixMarkup(hljs.highlight(this.language, this.getAttribute("code")).value);
domNode.className = language.toLowerCase() + " hljs";
domNode.children[0].innerHTML = hljs.fixMarkup(hljs.highlight(language, this.getAttribute("code")).value);
}
catch(err) {
// Can't easily tell if a language is registered or not in the packed version of hightlight.js,