From 165a00570e0913510268f085544acbe8b9882ea4 Mon Sep 17 00:00:00 2001 From: Ayo Ayco Date: Sat, 18 Jan 2025 22:23:52 +0100 Subject: [PATCH] feat: add enhance-content web component --- static/enhance-content.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 static/enhance-content.js diff --git a/static/enhance-content.js b/static/enhance-content.js new file mode 100644 index 0000000..d26c681 --- /dev/null +++ b/static/enhance-content.js @@ -0,0 +1,37 @@ +class EnhanceContent extends HTMLElement { + connectedCallback() { + const el = this.getElementsByClassName("hashtag"); + const server = this.dataset.server; + const tagUrl = this.dataset.tagUrl; + + for (let i = 0; i < el.length; i++) { + const tagEl = el.item(i); + const currentHref = tagEl.getAttribute("href"); + const tagName = currentHref.replace(`${server}/tags/`, ""); + tagEl.setAttribute("href", tagUrl + tagName); + + const parentEl = tagEl.parentElement; + const siblings = parentEl.childNodes; + let validSiblingsCount = 0; + + for (const sibling of siblings) { + if (!(sibling.nodeType === 3 && sibling.textContent === " ")) { + validSiblingsCount++; + } + } + + const childrenTags = parentEl.getElementsByClassName("hashtag"); + + console.log({ childrenTags, siblings, validSiblingsCount }); + + const isTagBar = validSiblingsCount === childrenTags.length; + + if (isTagBar) { + tagEl.textContent = tagName; + tagEl.classList.add("pill"); + } + } + } +} + +customElements.define("enhance-content", EnhanceContent);