1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-09-18 10:19:44 +00:00
TiddlyWiki5/plugins/tiddlywiki/highlight/highlightblock.js
Jermolene b1ecf81b0c Tentative improvements to highlight plugin problems
We now use highlight.js in raw HTML mode on the server, rather than
trying to use it with the fakedom. This causes problems with fakedoms
inability to get textContent for a node that has been created by
assigning innerHTML. So we extend the fakedom to allow the original
text content to be saved.

See #2778 for discussion.
2017-02-21 13:09:32 +00:00

46 lines
1.2 KiB
JavaScript

/*\
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;
var hljs = require("$:/plugins/tiddlywiki/highlight/highlight.js");
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) {
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;
}
}
}
};
})();