properly handle url= tags

This commit is contained in:
Lera Elvoé 2025-05-26 00:29:15 +03:00
parent 1e9809e4b2
commit a2d3672fa8
Signed by: yagich
SSH Key Fingerprint: SHA256:6xjGb6uA7lAVcULa7byPEN//rQ0wPoG+UzYVMfZnbvc
2 changed files with 12 additions and 5 deletions

View File

@ -23,15 +23,17 @@ function babycode.to_html(s, escape_html)
end end
end) end)
local url_tags = {}
-- replace `[url=https://example.com]Example[/url] tags -- replace `[url=https://example.com]Example[/url] tags
text = text:gsub("%[url=([^%]]+)%](.-)%[/url%]", function(url, label) text = text:gsub("%[url=([^%]]+)%](.-)%[/url%]", function(url, label)
return '<a href="'..url..'">'..label..'</a>' table.insert(url_tags, {url = url, label = label})
return "\1URL:"..#url_tags.."\1"
end) end)
-- replace `[url]https://example.com[/url] tags -- replace `[url]https://example.com[/url] tags
text = text:gsub("%[url%]([^%]]+)%[/url%]", function(url) -- text = text:gsub("%[url%]([^%]]+)%[/url%]", function(url)
return '<a href="'..url..'">'..url..'</a>' -- return '<a href="'..url..'">'..url..'</a>'
end) -- end)
-- bold, italics, strikethrough -- bold, italics, strikethrough
text = text:gsub("%[b%](.-)%[/b%]", "<strong>%1</strong>") text = text:gsub("%[b%](.-)%[/b%]", "<strong>%1</strong>")
@ -48,6 +50,11 @@ function babycode.to_html(s, escape_html)
return url return url
end) end)
text = text:gsub("\1URL:(%d+)\1", function(n)
local url = url_tags[tonumber(n)]
return ("<a href=%s>%s</a>"):format(url.url, url.label)
end)
-- rule -- rule
text = text:gsub("\n+%-%-%-", "<hr>") text = text:gsub("\n+%-%-%-", "<hr>")

View File

@ -1,11 +1,11 @@
<details> <details>
<summary>babycode guide</summary> <summary>babycode guide</summary>
<ul> <ul>
<li>Loose links will be converted to clickable links automatically</li>
<li>[b]<b>bold</b>[/b]</li> <li>[b]<b>bold</b>[/b]</li>
<li>[i]<i>italic</i>[/i]</li> <li>[i]<i>italic</i>[/i]</li>
<li>[s]<del>strikethrough</del>[/s]</li> <li>[s]<del>strikethrough</del>[/s]</li>
<li>[url=https://example.com]<a href="https://example.com">labeled URL</a>[/url]</li> <li>[url=https://example.com]<a href="https://example.com">labeled URL</a>[/url]</li>
<li>[url]<a href="https://unlabeled-url.example.com">https://unlabeled-url.example.com</a>[/url]</li>
<li> <li>
[code]with<br>line breaks[/code] will produce a code block: [code]with<br>line breaks[/code] will produce a code block:
<details> <details>