| import type { Transformer } from "./types.js"; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export const hfUserTransformer: Transformer = { |
| name: "hfUser", |
| apply(document) { |
| for (const div of [...document.querySelectorAll('div[data-component="hfUser"]')]) { |
| const username = (div.getAttribute("username") || div.getAttribute("data-username") || "").trim(); |
| const rawName = (div.getAttribute("name") || div.getAttribute("data-name") || "").trim(); |
| const rawUrl = (div.getAttribute("url") || div.getAttribute("data-url") || "").trim(); |
|
|
| if (!username) { |
| div.remove(); |
| continue; |
| } |
|
|
| const displayName = rawName || username; |
| const profileUrl = rawUrl || `https://huggingface.co/${encodeURIComponent(username)}`; |
| const avatarSrc = `https://huggingface.co/api/users/${encodeURIComponent(username)}/avatar`; |
|
|
| const card = document.createElement("div"); |
| card.className = "hf-user"; |
|
|
| const left = document.createElement("div"); |
| left.className = "hf-user__left"; |
|
|
| const img = document.createElement("img"); |
| img.className = "hf-user__avatar"; |
| img.setAttribute("src", avatarSrc); |
| img.setAttribute("alt", `${displayName} avatar`); |
| img.setAttribute("width", "44"); |
| img.setAttribute("height", "44"); |
| img.setAttribute("loading", "lazy"); |
| img.setAttribute("decoding", "async"); |
| img.setAttribute("referrerpolicy", "no-referrer"); |
|
|
| const text = document.createElement("span"); |
| text.className = "hf-user__text"; |
|
|
| const nameLink = document.createElement("a"); |
| nameLink.className = "hf-user__name"; |
| nameLink.setAttribute("href", profileUrl); |
| nameLink.setAttribute("target", "_blank"); |
| nameLink.setAttribute("rel", "noopener noreferrer"); |
| nameLink.textContent = displayName; |
|
|
| const row = document.createElement("span"); |
| row.className = "hf-user__row"; |
|
|
| const userLink = document.createElement("a"); |
| userLink.className = "hf-user__username"; |
| userLink.setAttribute("href", profileUrl); |
| userLink.setAttribute("target", "_blank"); |
| userLink.setAttribute("rel", "noopener noreferrer"); |
| userLink.textContent = `@${username}`; |
|
|
| row.appendChild(userLink); |
| text.appendChild(nameLink); |
| text.appendChild(row); |
| left.appendChild(img); |
| left.appendChild(text); |
| card.appendChild(left); |
|
|
| div.replaceWith(card); |
| } |
| }, |
| }; |
|
|