Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(mobile): keep code fences and literal preview text from older HTML descriptions

Tin Severbe58ea94c491
diff --git a/apps/mobile/src/lib/legacy-html.test.ts b/apps/mobile/src/lib/legacy-html.test.ts
index f5617037a..0121b9808 100644
--- a/apps/mobile/src/lib/legacy-html.test.ts
+++ b/apps/mobile/src/lib/legacy-html.test.ts
@@ -37,6 +37,12 @@ describe("legacy HTML descriptions", () => {
).toBe("**Hello** world, *in*line and `` echo `hostname` ``");
});
+ it("fences code blocks that contain backtick fences", () => {
+ expect(
+ legacyHtmlToMarkdown("<pre><code>a\n```\nb\n</code></pre><p>after</p>"),
+ ).toBe("````\na\n```\nb\n````\n\nafter");
+ });
+
it("converts editor task lists with their checked state", () => {
const html =
'<ul class="not-prose pl-2" data-type="taskList"><li class="flex gap-2 items-start" data-checked="false" data-type="taskItem"><label><input type="checkbox"><span></span></label><div><p>Move the instances.json</p></div></li><li data-checked="true" data-type="taskItem"><label><input type="checkbox" checked="checked"><span></span></label><div><p>Use a $schema</p></div></li></ul>';
@@ -59,7 +65,7 @@ describe("legacy HTML descriptions", () => {
'<h2>Title</h2><p>See <a href="https://kaneo.app" target="_blank">site</a></p><blockquote><p>Quote</p><p>More</p></blockquote><pre><code class="language-ts">const a = 1 &lt; 2;\n</code></pre>',
),
).toBe(
- "## Title\n\nSee [site](https://kaneo.app)\n\n> Quote\n>\n> More\n\n```\nconst a = 1 < 2;\n\n```",
+ "## Title\n\nSee [site](https://kaneo.app)\n\n> Quote\n>\n> More\n\n```\nconst a = 1 < 2;\n```",
);
});
diff --git a/apps/mobile/src/lib/legacy-html.ts b/apps/mobile/src/lib/legacy-html.ts
index 29bcd2c61..1b1ac2b6e 100644
--- a/apps/mobile/src/lib/legacy-html.ts
+++ b/apps/mobile/src/lib/legacy-html.ts
@@ -29,6 +29,7 @@ const framed = new Set([
"blockquote",
"a",
"code",
+ "pre",
...Object.keys(marks),
]);
@@ -65,6 +66,10 @@ function attr(raw: string, name: string): string | undefined {
return match ? decode(match[1] ?? match[2]) : undefined;
}
+function longestBackticks(text: string): number {
+ return Math.max(0, ...(text.match(/`+/g) ?? []).map((run) => run.length));
+}
+
function finish(frame: Frame, parent: Frame): string {
const body = frame.out.trim();
switch (frame.tag) {
@@ -95,12 +100,12 @@ function finish(frame: Frame, parent: Frame): string {
.split("\n")
.map((line) => (line ? `> ${line}` : ">"))
.join("\n")}\n\n`;
+ case "pre": {
+ const fence = "`".repeat(Math.max(3, longestBackticks(frame.out) + 1));
+ return `${fence}\n${frame.out.replace(/\n$/, "")}\n${fence}\n\n`;
+ }
case "code": {
- const longest = Math.max(
- 0,
- ...(frame.out.match(/`+/g) ?? []).map((run) => run.length),
- );
- const fence = "`".repeat(longest + 1);
+ const fence = "`".repeat(longestBackticks(frame.out) + 1);
const pad = /^`|`$/.test(frame.out) ? " " : "";
return frame.out ? `${fence}${pad}${frame.out}${pad}${fence}` : "";
}
@@ -147,6 +152,7 @@ export function legacyHtmlToMarkdown(html: string): string {
} else if (name === "code" && pre) {
continue;
} else if (framed.has(name)) {
+ if (name === "pre") pre = Math.max(0, pre + (closing ? -1 : 1));
if (!closing) {
stack.push({ tag: name, attrs: rawAttrs, out: "", items: 0 });
} else if (stack.some((frame, i) => i > 0 && frame.tag === name)) {
@@ -156,9 +162,6 @@ export function legacyHtmlToMarkdown(html: string): string {
top().out += finish(frame, top());
} while (frame.tag !== name);
}
- } else if (name === "pre") {
- pre = Math.max(0, pre + (closing ? -1 : 1));
- emit(closing ? "\n```\n\n" : "```\n");
} else if (/^h[1-6]$/.test(name)) {
emit(closing ? "\n\n" : `${"#".repeat(Number(name[1]))} `);
} else if (name === "br") {
diff --git a/apps/mobile/src/lib/markdown.ts b/apps/mobile/src/lib/markdown.ts
index 23022a8b7..ade295044 100644
--- a/apps/mobile/src/lib/markdown.ts
+++ b/apps/mobile/src/lib/markdown.ts
@@ -67,13 +67,19 @@ export function toDisplayMarkdown(markdown: string, serverUrl: string): string {
/** Plain-text preview for list rows. */
export function toPlainText(markdown?: string | null): string {
if (!markdown) return "";
+ // Park escaped characters so the stripping below leaves literal text alone.
return toDisplayMarkdown(markdown, "")
- .replace(/\\([!-/:-@[-`{-~])/g, "$1")
+ .replace(/\\([!-/:-@[-`{-~])/g, (_, c: string) =>
+ String.fromCharCode(0xe000 + c.charCodeAt(0)),
+ )
.replace(/!\[[^\]]*\]\([^)]*\)/g, "")
.replace(/\[([^\]]*)\]\([^)]*\)/g, "$1")
.replace(/<[^>]+>/g, "")
.replace(/[*_`#>~]+/g, "")
.replace(/\s+/g, " ")
+ .replace(/[\ue000-\ue07f]/g, (c) =>
+ String.fromCharCode(c.charCodeAt(0) - 0xe000),
+ )
.trim();
}