diff --git a/app/javascript/controllers/content_loader_controller.js b/app/javascript/controllers/content_loader_controller.js index 429ea148..67fbba52 100644 --- a/app/javascript/controllers/content_loader_controller.js +++ b/app/javascript/controllers/content_loader_controller.js @@ -7,20 +7,43 @@ export default class extends Controller { this.load() } + stripHtmlComments(input) { + let previous + let output = input + + do { + previous = output + output = output.replace(//g, '') + } while (output !== previous) + + return output + } + load() { fetch(this.urlValue) .then(response => response.text()) .then(html => { - const parentElement = this.element.parentElement; - // Replace the entire element with the fetched HTML - this.element.outerHTML = html; - // Hide primo links if libkey link is present - if (parentElement.querySelector('.libkey-link')) { - const resultGet = parentElement.closest('.result-get'); - if (resultGet) { - const primoLinks = resultGet.querySelectorAll('.primo-link'); - // removing instead of hiding to avoid layout issues when selecting which link to highlight - primoLinks.forEach(link => link.remove()); + const parentElement = this.element.parentElement + // Strip HTML comments and trim whitespace + const cleanedHtml = this.stripHtmlComments(html).trim() + // Replace the entire element with the fetched HTML, or remove if empty + if (cleanedHtml) { + this.element.outerHTML = cleanedHtml + // Hide primo links if libkey link is present + if (parentElement.querySelector('.libkey-link')) { + const resultGet = parentElement.closest('.result-get') + if (resultGet) { + const primoLinks = resultGet.querySelectorAll('.primo-link') + // removing instead of hiding to avoid layout issues when selecting which link to highlight + primoLinks.forEach(link => link.remove()) + } + } + } else { + // Remove only this loader element + this.element.remove(); + // Remove result-get container if it's now empty (no fulfillment links and no other content) + if (!parentElement.textContent.trim()) { + parentElement.remove(); } } })