1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-16 02:19:55 +00:00

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.
This commit is contained in:
Jermolene 2017-02-21 13:09:32 +00:00
parent eee18aab40
commit b1ecf81b0c
2 changed files with 27 additions and 7 deletions

View File

@ -206,13 +206,29 @@ Object.defineProperty(TW_Element.prototype, "innerHTML", {
set: function(value) {
this.isRaw = true;
this.rawHTML = value;
this.rawTextContent = null;
}
});
Object.defineProperty(TW_Element.prototype, "textInnerHTML", {
set: function(value) {
if(this.isRaw) {
this.rawTextContent = value;
} else {
throw "Cannot set textInnerHTML of a non-raw TW_Element";
}
}
});
Object.defineProperty(TW_Element.prototype, "textContent", {
get: function() {
if(this.isRaw) {
throw "Cannot get textContent on a raw TW_Element";
if(this.rawTextContent === null) {
console.log(booboo)
throw "Cannot get textContent on a raw TW_Element";
} else {
return this.rawTextContent;
}
} else {
var b = [];
$tw.utils.each(this.children,function(node) {

View File

@ -28,12 +28,16 @@ CodeBlockWidget.prototype.postRender = function() {
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
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;
}
}
}
};