Fiddling with blog links.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4749 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2024-01-10 02:23:40 +00:00
parent d38c58ce1d
commit b11d5192c2
3 changed files with 42 additions and 17 deletions

View File

@ -1,5 +1,5 @@
{ {
"type": "tildefriends-app", "type": "tildefriends-app",
"emoji": "🪵", "emoji": "🪵",
"previous": "&7E+OhgnyEUUIIHPhvWCF6esqRRDdqPHhqJiGp1h6xg4=.sha256" "previous": "&7Ch5DjIMAEbJm05ofH1U/+wlzn9kr65DChcKKgurE8E=.sha256"
} }

View File

@ -8,7 +8,7 @@ function escapeAttribute(text) {
return (text ?? '').replaceAll('&', '&amp;').replaceAll('<', '&lt;').replaceAll('>', '&gt;').replaceAll('"', '&quot;').replaceAll("'", '&#39;'); return (text ?? '').replaceAll('&', '&amp;').replaceAll('<', '&lt;').replaceAll('>', '&gt;').replaceAll('"', '&quot;').replaceAll("'", '&#39;');
} }
function markdown(md) { export function markdown(md) {
let reader = new commonmark.Parser({safe: true}); let reader = new commonmark.Parser({safe: true});
let writer = new commonmark.HtmlRenderer(); let writer = new commonmark.HtmlRenderer();
let parsed = reader.parse(md || ''); let parsed = reader.parse(md || '');
@ -17,10 +17,10 @@ function markdown(md) {
while ((event = walker.next())) { while ((event = walker.next())) {
node = event.node; node = event.node;
if (event.entering) { if (event.entering) {
if (node.type == 'image') { if (node.destination?.startsWith('&')) {
if (node.destination.startsWith('&')) { node.destination = '/' + node.destination + '/view?filename=' + node.firstChild?.literal;
node.destination = '/' + node.destination + '/view'; } else if (node.destination?.startsWith('@') || node.destination?.startsWith('%')) {
} node.destination = '/~core/ssb/#' + node.destination;
} }
} }
} }
@ -31,6 +31,9 @@ export async function render_blog_post_html(blog_post) {
let blob = utf8Decode(await ssb.blobGet(blog_post.blog)); let blob = utf8Decode(await ssb.blobGet(blog_post.blog));
return `<!DOCTYPE html> return `<!DOCTYPE html>
<html> <html>
<head>
<base target="_top">
</head>
<body> <body>
<div> <div>
<div><a href="../ssb/#${escapeAttribute(blog_post.author)}">${escape(blog_post.name)}</a> ${escape(new Date(blog_post.timestamp).toString())}</div> <div><a href="../ssb/#${escapeAttribute(blog_post.author)}">${escape(blog_post.name)}</a> ${escape(new Date(blog_post.timestamp).toString())}</div>
@ -44,7 +47,7 @@ export async function render_blog_post_html(blog_post) {
function render_blog_post(blog_post) { function render_blog_post(blog_post) {
return ` return `
<div> <div>
<h2><a href="../ssb/#${escapeAttribute(blog_post.id)}">${escape(blog_post.title)}</a></h2> <h2><a href="./${escapeAttribute(blog_post.id)}">${escape(blog_post.title)}</a></h2>
<div><a href="../ssb/#${escapeAttribute(blog_post.author)}">${escape(blog_post.name)}</a> ${escape(new Date(blog_post.timestamp).toString())}</div> <div><a href="../ssb/#${escapeAttribute(blog_post.author)}">${escape(blog_post.name)}</a> ${escape(new Date(blog_post.timestamp).toString())}</div>
<div>${markdown(blog_post.summary)}</div> <div>${markdown(blog_post.summary)}</div>
</div> </div>
@ -62,11 +65,11 @@ export function render_html(blogs) {
background-color: #ccc; background-color: #ccc;
} }
</style> </style>
<base target="_blank"> <base target="_top">
</head> </head>
<body> <body>
<div style="display: flex; flex-direction: row; align-items: center; gap: 1em"> <div style="display: flex; flex-direction: row; align-items: center; gap: 1em">
<h1>🪵Tilde Blog</h1> <h1>🪵Tilde Friends Blog</h1>
<div style="font-size: xx-small; vertical-align: middle"><a href="/~cory/blog/atom">atom feed</a></div> <div style="font-size: xx-small; vertical-align: middle"><a href="/~cory/blog/atom">atom feed</a></div>
</div> </div>
${blogs.map(blog_post => render_blog_post(blog_post)).join('\n')} ${blogs.map(blog_post => render_blog_post(blog_post)).join('\n')}

View File

@ -1,6 +1,31 @@
import * as blog from './blog.js'; import * as blog from './blog.js';
async function main() { async function main() {
if (request.path.startsWith('%') && request.path.endsWith('.sha256')) {
let id = request.path.startsWith('%25') ? '%' + request.path.substring(3) : request.path;
let blob = await ssb.messageContentGet(id);
if (blob) {
let content = JSON.parse(utf8Decode(blob));
let md = content?.text;
if (content?.type == 'blog') {
md = utf8Decode(await ssb.blobGet(content?.blog));
}
respond({data: `<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
${blog.markdown(md)}
</body>
</html>`, content_type: 'text/html; charset=utf-8'});
} else {
respond({data: `Message ${id} not found.`, content_type: 'text/html; charset=utf-8'});
}
} else if (request.path == 'atom') {
let blogs = await blog.get_posts();
respond({data: blog.render_atom(blogs), content_type: 'application/atom+xml'});
} else {
let blogs = await blog.get_posts(); let blogs = await blog.get_posts();
for (let blog_post of blogs) { for (let blog_post of blogs) {
let title = (blog_post.title || '').replaceAll(/\W/g, '_').toLowerCase(); let title = (blog_post.title || '').replaceAll(/\W/g, '_').toLowerCase();
@ -9,9 +34,6 @@ async function main() {
return; return;
} }
} }
if (request.path == 'atom') {
respond({data: blog.render_atom(blogs), content_type: 'application/atom+xml'});
} else {
respond({data: blog.render_html(blogs), content_type: 'text/html; charset=utf-8'}); respond({data: blog.render_html(blogs), content_type: 'text/html; charset=utf-8'});
} }
} }