import * as commonmark from './commonmark.min.js'; function escape(text) { return (text ?? '').replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>'); } function escapeAttribute(text) { return (text ?? '').replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>').replaceAll('"', '"').replaceAll("'", '''); } function markdown(md) { let reader = new commonmark.Parser({safe: true}); let writer = new commonmark.HtmlRenderer(); let parsed = reader.parse(md || ''); let walker = parsed.walker(); let event, node; while ((event = walker.next())) { node = event.node; if (event.entering) { if (node.type == 'image') { if (node.destination.startsWith('&')) { node.destination = '/' + node.destination + '/view'; } } } } return writer.render(parsed); } export async function render_blog_post_html(blog_post) { let blob = utf8Decode(await ssb.blobGet(blog_post.blog)); return `
${escape(blog_post.name)} ${escape(new Date(blog_post.timestamp).toString())}
${markdown(blob)}
`; } function render_blog_post(blog_post) { return `

${escape(blog_post.title)}

${escape(blog_post.name)} ${escape(new Date(blog_post.timestamp).toString())}
${markdown(blog_post.summary)}
`; } export function render_html(blogs) { return ` 🪵Tilde Blog

🪵Tilde Blog

atom feed
${blogs.map(blog_post => render_blog_post(blog_post)).join('\n')} `; } function render_blog_post_atom(blog_post) { return ` ${escape(blog_post.title)} ${blog_post.id} ${escape(new Date(blog_post.timestamp).toString())} ${escape(blog_post.summary)} ${escape(blog_post.name)} ${escape(blog_post.author)} `; } export function render_atom(blogs) { return ` 🪵Tilde Blog A subtitle. https://www.tildefriends.net/~cory/blog/ ${new Date().toString()} ${blogs.map(blog_post => render_blog_post_atom(blog_post)).join('\n')} `; } export async function get_posts() { let blogs = []; await ssb.sqlAsync(` WITH blogs AS ( SELECT messages.author, messages.id, json_extract(messages.content, '$.title') AS title, json_extract(messages.content, '$.summary') AS summary, json_extract(messages.content, '$.blog') AS blog, messages.timestamp FROM messages_fts('blog') JOIN messages ON messages.rowid = messages_fts.rowid WHERE json_extract(messages.content, '$.type') = 'blog'), public AS ( SELECT author FROM ( SELECT messages.author, RANK() OVER (PARTITION BY messages.author ORDER BY messages.sequence DESC) AS author_rank, json_extract(messages.content, '$.publicWebHosting') AS is_public FROM messages_fts('about') JOIN messages ON messages.rowid = messages_fts.rowid WHERE json_extract(messages.content, '$.type') = 'about' AND is_public IS NOT NULL) WHERE author_rank = 1 AND is_public), names AS ( SELECT author, name FROM ( SELECT messages.author, RANK() OVER (PARTITION BY messages.author ORDER BY messages.sequence DESC) AS author_rank, json_extract(messages.content, '$.name') AS name FROM messages_fts('about') JOIN messages ON messages.rowid = messages_fts.rowid WHERE json_extract(messages.content, '$.type') = 'about' AND json_extract(messages.content, '$.about') = messages.author AND name IS NOT NULL) WHERE author_rank = 1) SELECT blogs.*, names.name FROM blogs JOIN public ON public.author = blogs.author LEFT OUTER JOIN names ON names.author = blogs.author ORDER BY blogs.timestamp DESC LIMIT 20 `, [], function(row) { blogs.push(row); }); return blogs; }