export function getPostCard(html: HTMLHtmlElement) { const title = html .querySelector('meta[property="cozy:title"]') ?.getAttribute("content") || html.querySelector("title")?.innerHTML?.replace("Cozy 🧸 | ", ""); const description = html .querySelector('meta[property="cozy:description"]') ?.getAttribute("content"); const image = html .querySelector('meta[property="cozy:image"]') ?.getAttribute("content"); const source = html .querySelector('meta[property="cozy:source"]') ?.getAttribute("content"); const published = html .querySelector('meta[property="cozy:published"]') ?.getAttribute("content"); const postCard = `
${ image ? ` ${title} | ${description} ` : '' }
${ source || published ? `
${ source && `

${source}

` } ${ published && `

${ new Date(published)?.toLocaleDateString() || "" }

` }
` : "" }

${title}

${ description ? `

${description}

` : "" }
`; return postCard; } export function renderPost(responseText, url, postDivSelector: string, preventPushState = false) { const postDiv = document.querySelector(postDivSelector); const html = document.createElement('html'); html.innerHTML = responseText; const newPost = html.querySelector('body')?.querySelector('#post'); if (postDiv && newPost?.innerHTML) { postDiv.innerHTML = newPost.innerHTML const appUrl = document.getElementById('app-url') as HTMLInputElement; const cozyUrl = html.querySelector('meta[property="cozy:url"]')?.getAttribute('content'); const homeBtn = document.getElementById('app-home') as HTMLButtonElement; const submitBtn = document.getElementById('submit') as HTMLButtonElement; if(cozyUrl !== '/') { appUrl.value = cozyUrl || ''; homeBtn.removeAttribute('disabled'); submitBtn.removeAttribute('disabled'); } else { appUrl.value = ''; homeBtn.setAttribute('disabled', 'true'); submitBtn.setAttribute('disabled', 'true'); } if(!preventPushState) { window.history.pushState({url}, '', url); } } }