add markdown-style link formatting

This commit is contained in:
Lephenixnoir 2023-08-08 22:07:30 +02:00
parent f20e40a9d5
commit 3a3527c6f9
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
2 changed files with 29 additions and 8 deletions

View File

@ -17,7 +17,6 @@ body {
--shoutbox-tab-selected-fg: white;
--shoutbox-message-date-color: #949494;
--shoutbox-message-author-color: #777777;
--shoutbox-message-bot-color: gray;
--shoutbox-message-bg1: #ffffff;
--shoutbox-message-bg2: #f8f8f8;
}
@ -149,11 +148,6 @@ body {
word-break: break-word;
white-space: pre-wrap;
}
#v5shoutbox .message[data-author="GLaDOS"] .message-content,
#v5shoutbox .message[data-author="Gitea"] .message-content {
font-style: italic;
color: var(--shoutbox-message-bot-color);
}
#v5shoutbox .message .message-date {
color: var(--shoutbox-message-date-color);

View File

@ -374,6 +374,32 @@ let shoutbox = new function() {
return url.length;
};
/* Match up to the start of the link */
const rLink = /\[([^\]]+)\]\((https?:\/\/|ftp:\/\/|magnet:)/d;
const fLink = (match) => {
/* Read link while keeping balance parentheses. Compared to raw URLs, we
allow spaces, and also keep quotes/commas as last characters since
there is a reasonable delimiter */
let i = match.indices[0][1];
let par_depth = 0;
while(i < match.input.length) {
par_depth += (match.input[i] == "(");
par_depth -= (match.input[i] == ")");
if(par_depth < 0)
break;
i++;
}
const url = match.input.substring(match.indices[2][0], i);
const a = document.createElement("a");
a.href = url;
a.target = "_blank";
a.appendChild(document.createTextNode(match[1]));
element.appendChild(a);
return i + (i < match.input.length) - match.indices[0][0];
};
const rInlineCode = /`([^`]+)`/;
const fInlineCode = (match) => {
const code = document.createElement("code");
@ -385,6 +411,7 @@ let shoutbox = new function() {
/* List of matchers: regex, handling function, match object, index. */
let matchers = [
[rURL, fURL, null, -1],
[rLink, fLink, null, -1],
[rInlineCode, fInlineCode, null, -1],
];
@ -397,10 +424,10 @@ let shoutbox = new function() {
/* Update the next matches for all regexes and find the one that matches
the earliest. */
for(const m of matchers) {
if(m[3] < 0) {
if(m[3] < i) {
m[0].lastIndex = 0;
m[2] = m[0].exec(message.substring(i));
m[3] = (m[2] !== null) ? i + m[2].index : message.length;
m[3] = (m[2] !== null) ? i + m[2].index : -1;
}
if(m[3] >= 0 && m[3] < next) {
next = m[3];