mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-07-07 04:22:50 +00:00
feat: add to styleblock
This commit is contained in:
parent
fb8732a081
commit
078cd5fba2
@ -74,16 +74,19 @@ exports.parse = function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
exports.serialize = function (tree, serialize) {
|
exports.serialize = function (tree, serialize) {
|
||||||
// tree: { type: 'element', tag: 'blockquote', attributes: { class: { type: 'string', value: 'tc-quote' } }, children: [{ type: 'text', text: 'Quote text' }] }
|
// tree: { type: "element", tag: "blockquote", attributes: { class: { type: "string", value: "tc-quote" } }, children: [{ type: "element", tag: "cite", children: [{ type: "text", text: "tc-quote" }] }, { type: "element", tag: "p", children: [{ type: "text", text: "Quote text\n" }] }] }
|
||||||
// serialize: function that accepts array of nodes or a node and returns a string
|
var result = [];
|
||||||
// Split the class attribute value into an array of classes, classes: ['tc-quote']
|
if(tree.type === "element" && tree.tag === "blockquote") {
|
||||||
var classes = tree.attributes.class.value.split(" ");
|
// tree.attributes.class.value: "tc-quote"
|
||||||
// Start the serialized string with the opening delimiter and classes, serialized: '<<<tc-quote\n'
|
result.push("<<<" + tree.attributes.class.value);
|
||||||
var serialized = "<<<" + classes.join(" ") + "\n";
|
tree.children.forEach(function (child) {
|
||||||
// Serialize the children of the blockquote, serialized: '<<<tc-quote\nQuote text'
|
if(child.type === "element" && child.tag === "p") {
|
||||||
serialized += serialize(tree.children);
|
result.push(serialize(child.children).trim());
|
||||||
// Return the complete serialized string with the closing delimiter, serialized: '<<<tc-quote\nQuote text\n<<<'
|
}
|
||||||
return serialized + "\n<<<";
|
});
|
||||||
|
result.push("<<<");
|
||||||
|
}
|
||||||
|
return result.join("\n");
|
||||||
};
|
};
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
@ -57,21 +57,33 @@ exports.parse = function() {
|
|||||||
if(tokens.length > 0) {
|
if(tokens.length > 0) {
|
||||||
this.parser.amendRules(tokens[0],tokens.slice(1));
|
this.parser.amendRules(tokens[0],tokens.slice(1));
|
||||||
}
|
}
|
||||||
// No parse tree nodes to return
|
// No widget to render, return void node.
|
||||||
return [];
|
return [{
|
||||||
|
type: "void",
|
||||||
|
attributes: {
|
||||||
|
action: {type: "string", value: tokens[0]},
|
||||||
|
rules: {type: "string", value: tokens.slice(1).join(" ")}
|
||||||
|
},
|
||||||
|
children: []
|
||||||
|
}];
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.serialize = function (tree, serialize) {
|
exports.serialize = function (tree, serialize) {
|
||||||
// tree: { type: 'pragma', name: 'rules', args: ['except', 'ruleone', 'ruletwo', 'rulethree'] }
|
// tree: { type: "void", attributes: { action: { type: "string", value: "except" }, rules: { type: "string", value: "ruleone ruletwo rulethree" } }, children: [{ type: "void", attributes: { action: { type: "string", value: "only" }, rules: { type: "string", value: "ruleone ruletwo rulethree" } }, children: [] }] }
|
||||||
// serialize: function that accepts array of nodes or a node and returns a string
|
var result = [];
|
||||||
// Start the serialized string with the pragma name
|
if(tree.attributes.action && tree.attributes.rules) {
|
||||||
var serialized = "\\rules";
|
// tree.attributes.action.value: "except"
|
||||||
// Iterate over the arguments and append them to the serialized string
|
// tree.attributes.rules.value: "ruleone ruletwo rulethree"
|
||||||
for(var i = 0; i < tree.args.length; i++) {
|
result.push("\\rules " + tree.attributes.action.value + " " + tree.attributes.rules.value);
|
||||||
serialized += " " + tree.args[i];
|
tree.children.forEach(function (child) {
|
||||||
|
if(child.type === "void" && child.attributes.action && child.attributes.rules) {
|
||||||
|
// child.attributes.action.value: "only"
|
||||||
|
// child.attributes.rules.value: "ruleone ruletwo rulethree"
|
||||||
|
result.push("\\rules " + child.attributes.action.value + " " + child.attributes.rules.value);
|
||||||
}
|
}
|
||||||
// Return the complete serialized string
|
});
|
||||||
return serialized;
|
}
|
||||||
|
return result.join("\n");
|
||||||
};
|
};
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
@ -67,29 +67,62 @@ exports.parse = function() {
|
|||||||
$tw.utils.addAttributeToParseTreeNode(tree[t],"style",styles.join(""));
|
$tw.utils.addAttributeToParseTreeNode(tree[t],"style",styles.join(""));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return tree;
|
return [{
|
||||||
|
type: "void",
|
||||||
|
children: tree
|
||||||
|
}]
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.serialize = function(tree, serialize) {
|
exports.serialize = function(tree, serialize) {
|
||||||
// tree: [{ type: 'element', tag: 'p', attributes: { class: { type: 'string', value: 'myClass' }, style: { type: 'string', value: 'background-color:red;' } }, children: [{ type: 'text', text: 'This paragraph will have the CSS class `myClass`.' }] }]
|
// serialize: function that serializes an array of nodes or a single node to a string
|
||||||
// serialize: function that accepts array of nodes or a node and returns a string
|
var result = [];
|
||||||
// Initialize the serialized string with the opening delimiter
|
var classes = [];
|
||||||
var serialized = "@@";
|
var styles = [];
|
||||||
// Check for styles and append them to the serialized string
|
|
||||||
if(tree[0].attributes.style) {
|
// Collect all unique classes and styles from child nodes
|
||||||
serialized += tree[0].attributes.style.value;
|
for(var i = 0; i < tree.children.length; i++) {
|
||||||
}
|
var node = tree.children[i];
|
||||||
// Check for classes and append them to the serialized string
|
if(node.attributes && node.attributes.class) {
|
||||||
if(tree[0].attributes.class) {
|
var nodeClasses = node.attributes.class.value.split(" ");
|
||||||
var classes = tree[0].attributes.class.value.split(" ");
|
for(var j = 0; j < nodeClasses.length; j++) {
|
||||||
for(var i = 0; i < classes.length; i++) {
|
if(classes.indexOf(nodeClasses[j]) === -1) {
|
||||||
serialized += "." + classes[i];
|
classes.push(nodeClasses[j]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Append the serialized children and the closing delimiter
|
}
|
||||||
serialized += "\n" + serialize(tree) + "\n@@";
|
if(node.attributes && node.attributes.style) {
|
||||||
// Return the complete serialized string
|
var nodeStyles = node.attributes.style.value.split(";");
|
||||||
return serialized;
|
for(var k = 0; k < nodeStyles.length; k++) {
|
||||||
|
var style = nodeStyles[k].trim();
|
||||||
|
if(style && styles.indexOf(style) === -1) {
|
||||||
|
styles.push(style);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the style block header if there are any classes or styles
|
||||||
|
if(classes.length > 0 || styles.length > 0) {
|
||||||
|
if(styles.length > 0) {
|
||||||
|
result.push("@@");
|
||||||
|
result.push(styles.join(";"));
|
||||||
|
result.push(";\n");
|
||||||
|
}
|
||||||
|
if(classes.length > 0) {
|
||||||
|
result.push("@@.");
|
||||||
|
result.push(classes.join("."));
|
||||||
|
result.push("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serialize each child node and add to result
|
||||||
|
for(var i = 0; i < tree.children.length; i++) {
|
||||||
|
result.push(serialize(tree.children[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the closing @@ for the style block
|
||||||
|
result.push("@@");
|
||||||
|
return result.join("");
|
||||||
};
|
};
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
@ -276,7 +276,6 @@ describe("WikiAST serialization unit tests", function () {
|
|||||||
var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler("ParserModeTest").tree).trimEnd();
|
var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler("ParserModeTest").tree).trimEnd();
|
||||||
expect(serialized).toBe(wiki.getTiddlerText("ParserModeTest").trimEnd());
|
expect(serialized).toBe(wiki.getTiddlerText("ParserModeTest").trimEnd());
|
||||||
});
|
});
|
||||||
return;
|
|
||||||
|
|
||||||
wiki.addTiddler({
|
wiki.addTiddler({
|
||||||
title: "PrettyExtLinkTest",
|
title: "PrettyExtLinkTest",
|
||||||
@ -316,12 +315,19 @@ describe("WikiAST serialization unit tests", function () {
|
|||||||
|
|
||||||
wiki.addTiddler({
|
wiki.addTiddler({
|
||||||
title: "StyleBlockTest",
|
title: "StyleBlockTest",
|
||||||
text: "@@.myClass\n@@background-color:red;\nThis paragraph will have the CSS class `myClass`.\n\n* The `<ul>` around this list will also have the class `myClass`\n* List item 2\n@@",
|
text: "@@.myClass\n@@background-color:red;\nThis paragraph will have the CSS class `myClass`.\n\n* The `<ul>` around this list will also have the class `myClass`\n* List item 2\n@@\n\n@@font-size:1.5em;\n@@.coloured-text\n@@.coloured-bg\n* Block content\n* With custom style and classes\n@@",
|
||||||
|
});
|
||||||
|
wiki.addTiddler({
|
||||||
|
title: "StyleBlockTest2",
|
||||||
|
text: "@@.myFirstClass.mySecondClass\n@@width:100px;.myThirdClass\nThis is a paragraph\n@@\n\n@@background-color:lightcyan;\n* Item one\n* Item two\n@@",
|
||||||
});
|
});
|
||||||
it("should serialize style blocks correctly", function () {
|
it("should serialize style blocks correctly", function () {
|
||||||
var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler("StyleBlockTest").tree).trimEnd();
|
var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler("StyleBlockTest").tree).trimEnd();
|
||||||
expect(serialized).toBe(wiki.getTiddlerText("StyleBlockTest").trimEnd());
|
expect(serialized).toBe(wiki.getTiddlerText("StyleBlockTest").trimEnd());
|
||||||
|
var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler("StyleBlockTest2").tree).trimEnd();
|
||||||
|
expect(serialized).toBe(wiki.getTiddlerText("StyleBlockTest2").trimEnd());
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
|
|
||||||
wiki.addTiddler({
|
wiki.addTiddler({
|
||||||
title: "StyleInlineTest",
|
title: "StyleInlineTest",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user