rep defect 42, questionnaire defect 67, enf defect 16

This commit is contained in:
2025-01-30 14:11:17 +00:00
parent 390758bfd3
commit 6fb4f5722a
+40 -17
View File
@@ -561,14 +561,23 @@ export const updateLinks = (jsonObj) => {
// Find all text nodes in the document // Find all text nodes in the document
const walk = (node) => { const walk = (node) => {
if (node.nodeType === Node.TEXT_NODE) { if (node.nodeType === Node.TEXT_NODE) {
// Check if the text node contains a URL (simple check for a URL pattern)
const urlRegex = const urlRegex =
/(?:https?:\/\/)?(?:www\.)?([a-zA-Z0-9.-]+\.[a-zA-Z]{2,})(\/[^\s]*)?/g; /(?:https?:\/\/)?(?:www\.)?([a-zA-Z0-9.-]+\.[a-zA-Z]{2,})(\/[^\s]*)?/g;
let match; let match;
let currentText = node.textContent;
let lastIndex = 0;
let newNodeContent = [];
// Process the text node if it contains a URL // Process all URLs found in the current text node
while ((match = urlRegex.exec(node.textContent)) !== null) { while ((match = urlRegex.exec(currentText)) !== null) {
// Construct the full URL by adding the protocol if missing, and the domain and path // Before the URL
if (match.index > lastIndex) {
newNodeContent.push(
currentText.slice(lastIndex, match.index)
);
}
// Construct the full URL (default to https://)
let fullUrl = match[0]; let fullUrl = match[0];
if ( if (
!fullUrl.startsWith("http://") && !fullUrl.startsWith("http://") &&
@@ -577,21 +586,35 @@ export const updateLinks = (jsonObj) => {
fullUrl = `https://${match[1]}${match[2] || ""}`; // Default to https fullUrl = `https://${match[1]}${match[2] || ""}`; // Default to https
} }
// Check if the match is already inside an <a> tag, if so, skip it // Create the anchor element
if (node.parentNode.tagName !== "A") { const anchor = document.createElement("a");
const anchor = document.createElement("a"); anchor.href = fullUrl;
anchor.href = fullUrl; anchor.textContent = match[0];
anchor.textContent = match[0]; anchor.setAttribute("rel", "noopener noreferrer");
// Add rel="noopener noreferrer" for security // Add the anchor tag to the node content
anchor.setAttribute("rel", "noopener noreferrer"); newNodeContent.push(anchor);
// Replace the URL with the anchor element in the text node // Update the last index processed
const range = document.createRange(); lastIndex = urlRegex.lastIndex;
range.selectNodeContents(node); }
range.deleteContents();
range.insertNode(anchor); // Add the remaining text after the last URL
} if (lastIndex < currentText.length) {
newNodeContent.push(currentText.slice(lastIndex));
}
// If there were any replacements, update the text node
if (newNodeContent.length > 0) {
const fragment = document.createDocumentFragment();
newNodeContent.forEach((item) => {
if (typeof item === "string") {
fragment.appendChild(document.createTextNode(item));
} else {
fragment.appendChild(item);
}
});
node.parentNode.replaceChild(fragment, node);
} }
} }