Mercurial Hosting > chat
changeset 118:04933d5ba05a
handle markdown
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Thu, 04 Dec 2025 22:57:00 -0700 |
| parents | e2deb5236f26 |
| children | ec1a61bf5738 |
| files | src/chat.js src/index.html.luan |
| diffstat | 2 files changed, 8 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/src/chat.js Thu Dec 04 21:43:47 2025 -0700 +++ b/src/chat.js Thu Dec 04 22:57:00 2025 -0700 @@ -92,10 +92,10 @@ whenSpan.textContent = new Date(Number(whenSpan.textContent)).toLocaleString([],{dateStyle:'short',timeStyle:'short'}); } let textDiv = div.querySelector('div[text]'); - textDiv.innerHTML = urlsToLinks(textDiv.innerHTML); + textDiv.innerHTML = handleMarkdown(textDiv.innerHTML); let reply = div.querySelector('blockquote'); if( reply ) - reply.innerHTML = urlsToLinks(reply.innerHTML); + reply.innerHTML = handleMarkdown(reply.innerHTML); let html; if( div.getAttribute('author') === userId ) { html = document.querySelector('div[hidden] span[pulldown=author]').innerHTML; @@ -251,12 +251,12 @@ active(); }; -let urlRegex = /(^|\s)(https?:\/\/\S+)/g; -let filebinUrlRegex = /(^|\s)(https:\/\/filebin.net\/[0-9a-f]+\/(\S+))/g; +let converter = window.markdownit({linkify:true}); +let filebinUrlRegex = />https:\/\/filebin.net\/[0-9a-f]+\/([^<]+)</g; -function urlsToLinks(text) { - text = text.replace( filebinUrlRegex, '$1<a target="_blank" href="$2">$3</a>' ); - text = text.replace( urlRegex, '$1<a target="_blank" href="$2">$2</a>' ); +function handleMarkdown(text) { + text = converter.render(text); + text = text.replace( filebinUrlRegex, '>$1<' ); return text; }
--- a/src/index.html.luan Thu Dec 04 21:43:47 2025 -0700 +++ b/src/index.html.luan Thu Dec 04 22:57:00 2025 -0700 @@ -33,6 +33,7 @@ <style> @import "chat.css?s=<%=started%>"; </style> + <script src="https://cdn.jsdelivr.net/npm/markdown-it@14.1.0/dist/markdown-it.min.js"></script> <script src="chat.js?s=<%=started%>"></script> </head> <body>
