Skip to content

Build your personal knowledge base with Trilium Notes

fix(docs): keep an escaped pipe in a table cell escaped once

Elian Doran060e935c27aa
diff --git a/apps/edit-docs/src/openapi_markdown.spec.ts b/apps/edit-docs/src/openapi_markdown.spec.ts
index 67a34e9a48..5c4cbac871 100644
--- a/apps/edit-docs/src/openapi_markdown.spec.ts
+++ b/apps/edit-docs/src/openapi_markdown.spec.ts
@@ -295,7 +295,8 @@ describe("openApiToMarkdown", () => {
type: "object",
properties: {
name: { type: "string", pattern: "^[^\\s]+", description: "The name" },
- dateCreated: { $ref: "#/components/schemas/LocalDateTime" }
+ dateCreated: { $ref: "#/components/schemas/LocalDateTime" },
+ separator: { type: "string", description: "Splits on a\\|b or c|d" }
}
}
}
@@ -313,7 +314,9 @@ describe("openApiToMarkdown", () => {
].join("\n"));
expect(markdown).toContain([
"| `name` | `string`<br>The name. Matches `^[^\\s]+`. |",
- "| `dateCreated` | [`LocalDateTime`](#schema-LocalDateTime) |"
+ "| `dateCreated` | [`LocalDateTime`](#schema-LocalDateTime) |",
+ // A pipe escaped already stays escaped once.
+ "| `separator` | `string`<br>Splits on a\\|b or c\\|d |"
].join("\n"));
});
diff --git a/apps/edit-docs/src/openapi_markdown.ts b/apps/edit-docs/src/openapi_markdown.ts
index 21459207fd..d9e05688a6 100644
--- a/apps/edit-docs/src/openapi_markdown.ts
+++ b/apps/edit-docs/src/openapi_markdown.ts
@@ -571,9 +571,14 @@ function table(headers: string[], rows: string[][]) {
.join("\n");
}
-/** A table cell holds one line, and a pipe would end it early, even inside a code span. */
+/**
+ * A table cell holds one line, and a pipe would end it early, even inside a code span. A pipe after
+ * an odd number of backslashes is escaped already.
+ */
function cell(text: string) {
- return text.replace(/\s*\n\s*/g, " ").replaceAll("|", "\\|").trim();
+ return text.replace(/\s*\n\s*/g, " ")
+ .replace(/(\\*)\|/g, (_, backslashes: string) => `${backslashes}${backslashes.length % 2 ? "" : "\\"}|`)
+ .trim();
}
/** The name cell of a parameter or property, marking it below the name when it is required. */
diff --git a/apps/edit-docs/src/typedoc_markdown.spec.ts b/apps/edit-docs/src/typedoc_markdown.spec.ts
index 61af11f8c2..ed7281b9d4 100644
--- a/apps/edit-docs/src/typedoc_markdown.spec.ts
+++ b/apps/edit-docs/src/typedoc_markdown.spec.ts
@@ -28,6 +28,8 @@ export interface Unreached {
flag: boolean;
/** A use of \`Screen\` the page does not show, which leaves it used in one place. */
screen: Screen;
+ /** Splits on a\\|b or c|d. */
+ separator: string;
}
/** A widget's constructor. */
@@ -273,6 +275,10 @@ describe("typeDocToMarkdown", () => {
expect(exports).toContain("type Func = ((...args: unknown[]) => unknown) | string");
});
+ it("escapes the pipes of a table cell, leaving one escaped already as it is", () => {
+ expect(typeDocToMarkdown(project)).toContain("| `separator` | `string`<br>Splits on a\\|b or c\\|d. |");
+ });
+
it("refuses prefixes that would give two declarations one name", () => {
expect(() => typeDocToMarkdown(project, { root: "DemoApi", stripPrefixes: [ "Demo", "DemoWindow" ] }))
.toThrow("DemoApi and DemoWindowApi would both be named Api");
diff --git a/apps/edit-docs/src/typedoc_markdown.ts b/apps/edit-docs/src/typedoc_markdown.ts
index 0c97e9d581..0c9a104fa4 100644
--- a/apps/edit-docs/src/typedoc_markdown.ts
+++ b/apps/edit-docs/src/typedoc_markdown.ts
@@ -541,7 +541,7 @@ function fence(text: string) {
}
function table(headers: string[], rows: string[][]) {
- const line = (cells: string[]) => `| ${cells.map((cell) => cell.replace(/\|/g, "\\|")).join(" | ")} |`;
+ const line = (cells: string[]) => `| ${cells.map(escapePipes).join(" | ")} |`;
return [ line(headers), `| ${headers.map(() => "---").join(" | ")} |`, ...rows.map(line) ].join("\n");
}
@@ -585,6 +585,15 @@ function markdownToHtml(markdown: string) {
.join("");
}
+/**
+ * Escapes the pipes of a table cell's Markdown, which would otherwise end the cell, even inside a
+ * code span. A pipe after an odd number of backslashes is escaped already.
+ */
+function escapePipes(markdown: string) {
+ return markdown.replace(/(\\*)\|/g, (_, backslashes: string) =>
+ `${backslashes}${backslashes.length % 2 ? "" : "\\"}|`);
+}
+
function escapeHtml(text: string) {
return text.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
}