Skip to content

Commit 9796d17

Browse files
committed
fix(mobile): text before a block inside a link keeps its space
1 parent d56f296 commit 9796d17

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

‎apps/mobile/src/lib/markdownDocument.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,10 @@ export function markdownHtmlContent(
174174
const replacement = htmlTagMarkdown(match[0], anchors, options);
175175
if (replacement === "[" && bangBefore(result, result.length))
176176
result = `${result.slice(0, -1)}\\!`;
177-
result += replacement;
177+
result +=
178+
anchors.open > 0 && /[\s[]/.test(result.at(-1) ?? "")
179+
? replacement.replace(/^ /, "")
180+
: replacement;
178181
end = match.index + match[0].length;
179182
}
180183
const text = content.slice(end);
@@ -200,7 +203,7 @@ function htmlTagMarkdown(
200203
return `![${alt}](${markdownDestination(src)})`;
201204
}
202205
// A blank line inside an open link label would end the paragraph before its "]".
203-
const blockBreak = anchors.open === 0 ? "\n\n" : tag[1] === "/" ? " " : "";
206+
const blockBreak = anchors.open === 0 ? "\n\n" : " ";
204207
if (/^<summary\b/i.test(tag)) return `${blockBreak}**`;
205208
if (/^<\/summary\s*>/i.test(tag)) return `**${blockBreak}`;
206209
// A line break would end the table row, so a cell keeps its content on one line.

‎apps/mobile/src/lib/markdownHtml.test.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,17 @@ describe("HTML in native Markdown", () => {
147147

148148
it("keeps an anchor around block content a single link", () => {
149149
const document = parseMarkdownDocument(
150-
"<div><a href='https://example.com'><p>Open</p><p>docs</p></a></div>",
150+
"<div><a href='https://example.com'><p>Open</p><p>the</p>docs<p>here</p></a></div>",
151151
);
152152
expect(document.children).toMatchObject([
153153
{
154154
type: "paragraph",
155155
children: [
156-
{ type: "link", url: "https://example.com", children: [{ value: "Open docs " }] },
156+
{
157+
type: "link",
158+
url: "https://example.com",
159+
children: [{ value: "Open the docs here " }],
160+
},
157161
],
158162
},
159163
]);

0 commit comments

Comments
 (0)