export function getPostCard(html: HTMLHtmlElement) { const {title, description, image, source, published} = getPostMeta(html); const postCard = `
${ image ? ` ${title} | ${description} ` : '' }
${ source || published ? `
${ source && `

${source}

` } ${ published && `

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

` }
` : "" }

${title}

${ description ? `

${description}

` : "" }
`; return postCard; } export function renderPost(responseText: string | null, url, postDivSelector: string, preventPushState = false) { const postDiv = document.querySelector(`#${postDivSelector}`); let postText = ''; let cozyUrl = '/'; let cozyTitle = 'Cozy'; if (responseText) { const html = document.createElement('html'); html.innerHTML = responseText; const newPost = html.querySelector('body')?.querySelector('#post'); postText = newPost?.innerHTML || ''; cozyUrl = html.querySelector('meta[property="cozy:url"]')?.getAttribute('content') ?? '/'; cozyTitle = `${getCozyTitle(html)} | Cozy`; } if (postDiv) { postDiv.innerHTML = postText; const appUrl = document.getElementById('app-url') as HTMLInputElement; const homeBtn = document.querySelector('#app-home'); const backBtn = document.querySelector('#app-back'); const submitBtn = document.querySelector('#app-submit'); if(cozyUrl !== '/') { appUrl.value = cozyUrl || ''; backBtn?.removeAttribute('disabled'); submitBtn?.removeAttribute('disabled'); homeBtn?.removeAttribute('disabled'); document.title = cozyTitle; } else { appUrl.value = ''; backBtn?.setAttribute('disabled', 'true'); submitBtn?.setAttribute('disabled', 'true'); homeBtn?.setAttribute('disabled', 'true'); document.title = `Cozy`; } if(!preventPushState) { window.history.pushState({url}, '', url); } } } function getPostMeta(html: HTMLHtmlElement) { const title = getCozyTitle(html); 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"); return {title, description, image, source, published}; } function getCozyTitle(html: HTMLHtmlElement): string | undefined { return html.querySelector('meta[property="cozy:title"]')?.getAttribute("content") /** * backwards compatibility for stuff before we implemented cozy:meta tags * REMOVE ON V1 release */ ?? html.querySelector("title")?.innerHTML ?.replace("Cozy 🧸 | ", "") }