mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-02-01 11:49:10 +00:00
feat: list
This commit is contained in:
parent
7b4ea476f3
commit
f69795c62e
@ -51,11 +51,11 @@ exports.parse = function() {
|
|||||||
}];
|
}];
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.serialize = function(tree) {
|
exports.serialize = function(tree,serialize) {
|
||||||
// Filter attribute
|
// Filter attribute
|
||||||
var filter = tree.attributes.filter.value;
|
var filter = tree.attributes.filter.value;
|
||||||
// Construct the serialized string
|
// Construct the serialized string with children that is actually the sibling below the pragma.
|
||||||
return "\\import " + filter;
|
return "\\import " + filter + "\n" + serialize(tree.children);
|
||||||
};
|
};
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
@ -154,27 +154,53 @@ exports.parse = function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
exports.serialize = function (tree, serialize) {
|
exports.serialize = function (tree, serialize) {
|
||||||
// Serialize the list recursively
|
// Helper function to find the marker for a given list container tag and item tag
|
||||||
return serializeList(tree, serialize, 0);
|
function findMarker(listTag, itemTag) {
|
||||||
};
|
for(var key in listTypes) {
|
||||||
|
if(listTypes[key].listTag === listTag && listTypes[key].itemTag === itemTag) {
|
||||||
// utility to handle recursion of list
|
return key; // Return the marker associated with the list tag and item tag
|
||||||
function serializeList(node, serialize, depth) {
|
}
|
||||||
// List tag
|
}
|
||||||
var listTag = node.tag;
|
return ''; // Return empty string if no matching marker is found
|
||||||
// List item tag
|
|
||||||
var itemTag = listTypes[listTag[0]].itemTag;
|
|
||||||
// Serialized list items
|
|
||||||
var items = node.children.map(function(child) {
|
|
||||||
// Classes
|
|
||||||
var classes = child.attributes && child.attributes.class ? child.attributes.class.value.split(" ").join(".") : "";
|
|
||||||
// Serialized children
|
|
||||||
var children = serialize(child.children);
|
|
||||||
// Construct the serialized list item
|
|
||||||
return Array(depth + 1).join(listTag[0]) + (classes ? "." + classes : "") + " " + children;
|
|
||||||
}).join("\n");
|
|
||||||
// Return the serialized list
|
|
||||||
return items;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Recursive function to serialize list nodes, handling nested lists and formatting output
|
||||||
|
function serializeList(node, markerPrefix) {
|
||||||
|
var result = [];
|
||||||
|
if(node.type === 'element' && ['ul', 'ol', 'dl', 'blockquote'].includes(node.tag)) {
|
||||||
|
node.children.forEach(function (child) {
|
||||||
|
if(['li', 'dt', 'dd', 'div'].includes(child.tag)) {
|
||||||
|
var currentMarker = findMarker(node.tag, child.tag);
|
||||||
|
// Handle class attributes
|
||||||
|
var classAttr = child.attributes && child.attributes.class ? '.' + child.attributes.class.value : '';
|
||||||
|
// same level text nodes may be split into multiple children, and separated by deeper list sub-tree. We collect same level text nodes into this list, and concat then submit them before enter deeper list.
|
||||||
|
var content = [];
|
||||||
|
$tw.utils.each(child.children,function (subNode) {
|
||||||
|
// Check if the child is a nested list or a simple line of list item
|
||||||
|
if(['ul', 'ol', 'dl', 'blockquote'].includes(subNode.tag)) {
|
||||||
|
// Recursive call for nested lists
|
||||||
|
if(content.length > 0) {
|
||||||
|
result.push(markerPrefix + currentMarker + classAttr + ' ' + content.join('').trim());
|
||||||
|
content = []
|
||||||
|
}
|
||||||
|
result.push(serializeList(subNode, markerPrefix + currentMarker).trim())
|
||||||
|
} else {
|
||||||
|
content.push(serialize(subNode)) ;
|
||||||
|
}
|
||||||
|
return ''; // Default return for unhandled node types
|
||||||
|
});
|
||||||
|
if(content.length > 0) {
|
||||||
|
result.push(markerPrefix + currentMarker + classAttr + ' ' + content.join('').trim());
|
||||||
|
content = []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return result.join('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Begin serialization from the root node, with an empty string as the initial marker prefix
|
||||||
|
return serializeList(tree, '') + '\n\n';
|
||||||
|
};
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
@ -213,11 +213,10 @@ describe('WikiAST serialization unit tests', function () {
|
|||||||
var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler('ImageTest').tree).trimEnd();
|
var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler('ImageTest').tree).trimEnd();
|
||||||
expect(serialized).toBe(wiki.getTiddlerText('ImageTest').trimEnd());
|
expect(serialized).toBe(wiki.getTiddlerText('ImageTest').trimEnd());
|
||||||
});
|
});
|
||||||
return;
|
|
||||||
|
|
||||||
wiki.addTiddler({
|
wiki.addTiddler({
|
||||||
title: 'ImportTest',
|
title: 'ImportTest',
|
||||||
text: '\\import [[$:/core/ui/PageMacros]] [all[shadows+tiddlers]tag[$:/tags/Macro]!has[draft.of]]',
|
text: '\\import [[$:/core/ui/PageMacros]] [all[shadows+tiddlers]tag[$:/tags/Macro]!has[draft.of]]\n\\import [[$:/core/ui/PageMacros]]',
|
||||||
});
|
});
|
||||||
it('should serialize import pragma correctly', function () {
|
it('should serialize import pragma correctly', function () {
|
||||||
var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler('ImportTest').tree).trimEnd();
|
var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler('ImportTest').tree).trimEnd();
|
||||||
@ -232,6 +231,7 @@ describe('WikiAST serialization unit tests', function () {
|
|||||||
var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler('ListTest').tree).trimEnd();
|
var serialized = $tw.utils.serializeParseTree(wiki.parseTiddler('ListTest').tree).trimEnd();
|
||||||
expect(serialized).toBe(wiki.getTiddlerText('ListTest').trimEnd());
|
expect(serialized).toBe(wiki.getTiddlerText('ListTest').trimEnd());
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
|
|
||||||
wiki.addTiddler({
|
wiki.addTiddler({
|
||||||
title: 'MacroCallBlockTest',
|
title: 'MacroCallBlockTest',
|
||||||
|
Loading…
Reference in New Issue
Block a user