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 bold and code from older HTML descriptions intact

Tin Severee50ace6a75f
diff --git a/apps/mobile/src/lib/legacy-html.test.ts b/apps/mobile/src/lib/legacy-html.test.ts
index 86727e37d..f5617037a 100644
--- a/apps/mobile/src/lib/legacy-html.test.ts
+++ b/apps/mobile/src/lib/legacy-html.test.ts
@@ -16,7 +16,7 @@ describe("legacy HTML descriptions", () => {
legacyHtmlToMarkdown(
"<p>Hello <strong>bold</strong> and <em>soft</em> &amp; <code>x &lt; y</code></p><p>Next<br>line</p>",
),
- ).toBe("Hello **bold** and _soft_ & `x < y`\n\nNext \nline");
+ ).toBe("Hello **bold** and *soft* & `x < y`\n\nNext \nline");
});
it("keeps Markdown characters in text literal", () => {
@@ -29,6 +29,14 @@ describe("legacy HTML descriptions", () => {
);
});
+ it("keeps spaces outside marks and backticks inside code", () => {
+ expect(
+ legacyHtmlToMarkdown(
+ "<p><strong>Hello </strong>world, <em>in</em>line and <code>echo `hostname`</code></p>",
+ ),
+ ).toBe("**Hello** world, *in*line and `` echo `hostname` ``");
+ });
+
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 6ae5f1e0b..29bcd2c61 100644
--- a/apps/mobile/src/lib/legacy-html.ts
+++ b/apps/mobile/src/lib/legacy-html.ts
@@ -16,13 +16,21 @@ const entities: Record<string, string> = {
const marks: Record<string, string> = {
strong: "**",
b: "**",
- em: "_",
- i: "_",
+ em: "*",
+ i: "*",
s: "~~",
del: "~~",
strike: "~~",
};
-const framed = new Set(["ul", "ol", "li", "blockquote", "a"]);
+const framed = new Set([
+ "ul",
+ "ol",
+ "li",
+ "blockquote",
+ "a",
+ "code",
+ ...Object.keys(marks),
+]);
type Frame = { tag: string; attrs: string; out: string; items: number };
@@ -87,7 +95,24 @@ function finish(frame: Frame, parent: Frame): string {
.split("\n")
.map((line) => (line ? `> ${line}` : ">"))
.join("\n")}\n\n`;
+ case "code": {
+ const longest = Math.max(
+ 0,
+ ...(frame.out.match(/`+/g) ?? []).map((run) => run.length),
+ );
+ const fence = "`".repeat(longest + 1);
+ const pad = /^`|`$/.test(frame.out) ? " " : "";
+ return frame.out ? `${fence}${pad}${frame.out}${pad}${fence}` : "";
+ }
default: {
+ const mark = marks[frame.tag];
+ if (mark) {
+ // Markdown ignores delimiters next to inner spaces, so keep spaces outside.
+ const [, lead, core, trail] = /^(\s*)([\s\S]*?)(\s*)$/.exec(
+ frame.out,
+ ) as RegExpExecArray;
+ return core ? `${lead}${mark}${core}${mark}${trail}` : frame.out;
+ }
const href = attr(frame.attrs, "href");
return href ? `[${frame.out}](${href})` : frame.out;
}
@@ -102,7 +127,6 @@ 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);
@@ -110,7 +134,8 @@ export function legacyHtmlToMarkdown(html: string): string {
if (pre) emit(decode(token));
else if (token.trim()) {
const text = decode(token.replace(/\s+/g, " "));
- emit(code ? text : escapeText(text));
+ const inCode = stack.some((frame) => frame.tag === "code");
+ emit(inCode ? text : escapeText(text));
} else if (token && !/\s$/.test(top().out)) emit(" ");
continue;
}
@@ -119,6 +144,8 @@ export function legacyHtmlToMarkdown(html: string): string {
const name = rawName.toLowerCase();
if (name === "img" || name.startsWith("kaneo-")) {
emit(token);
+ } else if (name === "code" && pre) {
+ continue;
} else if (framed.has(name)) {
if (!closing) {
stack.push({ tag: name, attrs: rawAttrs, out: "", items: 0 });
@@ -129,12 +156,6 @@ export function legacyHtmlToMarkdown(html: string): string {
top().out += finish(frame, top());
} while (frame.tag !== name);
}
- } else if (marks[name]) {
- emit(marks[name]);
- } else if (name === "code") {
- if (pre) continue;
- code = !closing;
- emit("`");
} else if (name === "pre") {
pre = Math.max(0, pre + (closing ? -1 : 1));
emit(closing ? "\n```\n\n" : "```\n");