forked from cory/tildefriends
		
	Make blogs semi-navigable.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4753 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
		| @@ -1,5 +1,5 @@ | |||||||
| { | { | ||||||
|   "type": "tildefriends-app", |   "type": "tildefriends-app", | ||||||
|   "emoji": "🪵", |   "emoji": "🪵", | ||||||
|   "previous": "&7Ch5DjIMAEbJm05ofH1U/+wlzn9kr65DChcKKgurE8E=.sha256" |   "previous": "&k+K+38dqVUjSDSspsyQhFkZexug/umccp20SSrsUq3o=.sha256" | ||||||
| } | } | ||||||
| @@ -8,6 +8,39 @@ function escapeAttribute(text) { | |||||||
| 	return (text ?? '').replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>').replaceAll('"', '"').replaceAll("'", '''); | 	return (text ?? '').replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>').replaceAll('"', '"').replaceAll("'", '''); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | export async function get_blog_message(id) { | ||||||
|  | 	let message; | ||||||
|  | 	await ssb.sqlAsync( | ||||||
|  | 		'SELECT author, timestamp, content FROM messages WHERE id = ?', | ||||||
|  | 		[id], | ||||||
|  | 		function(row) { | ||||||
|  | 			let content = JSON.parse(row.content); | ||||||
|  | 			message = { | ||||||
|  | 				author: row.author, | ||||||
|  | 				timestamp: row.timestamp, | ||||||
|  | 				blog: content?.blog, | ||||||
|  | 				title: content?.title, | ||||||
|  | 			}; | ||||||
|  | 		}); | ||||||
|  | 	if (message) { | ||||||
|  | 		await ssb.sqlAsync( | ||||||
|  | 			` | ||||||
|  | 				SELECT json_extract(content, '$.name') AS name | ||||||
|  | 				FROM messages | ||||||
|  | 				WHERE author = ? | ||||||
|  | 				AND json_extract(content, '$.type') = 'about' | ||||||
|  | 				AND json_extract(content, '$.about') = author | ||||||
|  | 				AND name IS NOT NULL | ||||||
|  | 				ORDER BY sequence DESC LIMIT 1 | ||||||
|  | 			`, | ||||||
|  | 			[message.author], | ||||||
|  | 			function(row) { | ||||||
|  | 				message.name = row.name; | ||||||
|  | 			}); | ||||||
|  | 	} | ||||||
|  | 	return message; | ||||||
|  | } | ||||||
|  |  | ||||||
| export 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(); | ||||||
| @@ -20,7 +53,7 @@ export function markdown(md) { | |||||||
| 			if (node.destination?.startsWith('&')) { | 			if (node.destination?.startsWith('&')) { | ||||||
| 				node.destination = '/' + node.destination + '/view?filename=' + node.firstChild?.literal; | 				node.destination = '/' + node.destination + '/view?filename=' + node.firstChild?.literal; | ||||||
| 			} else if (node.destination?.startsWith('@') || node.destination?.startsWith('%')) { | 			} else if (node.destination?.startsWith('@') || node.destination?.startsWith('%')) { | ||||||
| 				node.destination = '/~core/ssb/#' + node.destination; | 				node.destination = '/~core/ssb/#' + escape(node.destination); | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| @@ -32,9 +65,11 @@ export async function render_blog_post_html(blog_post) { | |||||||
| 	return `<!DOCTYPE html> | 	return `<!DOCTYPE html> | ||||||
| 		<html> | 		<html> | ||||||
| 			<head> | 			<head> | ||||||
|  | 				<title>🪵Tilde Friends Blog - ${markdown(blog_post.title)}</title> | ||||||
| 				<base target="_top"> | 				<base target="_top"> | ||||||
| 			</head> | 			</head> | ||||||
| 			<body> | 			<body> | ||||||
|  | 				<h1><a href="./">🪵Tilde Friends Blog</a></h1> | ||||||
| 				<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> | ||||||
| 					<div>${markdown(blob)}</div> | 					<div>${markdown(blob)}</div> | ||||||
|   | |||||||
| @@ -3,22 +3,9 @@ import * as blog from './blog.js'; | |||||||
| async function main() { | async function main() { | ||||||
| 	if (request.path.startsWith('%') && request.path.endsWith('.sha256')) { | 	if (request.path.startsWith('%') && request.path.endsWith('.sha256')) { | ||||||
| 		let id = request.path.startsWith('%25') ? '%' + request.path.substring(3) : request.path; | 		let id = request.path.startsWith('%25') ? '%' + request.path.substring(3) : request.path; | ||||||
| 		let blob = await ssb.messageContentGet(id); | 		let message = await blog.get_blog_message(id); | ||||||
| 		if (blob) { | 		if (message) { | ||||||
| 			let content = JSON.parse(utf8Decode(blob)); | 			respond({data: await blog.render_blog_post_html(message), content_type: 'text/html; charset=utf-8'}); | ||||||
| 			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 { | 		} else { | ||||||
| 			respond({data: `Message ${id} not found.`, content_type: 'text/html; charset=utf-8'}); | 			respond({data: `Message ${id} not found.`, content_type: 'text/html; charset=utf-8'}); | ||||||
| 		} | 		} | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user