send links to the LLM as markdown so drafts keep real hyperlinks

Transcripts flattened links to "text ( url )", so the model copied that shape and drafts came back with the URL in brackets instead of a link. HTML to text for the LLM now writes "[text](url)", which Markdown2HTML turns back into an anchor.
This commit is contained in:
Abhinav Raut
2026-07-31 18:02:22 +05:30
parent da378e7615
commit 2e89586a11
8 changed files with 98 additions and 16 deletions
+24 -4
View File
@@ -297,16 +297,16 @@ func TestSplitName(t *testing.T) {
}
}
func TestHTML2TextWithLinks(t *testing.T) {
func TestHTML2TextMarkdownLinks(t *testing.T) {
tests := []struct {
name string
html string
want string
}{
{
name: "link with distinct text keeps url",
name: "link with distinct text becomes a markdown link",
html: `<p>See <a href="https://example.com/guide">the guide</a> for steps.</p>`,
want: "See the guide ( https://example.com/guide ) for steps.",
want: "See [the guide](https://example.com/guide) for steps.",
},
{
name: "link text equal to url not duplicated",
@@ -318,10 +318,30 @@ func TestHTML2TextWithLinks(t *testing.T) {
html: `<p>No links here.</p>`,
want: "No links here.",
},
{
name: "nested markup inside the anchor flattens to link text",
html: `<p><a href="https://example.com/x"><strong>Pay</strong> now</a></p>`,
want: "[Pay now](https://example.com/x)",
},
{
name: "brackets in link text are escaped",
html: `<p><a href="https://example.com">Docs [beta]</a></p>`,
want: `[Docs \[beta\]](https://example.com)`,
},
{
name: "url with parentheses is wrapped in angle brackets",
html: `<p><a href="https://example.com/a(b)">See</a></p>`,
want: "[See](<https://example.com/a(b)>)",
},
{
name: "anchor without href keeps its text",
html: `<p><a name="top">Top</a> of page.</p>`,
want: "Top of page.",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := HTML2TextWithLinks(tt.html); got != tt.want {
if got := HTML2TextMarkdownLinks(tt.html); got != tt.want {
t.Errorf("got %q, want %q", got, tt.want)
}
})