mirror of
https://github.com/driftywinds/driftywinds.github.io.git
synced 2025-12-19 19:13:33 +00:00
Better URL regex
This commit is contained in:
22
js/app.js
22
js/app.js
@@ -91,16 +91,7 @@ fetch(sourceURL)
|
|||||||
previewScreenshots.insertAdjacentHTML("beforeend", `<img src="${url}" alt="">`);
|
previewScreenshots.insertAdjacentHTML("beforeend", `<img src="${url}" alt="">`);
|
||||||
});
|
});
|
||||||
|
|
||||||
let localizedDescription = app.localizedDescription;
|
previewDescription.innerHTML = formatString(app.localizedDescription);
|
||||||
|
|
||||||
const perviewDescriptionURLs = [...new Set(localizedDescription.match(urlRegex))]; // Creating set from array to remove duplicates
|
|
||||||
|
|
||||||
perviewDescriptionURLs.forEach(url => {
|
|
||||||
localizedDescription = localizedDescription.replaceAll(url, `<a href="${url}">${url}</a>`)
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
previewDescription.innerHTML = localizedDescription.replaceAll("\n", "<br>");
|
|
||||||
|
|
||||||
const more = `
|
const more = `
|
||||||
<a id="more" onclick="revealTruncatedText(this);">
|
<a id="more" onclick="revealTruncatedText(this);">
|
||||||
@@ -139,20 +130,13 @@ fetch(sourceURL)
|
|||||||
const units = ["B", "KB", "MB", "GB"];
|
const units = ["B", "KB", "MB", "GB"];
|
||||||
var appSize = app.size, c = 0;
|
var appSize = app.size, c = 0;
|
||||||
while (appSize > 1024) {
|
while (appSize > 1024) {
|
||||||
appSize = parseFloat(appSize/1024).toFixed(1);
|
appSize = parseFloat(appSize / 1024).toFixed(1);
|
||||||
c++;
|
c++;
|
||||||
}
|
}
|
||||||
versionSize.textContent = `${appSize} ${units[c]}`;
|
versionSize.textContent = `${appSize} ${units[c]}`;
|
||||||
|
|
||||||
// Version description
|
// Version description
|
||||||
var appVersionDescription = app.versionDescription;
|
versionDescription.innerHTML = formatString(app.versionDescription);
|
||||||
const urls = [...new Set(appVersionDescription.match(urlRegex))]; // Creating set from array to remove duplicates
|
|
||||||
|
|
||||||
urls.forEach(url =>
|
|
||||||
appVersionDescription = appVersionDescription.replaceAll(url, `<a href="${url}">${url}</a>`)
|
|
||||||
);
|
|
||||||
|
|
||||||
versionDescription.innerHTML = appVersionDescription.replaceAll("\n", "<br>");
|
|
||||||
if (versionDescription.scrollHeight > versionDescription.clientHeight)
|
if (versionDescription.scrollHeight > versionDescription.clientHeight)
|
||||||
versionDescription.insertAdjacentHTML("beforeend", more);
|
versionDescription.insertAdjacentHTML("beforeend", more);
|
||||||
|
|
||||||
|
|||||||
12
js/home.js
12
js/home.js
@@ -49,24 +49,16 @@ fetch(sourceURL)
|
|||||||
count++;
|
count++;
|
||||||
});
|
});
|
||||||
|
|
||||||
var description = json.description;
|
var description = formatString(json.description);
|
||||||
|
|
||||||
if (description) {
|
if (description) {
|
||||||
const urls = [...new Set(description.match(urlRegex))]; // Creating set from array to remove duplicates
|
|
||||||
|
|
||||||
urls.forEach(url =>
|
|
||||||
description = description.replaceAll(url, `<a href="${url}">${url}</a>`)
|
|
||||||
);
|
|
||||||
|
|
||||||
document.getElementById("about").insertAdjacentHTML("beforeend", `
|
document.getElementById("about").insertAdjacentHTML("beforeend", `
|
||||||
<div class="item">
|
<div class="item">
|
||||||
<p>${description.replaceAll("\n", "<br>")}</p>
|
<p>${description}</p>
|
||||||
</div>
|
</div>
|
||||||
`);
|
`);
|
||||||
} else {
|
} else {
|
||||||
document.getElementById("about").remove();
|
document.getElementById("about").remove();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
waitForAllImagesToLoad();
|
waitForAllImagesToLoad();
|
||||||
});
|
});
|
||||||
|
|||||||
14
js/main.js
14
js/main.js
@@ -7,8 +7,18 @@ if (!urlSearchParams.has('source') || !sourceURL) {
|
|||||||
window.location.replace("index.html");
|
window.location.replace("index.html");
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://stackoverflow.com/a/31760088
|
const urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; // https://stackoverflow.com/a/8943487
|
||||||
const urlRegex = /(https?:\/\/[^ ]*)/g; // "g": global flag; without this, match() returns only the first matching result
|
function formatString(string) {
|
||||||
|
if (!string) return undefined;
|
||||||
|
|
||||||
|
// URLs
|
||||||
|
const urlArray = string.match(urlRegex);
|
||||||
|
const urlSet = [...new Set(urlArray)]; // Converting to set to remove duplicates
|
||||||
|
urlSet.forEach(url => string = string.replaceAll(url, `<a href="${url}">${url}</a>`));
|
||||||
|
|
||||||
|
// New lines
|
||||||
|
return string.replaceAll("\n", "<br>");
|
||||||
|
}
|
||||||
|
|
||||||
// If source is not a URL
|
// If source is not a URL
|
||||||
if (!sourceURL.match(urlRegex)) {
|
if (!sourceURL.match(urlRegex)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user