changeset 19:435f474f07c7

urls
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 04 Nov 2024 19:23:38 -0700
parents 0721dcf222e1
children dade6a560494
files src/chat.js src/lib/Shared.luan
diffstat 2 files changed, 15 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/chat.js	Mon Nov 04 17:08:41 2024 -0700
+++ b/src/chat.js	Mon Nov 04 19:23:38 2024 -0700
@@ -31,7 +31,7 @@
 
 function gotChat(html) {
 	document.querySelector('div[posts]').innerHTML = html;
-	fixDates();
+	fixPosts();
 	document.querySelector('div[input] textarea').focus();
 	document.querySelector('div[input]').scrollIntoView({block: 'end'});
 }
@@ -61,12 +61,17 @@
 	}
 }
 
-function fixDates() {
+function fixPosts() {
 	let spans = document.querySelectorAll('span[when][fix]');
 	for( let span of spans ) {
 		span.textContent = new Date(Number(span.textContent)).toLocaleString();
 		span.removeAttribute('fix');
 	}
+	let divs = document.querySelectorAll('div[text][fix]');
+	for( let div of divs ) {
+		div.innerHTML = urlsToLinks(div.innerHTML);
+		div.removeAttribute('fix');
+	}
 }
 
 function deleteChat() {
@@ -76,7 +81,7 @@
 function added(html) {
 	let input = document.querySelector('div[input]');
 	input.insertAdjacentHTML('beforebegin',html);
-	fixDates();
+	fixPosts();
 	input.scrollIntoView({block: 'end'});
 	if( !document.hasFocus() )
 		document.title = title + ' *';
@@ -107,3 +112,9 @@
 window.onfocus = function() {
 	document.title = title;
 };
+
+let urlRegex = /(^|\s)(https?:\/\/\S+)/g;
+
+function urlsToLinks(text) {
+	return text.replace( urlRegex, '$1<a href="$2">$2</a>' );
+}
--- a/src/lib/Shared.luan	Mon Nov 04 17:08:41 2024 -0700
+++ b/src/lib/Shared.luan	Mon Nov 04 19:23:38 2024 -0700
@@ -100,7 +100,7 @@
 				<span author><%=author.email%></span>
 				<span when fix><%=post.date%></span>
 			</div>
-			<div text><%= html_encode(post.content) %></div>
+			<div text fix><%= html_encode(post.content) %></div>
 		</div>
 <%
 end