| 
									
										
										
										
											2023-11-16 01:33:15 +00:00
										 |  |  | import * as utils from './utils.js'; | 
					
						
							|  |  |  | import * as commonmark from './commonmark.min.js'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function markdown(md) { | 
					
						
							| 
									
										
										
										
											2024-11-12 20:43:03 -05:00
										 |  |  | 	let reader = new commonmark.Parser(); | 
					
						
							|  |  |  | 	let writer = new commonmark.HtmlRenderer({safe: true}); | 
					
						
							| 
									
										
										
										
											2023-11-16 01:35:36 +00:00
										 |  |  | 	let parsed = reader.parse(md || ''); | 
					
						
							| 
									
										
										
										
											2023-11-16 01:33:15 +00:00
										 |  |  | 	let walker = parsed.walker(); | 
					
						
							|  |  |  | 	let event; | 
					
						
							|  |  |  | 	while ((event = walker.next())) { | 
					
						
							|  |  |  | 		let node = event.node; | 
					
						
							|  |  |  | 		if (event.entering) { | 
					
						
							| 
									
										
										
										
											2024-01-28 23:54:28 +00:00
										 |  |  | 			if (node.destination?.startsWith('&')) { | 
					
						
							| 
									
										
										
										
											2024-02-24 11:09:34 -05:00
										 |  |  | 				node.destination = | 
					
						
							|  |  |  | 					'/' + node.destination + '/view?filename=' + node.firstChild?.literal; | 
					
						
							| 
									
										
										
										
											2024-01-28 23:54:28 +00:00
										 |  |  | 			} else if (node.type === 'link') { | 
					
						
							| 
									
										
										
										
											2024-02-24 11:09:34 -05:00
										 |  |  | 				if ( | 
					
						
							|  |  |  | 					node.destination.indexOf(':') == -1 && | 
					
						
							|  |  |  | 					node.destination.indexOf('/') == -1 | 
					
						
							|  |  |  | 				) { | 
					
						
							| 
									
										
										
										
											2024-01-08 17:42:56 +00:00
										 |  |  | 					node.destination = `${node.destination}`; | 
					
						
							| 
									
										
										
										
											2023-11-16 01:33:15 +00:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return writer.render(parsed); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function main() { | 
					
						
							|  |  |  | 	let slash = request.path.indexOf('/'); | 
					
						
							|  |  |  | 	if (slash != -1) { | 
					
						
							|  |  |  | 		let wiki_name = request.path.substring(0, slash); | 
					
						
							|  |  |  | 		let wiki_doc_name = request.path.substring(slash + 1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 11:09:34 -05:00
										 |  |  | 		let ids = Object.keys( | 
					
						
							|  |  |  | 			await ssb.following(await ssb.getOwnerIdentities(), 1) | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2023-11-16 01:33:15 +00:00
										 |  |  | 		let [max_row_id, wikis] = await utils.collection(ids, 'wiki', null, -1, {}); | 
					
						
							|  |  |  | 		let wiki; | 
					
						
							|  |  |  | 		for (let w of Object.values(wikis)) { | 
					
						
							|  |  |  | 			if (w.name === wiki_name && !w.tombstone) { | 
					
						
							|  |  |  | 				wiki = w; | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		let wiki_doc; | 
					
						
							|  |  |  | 		if (wiki) { | 
					
						
							| 
									
										
										
										
											2024-02-24 11:09:34 -05:00
										 |  |  | 			let [max_row_id, wiki_docs] = await utils.collection( | 
					
						
							|  |  |  | 				ids, | 
					
						
							|  |  |  | 				'wiki-doc', | 
					
						
							|  |  |  | 				wiki.id, | 
					
						
							|  |  |  | 				-1, | 
					
						
							|  |  |  | 				{} | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2023-11-16 01:33:15 +00:00
										 |  |  | 			for (let w of Object.values(wiki_docs)) { | 
					
						
							|  |  |  | 				if (w.name === wiki_doc_name && !w.tombstone) { | 
					
						
							|  |  |  | 					wiki_doc = w; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		let md; | 
					
						
							|  |  |  | 		if (wiki_doc?.blob) { | 
					
						
							|  |  |  | 			md = utf8Decode(await ssb.blobGet(wiki_doc.blob)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		await respond({ | 
					
						
							|  |  |  | 			data: `
 | 
					
						
							|  |  |  | 				<h1>${wiki_name}: ${wiki_doc_name}</h1> | 
					
						
							| 
									
										
										
										
											2023-11-16 01:35:36 +00:00
										 |  |  | 				<div>${markdown(md)}</div> | 
					
						
							| 
									
										
										
										
											2023-11-16 01:33:15 +00:00
										 |  |  | 			`,
 | 
					
						
							| 
									
										
										
										
											2024-01-28 23:54:28 +00:00
										 |  |  | 			content_type: 'text/html; charset=utf-8', | 
					
						
							| 
									
										
										
										
											2023-11-16 01:33:15 +00:00
										 |  |  | 			status_code: 200, | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		await respond({ | 
					
						
							|  |  |  | 			data: '<h1>File not found</h1>', | 
					
						
							|  |  |  | 			content_type: 'text/html', | 
					
						
							|  |  |  | 			status_code: 404, | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-02-24 11:09:34 -05:00
										 |  |  | main(); |