From edabd7735c197d9cb1348a86d474e245d80be00b Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Thu, 16 Nov 2023 01:33:15 +0000 Subject: [PATCH] Forgot this file. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4635 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- apps/wiki/handler.js | 71 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 apps/wiki/handler.js diff --git a/apps/wiki/handler.js b/apps/wiki/handler.js new file mode 100644 index 000000000..373e805ee --- /dev/null +++ b/apps/wiki/handler.js @@ -0,0 +1,71 @@ +import * as utils from './utils.js'; +import * as commonmark from './commonmark.min.js'; + +function markdown(md) { + var reader = new commonmark.Parser({safe: true}); + var writer = new commonmark.HtmlRenderer(); + var parsed = reader.parse(md || ''); + let walker = parsed.walker(); + let event; + while ((event = walker.next())) { + let node = event.node; + if (event.entering) { + if (node.type === 'link') { + if (node.destination.indexOf(':') == -1 && + node.destination.indexOf('/') == -1) { + node.destination = `#${this.wiki?.name}/${node.destination}`; + } + } + } + } + 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); + + let ids = Object.keys(await ssb.following(await ssb.getOwnerIdentities(), 1)); + 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) { + let [max_row_id, wiki_docs] = await utils.collection(ids, 'wiki-doc', wiki.id, -1, {}); + 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: ` +

${wiki_name}: ${wiki_doc_name}

+
${markdown(md)}
+ `, + content_type: 'text/html', + status_code: 200, + }); + } else { + await respond({ + data: '

File not found

', + content_type: 'text/html', + status_code: 404, + }); + } +} +main(); \ No newline at end of file