mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-12-05 00:08:06 +00:00
Add support for retrieving formatted text content from the fake dom
The idea is to convert HTML block elements into newlines, so that the output is readable on a terminal.
This commit is contained in:
@@ -26,6 +26,12 @@ var TW_TextNode = function(text) {
|
||||
this.textContent = text;
|
||||
};
|
||||
|
||||
Object.defineProperty(TW_TextNode.prototype, "formattedTextContent", {
|
||||
get: function() {
|
||||
return this.textContent.replace(/(\r?\n)/g,"");
|
||||
}
|
||||
});
|
||||
|
||||
var TW_Element = function(tag,namespace) {
|
||||
bumpSequenceNumber(this);
|
||||
this.isTiddlyWikiFakeDom = true;
|
||||
@@ -176,6 +182,30 @@ Object.defineProperty(TW_Element.prototype, "textContent", {
|
||||
}
|
||||
});
|
||||
|
||||
Object.defineProperty(TW_Element.prototype, "formattedTextContent", {
|
||||
get: function() {
|
||||
if(this.isRaw) {
|
||||
throw "Cannot get formattedTextContent on a raw TW_Element";
|
||||
} else {
|
||||
var b = [],
|
||||
isBlock = $tw.config.htmlBlockElements.indexOf(this.tag) !== -1;
|
||||
if(isBlock) {
|
||||
b.push("\n");
|
||||
}
|
||||
if(this.tag === "li") {
|
||||
b.push("* ")
|
||||
}
|
||||
$tw.utils.each(this.children,function(node) {
|
||||
b.push(node.formattedTextContent);
|
||||
});
|
||||
if(isBlock) {
|
||||
b.push("\n");
|
||||
}
|
||||
return b.join("");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var document = {
|
||||
setSequenceNumber: function(value) {
|
||||
sequenceNumber = value;
|
||||
|
||||
Reference in New Issue
Block a user