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 Markdown characters in older HTML descriptions literal

Tin Severc2ccb9ed9266
diff --git a/apps/mobile/src/lib/legacy-html.test.ts b/apps/mobile/src/lib/legacy-html.test.ts
index 43281752c..86727e37d 100644
--- a/apps/mobile/src/lib/legacy-html.test.ts
+++ b/apps/mobile/src/lib/legacy-html.test.ts
@@ -19,6 +19,16 @@ describe("legacy HTML descriptions", () => {
).toBe("Hello **bold** and _soft_ & `x < y`\n\nNext \nline");
});
+ it("keeps Markdown characters in text literal", () => {
+ expect(
+ legacyHtmlToMarkdown(
+ "<p>Use *literal* markers, [draft](notes) and <code>a*b</code></p><p>1. not a list</p>",
+ ),
+ ).toBe(
+ "Use \\*literal\\* markers, \\[draft\\](notes) and `a*b`\n\n1\\. not a list",
+ );
+ });
+
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>';
diff --git a/apps/mobile/src/lib/legacy-html.ts b/apps/mobile/src/lib/legacy-html.ts
index 7f515c1f0..6ae5f1e0b 100644
--- a/apps/mobile/src/lib/legacy-html.ts
+++ b/apps/mobile/src/lib/legacy-html.ts
@@ -42,6 +42,13 @@ function decode(text: string): string {
);
}
+/** Keeps text literal, as the web shows it, instead of letting it read as Markdown. */
+function escapeText(text: string): string {
+ return text
+ .replace(/[\\`*_[\]<>#~|+-]/g, "\\$&")
+ .replace(/(\d)([.)])/g, "$1\\$2");
+}
+
function attr(raw: string, name: string): string | undefined {
const match = new RegExp(
`\\b${name}\\s*=\\s*(?:"([^"]*)"|'([^']*)')`,
@@ -95,13 +102,16 @@ export function legacyHtmlToMarkdown(html: string): string {
top().out += text;
};
let pre = 0;
+ let code = false;
for (const token of html.split(tagToken)) {
const tag = tagParts.exec(token);
if (!tag) {
if (pre) emit(decode(token));
- else if (token.trim()) emit(decode(token.replace(/\s+/g, " ")));
- else if (token && !/\s$/.test(top().out)) emit(" ");
+ else if (token.trim()) {
+ const text = decode(token.replace(/\s+/g, " "));
+ emit(code ? text : escapeText(text));
+ } else if (token && !/\s$/.test(top().out)) emit(" ");
continue;
}
@@ -122,7 +132,9 @@ export function legacyHtmlToMarkdown(html: string): string {
} else if (marks[name]) {
emit(marks[name]);
} else if (name === "code") {
- if (!pre) emit("`");
+ if (pre) continue;
+ code = !closing;
+ emit("`");
} else if (name === "pre") {
pre = Math.max(0, pre + (closing ? -1 : 1));
emit(closing ? "\n```\n\n" : "```\n");
diff --git a/apps/mobile/src/lib/markdown.ts b/apps/mobile/src/lib/markdown.ts
index 374313ddf..23022a8b7 100644
--- a/apps/mobile/src/lib/markdown.ts
+++ b/apps/mobile/src/lib/markdown.ts
@@ -68,6 +68,7 @@ export function toDisplayMarkdown(markdown: string, serverUrl: string): string {
export function toPlainText(markdown?: string | null): string {
if (!markdown) return "";
return toDisplayMarkdown(markdown, "")
+ .replace(/\\([!-/:-@[-`{-~])/g, "$1")
.replace(/!\[[^\]]*\]\([^)]*\)/g, "")
.replace(/\[([^\]]*)\]\([^)]*\)/g, "$1")
.replace(/<[^>]+>/g, "")