Skip to content

Instantly share code, notes, and snippets.

@sukima

sukima/poem.js Secret

Created August 8, 2025 02:22
Show Gist options
  • Select an option

  • Save sukima/25dd434cda8a197f1e1f3ac1f217a6d1 to your computer and use it in GitHub Desktop.

Select an option

Save sukima/25dd434cda8a197f1e1f3ac1f217a6d1 to your computer and use it in GitHub Desktop.
JS does not need to be over complicated
/**
* @typedef {object} PoemData
* @prop {string} title
* @prop {string} author
* @prop {string[]} lines
* @prop {string} linecount
*/
const container = document.getElementById('poem-cont');
container.innerHTML = 'Loading poem…';
const response = await fetch('https://poems.4-walls.net/random');
if (!response.ok) {
throw new Error(
`Unable to fetch poem data: ${response.statusText}`,
{ cause: response },
);
}
/** @type {PoemData[]} */
const [poemData] = await response.json();
container.innerHTML = `<h2></h2><cite></cite><p></p>`;
container.querySelector('h2').textContent = poemData.title;
container.querySelector('cite').textContent = poemData.author;
const poemContainer = cont.querySelector('p');
for (const line of poemData.lines) {
poemContainer.append(line, document.createElement('br'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment