fix(telegram): properly nest link tags inside bold/italic

This commit is contained in:
pradeep-peddineni-amida 2026-01-29 18:35:31 -05:00
parent 4583f88626
commit 382a20cf19
2 changed files with 24 additions and 7 deletions

View File

@ -93,13 +93,8 @@ export function renderMarkdownWithMarkers(ir: MarkdownIR, options: RenderOptions
for (let i = 0; i < points.length; i += 1) { for (let i = 0; i < points.length; i += 1) {
const pos = points[i]; const pos = points[i];
while (stack.length && stack[stack.length - 1]?.end === pos) { // Close links before styles to maintain proper nesting order
const span = stack.pop(); // (links are typically nested inside styles like bold/italic)
if (!span) break;
const marker = styleMarkers[span.style];
if (marker) out += marker.close;
}
const closingLinks = linkEnds.get(pos); const closingLinks = linkEnds.get(pos);
if (closingLinks && closingLinks.length > 0) { if (closingLinks && closingLinks.length > 0) {
for (const link of closingLinks) { for (const link of closingLinks) {
@ -107,6 +102,13 @@ export function renderMarkdownWithMarkers(ir: MarkdownIR, options: RenderOptions
} }
} }
while (stack.length && stack[stack.length - 1]?.end === pos) {
const span = stack.pop();
if (!span) break;
const marker = styleMarkers[span.style];
if (marker) out += marker.close;
}
const openingLinks = linkStarts.get(pos); const openingLinks = linkStarts.get(pos);
if (openingLinks && openingLinks.length > 0) { if (openingLinks && openingLinks.length > 0) {
for (const link of openingLinks) { for (const link of openingLinks) {

View File

@ -47,4 +47,19 @@ describe("markdownToTelegramHtml", () => {
const res = markdownToTelegramHtml("```js\nconst x = 1;\n```"); const res = markdownToTelegramHtml("```js\nconst x = 1;\n```");
expect(res).toBe("<pre><code>const x = 1;\n</code></pre>"); expect(res).toBe("<pre><code>const x = 1;\n</code></pre>");
}); });
it("properly nests link tags inside bold when URL is at end of bold span", () => {
const res = markdownToTelegramHtml("**Check https://example.com**");
expect(res).toBe('<b>Check <a href="https://example.com">https://example.com</a></b>');
});
it("properly nests link tags inside italic", () => {
const res = markdownToTelegramHtml("_visit https://docs.molt.bot_");
expect(res).toBe('<i>visit <a href="https://docs.molt.bot">https://docs.molt.bot</a></i>');
});
it("renders emoji before bold correctly", () => {
const res = markdownToTelegramHtml("🧪 **Header text**");
expect(res).toBe("🧪 <b>Header text</b>");
});
}); });