|
| 1 | +import { LIBS } from '../../scripts/scripts.js'; |
| 2 | + |
| 3 | +const iconsModule = await import(`${LIBS}/features/icons/icons.js`).catch(() => null); |
| 4 | +const loadIcons = iconsModule?.default ?? (() => {}); |
| 5 | + |
| 6 | +const SOCIAL_PLATFORMS = { |
| 7 | + 'linkedin.com': { name: 'LinkedIn', icon: 'linkedin' }, |
| 8 | + 'twitter.com': { name: 'X', icon: 'twitter' }, |
| 9 | + 'x.com': { name: 'X', icon: 'twitter' }, |
| 10 | + 'facebook.com': { name: 'Facebook', icon: 'facebook' }, |
| 11 | + 'instagram.com': { name: 'Instagram', icon: 'instagram' }, |
| 12 | +}; |
| 13 | + |
| 14 | +function resolvePlatform(href) { |
| 15 | + return Object.keys(SOCIAL_PLATFORMS).find((domain) => href?.includes(domain)); |
| 16 | +} |
| 17 | + |
| 18 | +function decorateSocial(row) { |
| 19 | + const links = [...row.querySelectorAll('a')]; |
| 20 | + row.replaceChildren(...links); |
| 21 | + row.className = 'blog-author-social'; |
| 22 | + links.forEach((a) => { |
| 23 | + const domain = resolvePlatform(a.href); |
| 24 | + if (!domain) { a.hidden = true; return; } |
| 25 | + const { name, icon } = SOCIAL_PLATFORMS[domain]; |
| 26 | + a.setAttribute('aria-label', name); |
| 27 | + a.target = '_blank'; |
| 28 | + a.rel = 'noopener noreferrer'; |
| 29 | + const span = document.createElement('span'); |
| 30 | + span.className = `icon icon-${icon}`; |
| 31 | + a.replaceChildren(span); |
| 32 | + }); |
| 33 | + loadIcons(row.querySelectorAll('span.icon')); |
| 34 | +} |
| 35 | + |
| 36 | +function injectSchema(el, company) { |
| 37 | + const name = el.querySelector('.blog-author-name')?.textContent; |
| 38 | + if (!name) return; |
| 39 | + |
| 40 | + const schema = { |
| 41 | + '@context': 'https://schema.org', |
| 42 | + '@type': 'Person', |
| 43 | + name, |
| 44 | + url: window.location.href, |
| 45 | + }; |
| 46 | + |
| 47 | + if (company) { |
| 48 | + schema.worksFor = { '@type': 'Organization', name: company }; |
| 49 | + } |
| 50 | + |
| 51 | + const title = el.querySelector('.blog-author-title')?.textContent; |
| 52 | + if (title) schema.jobTitle = title; |
| 53 | + |
| 54 | + const desc = [...el.querySelectorAll('.blog-author-description')] |
| 55 | + .map((p) => p.textContent).join(' '); |
| 56 | + if (desc) schema.description = desc; |
| 57 | + |
| 58 | + const img = el.querySelector('picture img')?.src; |
| 59 | + if (img) schema.image = img; |
| 60 | + |
| 61 | + const sameAs = [...el.querySelectorAll('.blog-author-social a:not([hidden])')] |
| 62 | + .map((a) => a.getAttribute('aria-label') && a.href).filter(Boolean); |
| 63 | + if (sameAs.length) schema.sameAs = sameAs; |
| 64 | + |
| 65 | + const script = document.createElement('script'); |
| 66 | + script.type = 'application/ld+json'; |
| 67 | + script.textContent = JSON.stringify(schema); |
| 68 | + document.head.append(script); |
| 69 | +} |
| 70 | + |
| 71 | +export default async function init(el) { |
| 72 | + let socialContainer = null; |
| 73 | + let textIdx = 0; |
| 74 | + let company = null; |
| 75 | + const HEX = '#[0-9a-fA-F]{3,6}'; |
| 76 | + const TEXT_CLASSES = ['blog-author-name', 'blog-author-title', 'blog-author-description']; |
| 77 | + |
| 78 | + function decorateRow(row) { |
| 79 | + const text = row.textContent.trim(); |
| 80 | + |
| 81 | + const gradient = text.match(new RegExp(`^(${HEX})\\s*,\\s*(${HEX})$`)); |
| 82 | + if (gradient) { |
| 83 | + el.style.background = `linear-gradient(to bottom, ${gradient[1]}, ${gradient[2]})`; |
| 84 | + row.parentElement.remove(); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + if (new RegExp(`^${HEX}$`).test(text)) { |
| 89 | + el.style.backgroundColor = text; |
| 90 | + row.parentElement.remove(); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + if (row.querySelector('picture')) { |
| 95 | + row.className = 'blog-author-image'; |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + if ([...row.querySelectorAll('a')].some((a) => resolvePlatform(a.href))) { |
| 100 | + if (!socialContainer) { |
| 101 | + socialContainer = row; |
| 102 | + } else { |
| 103 | + [...row.querySelectorAll('a')].forEach((a) => socialContainer.append(a)); |
| 104 | + row.parentElement.remove(); |
| 105 | + } |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + if (socialContainer) { |
| 110 | + company = text; |
| 111 | + row.parentElement.remove(); |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + row.className = TEXT_CLASSES[Math.min(textIdx, 2)]; |
| 116 | + textIdx += 1; |
| 117 | + } |
| 118 | + |
| 119 | + el.querySelectorAll(':scope > div > div').forEach(decorateRow); |
| 120 | + |
| 121 | + if (socialContainer) decorateSocial(socialContainer); |
| 122 | + |
| 123 | + const content = document.createElement('div'); |
| 124 | + content.className = 'blog-author-content'; |
| 125 | + el.querySelectorAll('.blog-author-name, .blog-author-title, .blog-author-description, .blog-author-social') |
| 126 | + .forEach((t) => content.append(t)); |
| 127 | + el.querySelectorAll(':scope > div:not(:has(*))').forEach((d) => d.remove()); |
| 128 | + el.append(content); |
| 129 | + |
| 130 | + injectSchema(el, company); |
| 131 | +} |
0 commit comments