Added support for links in lists. Either markdown-links or plain links

master
Eirik Th S 2021-06-21 12:45:36 +02:00
parent 04900313cb
commit b3ace89d8c
1 changed files with 32 additions and 0 deletions

View File

@ -204,6 +204,8 @@ class Store {
amount = amount || 1;
checked = Number(checked) || 0;
text = insertLinks(text);
try {
price = Number(price);
this.itemsObj[itemID] = { text: text, price: price, itemID: itemID, amount: amount, checked: checked };
@ -624,4 +626,34 @@ function handleAjaxErrors(jqxhr, textStatus, error){
else {
alert("An error occured:\n"+error);
}
}
function insertLinks(text){
let markdownLink = /\[([a-zA-ZæøåÆØÅ0-9 &+-]+)\]\(((?:|http(?:|s):\/\/)[a-zA-Z0-9\/.\-+?=&]+)\)/gm;
let m;
while((m = markdownLink.exec(text)) !== null){
// This is necessary to avoid infinite loops with zero-width matches
if(m.index === markdownLink.lastIndex){
markdownLink.lastIndex++;
}
text = text.replace(m[0], "<a href='"+m[2]+"' target='_blank'>"+m[1]+"</a>");
}
let linkRegex = /(?:([("']|)((?:[A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)(?:(?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/gm;
let n;
let inputText = text;
while((n = linkRegex.exec(inputText)) !== null){
// This is necessary to avoid infinite loops with zero-width matches
if(n.index === linkRegex.lastIndex){
linkRegex.lastIndex++;
}
if(n[1] === ""){
text = text.replace(n[0], "<a href='"+(n[0][0] !== "h"?"//":'')+n[0]+"' target='_blank'>"+(n[2].replace('https://','').replace('http://', ''))+(n[2].length<n[0].length?"/...":"")+"</a>");
}
}
return text;
}