fix(tables): only escape pipes outside SB context (#1292)
* fix(tables): only escape pipes outside SB context Fixes #943 * fix(tables): detect command context in tables Fixes #1016 * Added tests for parsing tables
This commit is contained in:
@@ -240,3 +240,43 @@ Deno.test("Test NakedURL parser", () => {
|
||||
"http://abc.com?e=2.71",
|
||||
]);
|
||||
});
|
||||
|
||||
const tableSample = `
|
||||
| Header A | Header B |
|
||||
|----------|----------|
|
||||
| [[Wiki|Alias]] | 1B |
|
||||
| 2A | 2B |
|
||||
| 3A | {[My: Command|Alias]("args", 1)} |
|
||||
`;
|
||||
|
||||
Deno.test("Test table parser", () => {
|
||||
const tree = parseMarkdown(tableSample);
|
||||
const cells = collectNodesOfType(tree, "TableCell");
|
||||
|
||||
assertEquals(cells.map((x) => x.children![0].text), [
|
||||
"Header A",
|
||||
"Header B",
|
||||
undefined,
|
||||
"1B",
|
||||
"2A",
|
||||
"2B",
|
||||
"3A",
|
||||
undefined,
|
||||
]);
|
||||
|
||||
// Check the Wiki Link - Make sure no backslash has been added (issue 943)
|
||||
assertEquals(cells[2].children![0].type, "WikiLink");
|
||||
const wikiName = findNodeOfType(cells[2], "WikiLinkPage");
|
||||
const wikiAlias = findNodeOfType(cells[2], "WikiLinkAlias");
|
||||
assertEquals(wikiName!.children![0].text, "Wiki");
|
||||
assertEquals(wikiAlias!.children![0].text, "Alias");
|
||||
|
||||
// Check the Command Link - Make sure no backslash has been added (issue 1016)
|
||||
assertEquals(cells[7].children![0].type, "CommandLink");
|
||||
const commandName = findNodeOfType(cells[7], "CommandLinkName");
|
||||
const commandArgs = findNodeOfType(cells[7], "CommandLinkArgs");
|
||||
const commandAlias = findNodeOfType(cells[7], "CommandLinkAlias");
|
||||
assertEquals(commandName!.children![0].text, "My: Command");
|
||||
assertEquals(commandAlias!.children![0].text, "Alias");
|
||||
assertEquals(commandArgs!.children![0].text, '"args", 1');
|
||||
});
|
||||
|
||||
@@ -11,7 +11,8 @@ import { tags as t } from "@lezer/highlight";
|
||||
// Forked from https://github.com/lezer-parser/markdown/blob/main/src/extension.ts
|
||||
// MIT License
|
||||
// Author: Marijn Haverbeke
|
||||
// Change made: Avoid wiki links with aliases [[link|alias]] from being parsed as table row separators
|
||||
// Change made: Avoid wiki links and commands with aliases [[link|alias]], {[My: Command|alias]}
|
||||
// from being parsed as table row separators
|
||||
|
||||
function parseRow(
|
||||
cx: BlockContext,
|
||||
@@ -36,16 +37,29 @@ function parseRow(
|
||||
};
|
||||
|
||||
let inWikilink = false;
|
||||
let inCommandButton = false;
|
||||
for (let i = startI; i < line.length; i++) {
|
||||
const next = line.charCodeAt(i);
|
||||
if (next === 91 /* '[' */ && line.charAt(i + 1) === "[") {
|
||||
|
||||
if (line[i] === "[" && line.charAt(i + 1) === "[") {
|
||||
inWikilink = true;
|
||||
} else if (
|
||||
next === 93 /* ']' */ && line.charAt(i - 1) === "]" && inWikilink
|
||||
) {
|
||||
} else if (line[i] === "]" && line.charAt(i - 1) === "]" && inWikilink) {
|
||||
inWikilink = false;
|
||||
}
|
||||
if (next == 124 /* '|' */ && !esc && !inWikilink) {
|
||||
|
||||
if (line[i] === "{" && line.charAt(i + 1) === "[") {
|
||||
inCommandButton = true;
|
||||
} else if (inCommandButton) {
|
||||
if (line[i] === "}" && line.charAt(i - 1) === "]") {
|
||||
// Command button without arguments: {[My Command: BeepBoop|Alias]}
|
||||
inCommandButton = false;
|
||||
} else if (line[i] === "}" && line.charAt(i - 1) === ")") {
|
||||
// Command button with arguments: {[My Command: BeepBoop|Alias]("args")}
|
||||
inCommandButton = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (next == 124 /* '|' */ && !esc && !inWikilink && !inCommandButton) {
|
||||
if (!first || cellStart > -1) count++;
|
||||
first = false;
|
||||
if (elts) {
|
||||
|
||||
+36
-1
@@ -8,7 +8,7 @@ export function defaultTransformer(v: any): Promise<string> {
|
||||
return Promise.resolve("");
|
||||
}
|
||||
if (typeof v === "string") {
|
||||
return Promise.resolve(v.replaceAll("\n", " ").replaceAll("|", "\\|"));
|
||||
return Promise.resolve(escapeRegularPipes(v.replaceAll("\n", " ")));
|
||||
}
|
||||
if (v && typeof v === "object") {
|
||||
return Promise.resolve(luaToString(v));
|
||||
@@ -16,6 +16,41 @@ export function defaultTransformer(v: any): Promise<string> {
|
||||
return Promise.resolve("" + v);
|
||||
}
|
||||
|
||||
/**
|
||||
* Escapes all pipes that would inadvertently delimit a markdown table column.
|
||||
* Does not escape columns that are used for aliasing in WikiLinks or Commands:
|
||||
* `[[WikiLink|Alias]]` and `{[Command: Name|Click Me!]("args")}`
|
||||
* @param s The text to replace
|
||||
* @returns The text where the pipes outside of silverbullet specific context is
|
||||
* replaced with an escaped pipe.
|
||||
*/
|
||||
function escapeRegularPipes(s: string) {
|
||||
let result = "";
|
||||
let isInWikiLink = false;
|
||||
let isInCommandButton = false;
|
||||
|
||||
for (let i = 0; i < s.length; i++) {
|
||||
if (s[i] == "[" && s[i + 1] == "[") {
|
||||
isInWikiLink = true;
|
||||
} else if (s[i] == "]" && s[i + 1] == "]" && isInWikiLink) {
|
||||
isInWikiLink = false;
|
||||
}
|
||||
if (s[i] == "{" && s[i + 1] == "[") {
|
||||
isInCommandButton = true;
|
||||
} else if (
|
||||
(s[i] == "]" || s[i] == ")") && s[i + 1] == "}" && isInCommandButton
|
||||
) {
|
||||
isInCommandButton = false;
|
||||
} else if (s[i] == "|" && !isInWikiLink && !isInCommandButton) {
|
||||
result += "\\";
|
||||
}
|
||||
|
||||
result += s[i];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Nicely format an array of JSON objects as a Markdown table
|
||||
export async function jsonToMDTable(
|
||||
jsonArray: any[],
|
||||
|
||||
Reference in New Issue
Block a user