From 0d3f38bce1b028b147b999bd2164d768079b25df Mon Sep 17 00:00:00 2001 From: tmaret Date: Thu, 11 Jan 2024 22:36:27 +0100 Subject: [PATCH] wip author block --- cigaradvisor/blocks/author/author.css | 4 ++++ cigaradvisor/blocks/author/author.js | 30 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 cigaradvisor/blocks/author/author.css create mode 100644 cigaradvisor/blocks/author/author.js diff --git a/cigaradvisor/blocks/author/author.css b/cigaradvisor/blocks/author/author.css new file mode 100644 index 00000000..c54d42bd --- /dev/null +++ b/cigaradvisor/blocks/author/author.css @@ -0,0 +1,4 @@ +.author { + display: flex; + flex-wrap: wrap; +} diff --git a/cigaradvisor/blocks/author/author.js b/cigaradvisor/blocks/author/author.js new file mode 100644 index 00000000..3c850f68 --- /dev/null +++ b/cigaradvisor/blocks/author/author.js @@ -0,0 +1,30 @@ +/** + * Loads an author. + * @param {string} path The path to the author + * @returns {HTMLElement} The root element of the author + */ +async function loadAuthor(path) { + if (path && path.startsWith('/')) { + const resp = await fetch(`${path}.plain.html`); + if (resp.ok) { + const div = document.createElement('div'); + div.innerHTML = await resp.text(); + return div; + } + } + return null; +} + +export default async function decorate(block) { + const link = block.querySelector('a'); + const path = link ? link.getAttribute('href') : block.textContent.trim(); + block.innerHTML = ''; + const author = await loadAuthor(path); + if (author) { + // add updated link to all author articles + const authorName = author.querySelector('h2').innerHTML; + link.textContent = `Show all ${authorName}'s Articles`; + author.append(link); + block.append(author); + } +}