Skip to content
Snippets Groups Projects
Commit bc66a6c5 authored by Joscha Schmiedt's avatar Joscha Schmiedt
Browse files

FIX: error in regexp to match URLs

The previous regex did not allow urls without top-level domain, i.e. http://localhost was not allowed.
parent 58cc8f1a
No related branches found
No related tags found
2 merge requests!131FIX: error in regexp to match URLs,!128F 344 extern linkify absorbs commas etc into link adress
Pipeline #46956 passed
/*
* This file is a part of the LinkAhead Project.
*
* Copyright (C) 2021-2023 IndiScale GmbH <info@indiscale.com>
* Copyright (C) 2021-2024 IndiScale GmbH <info@indiscale.com>
* Copyright (C) 2021 Timm Fitschen <t.fitschen@indiscale.com>
* Copyright (C) 2023 Florian Spreckelsen <f.spreckelsen@indiscale.com>
* Copyright (C) 2023 Daniel Hornung <d.hornung@indiscale.com>
* Copyright (C) 2024 Joscha Schmiedt <joscha@schmiedt.dev>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
......@@ -64,16 +65,17 @@ var cosmetics = new function () {
/**
* Return a string with all occurences of a http(s) url in a given string replaced by <a> elements
* @param {string} text - The text to be searched for URLs
* @returns {string} text with <a> elements instead of raw links
*/
function _linkify_text(text) {
const match_url_regex = /((https?:\/\/)?([a-z]+\.)+[a-z]{2,}(\/[^\s]*)?)/gi;
function linkify_string(text) {
const match_url_regex = /https?:\/\/[^\s]*\b(?![,:;?!])/gi;
return text.replace(match_url_regex, function (href) {
var link_text = href;
if (_link_cut_off_length > 4 && link_text.length > _link_cut_off_length) {
link_text = link_text.substring(0, _link_cut_off_length - 5) + "[...]";
}
return `<a title="Open ${href} in a new tab." target="_blank"class="caosdb-v-property-href-value" href="${href}">${link_text}<i class="bi bi-box-arrow-up-right"></i></a>`;
return `<a title="Open ${href} in a new tab." target="_blank" class="caosdb-v-property-href-value" href="${href}">${link_text} <i class="bi bi-box-arrow-up-right"></i></a>`;
});
}
......@@ -83,7 +85,7 @@ var cosmetics = new function () {
var _linkify_all_text_values = function () {
$('.caosdb-f-property-text-value').each(function (index) {
if (!($(this).hasClass("caosdb-v-property-linkified")) && (/https?:\/\//.test(this.innerText))) {
let linkified_text_value = _linkify_text(this.innerText);
var linkified_text_value = linkify_string(this.innerText);
// add class to highlight that this has been linkified already
// (see https://gitlab.com/caosdb/caosdb-webui/-/issues/199).
$(this).addClass("caosdb-v-property-linkified")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment