1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-27 03:57:21 +00:00

Fix markdown table to honor alignment directives (#4774)

* Extract withChildren

* Handle table cell alignment

* 🎨 add missing semicolon
This commit is contained in:
ento 2020-07-31 04:01:21 -08:00 committed by GitHub
parent 5a6e35b4b0
commit 7acb9a255b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -69,7 +69,7 @@ function findTagWithType(nodes, startPoint, type, level) {
function convertNodes(remarkableTree, isStartOfInline) {
let out = [];
var accumulatedText = '';
function wrappedElement(elementTag, currentIndex, currentLevel, closingType, nodes) {
function withChildren(currentIndex, currentLevel, closingType, nodes, callback) {
var j = findTagWithType(nodes, currentIndex + 1, closingType, currentLevel);
if (j === false) {
console.error("Failed to find a " + closingType + " node after position " + currentIndex);
@ -77,14 +77,18 @@ function convertNodes(remarkableTree, isStartOfInline) {
return currentIndex + 1;
}
let children = convertNodes(nodes.slice(currentIndex + 1, j));
out.push({
type: "element",
tag: elementTag,
children: children
});
callback(children);
return j;
}
function wrappedElement(elementTag, currentIndex, currentLevel, closingType, nodes) {
return withChildren(currentIndex, currentLevel, closingType, nodes, function(children) {
out.push({
type: "element",
tag: elementTag,
children: children
});
});
}
for (var i = 0; i < remarkableTree.length; i++) {
var currentNode = remarkableTree[i];
@ -110,33 +114,32 @@ function convertNodes(remarkableTree, isStartOfInline) {
break;
case "link_open":
var j = findTagWithType(remarkableTree, i + 1, "link_close", currentNode.level);
if (currentNode.href[0] !== "#") {
// External link
var attributes = {
href: { type: "string", value: currentNode.href }
};
if (pluginOpts.linkNewWindow) {
attributes.target = { type: "string", value: "_blank" };
i = withChildren(i, currentNode.level, "link_close", remarkableTree, function(children) {
if (currentNode.href[0] !== "#") {
// External link
var attributes = {
href: { type: "string", value: currentNode.href }
};
if (pluginOpts.linkNewWindow) {
attributes.target = { type: "string", value: "_blank" };
}
out.push({
type: "element",
tag: "a",
attributes: attributes,
children: children
});
} else {
// Internal link
out.push({
type: "link",
attributes: {
to: { type: "string", value: decodeURI(currentNode.href.substr(1)) }
},
children: children
});
}
out.push({
type: "element",
tag: "a",
attributes: attributes,
children: convertNodes(remarkableTree.slice(i + 1, j))
});
} else {
// Internal link
out.push({
type: "link",
attributes: {
to: { type: "string", value: decodeURI(currentNode.href.substr(1)) }
},
children: convertNodes(remarkableTree.slice(i + 1, j))
});
}
i = j;
});
break;
case "code":
@ -185,6 +188,23 @@ function convertNodes(remarkableTree, isStartOfInline) {
});
break;
case "th_open":
case "td_open":
var elementTag = currentNode.type.slice(0, 2);
i = withChildren(i, currentNode.level, elementTag + "_close", remarkableTree, function(children) {
var attributes = {};
if (currentNode.align) {
attributes.style = { type: "string", value: "text-align:" + currentNode.align };
}
out.push({
type: "element",
tag: elementTag,
attributes: attributes,
children: children
});
});
break;
case "hr":
out.push({
type: 'element',