From 55ef1a63136c2ff0ec808ba44160f5503cc4416b Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 31 Aug 2024 08:25:09 +0100 Subject: add markdown rendering, lay out localStorage subscriptions --- src/routes/index.js | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) (limited to 'src/routes/index.js') diff --git a/src/routes/index.js b/src/routes/index.js index 6cf5403..77ff118 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -1,8 +1,8 @@ const express = require('express'); +const he = require('he'); const router = express.Router(); const geddit = require('../geddit.js'); const G = new geddit.Geddit(); -const fs = require('fs/promises'); // GET / @@ -18,6 +18,7 @@ router.get('/r/:subreddit', async (req, res) => { var aboutReq = G.getSubreddit(`${subreddit}`); var [posts, about] = await Promise.all([postsReq, aboutReq]); + res.render('index', { subreddit, posts, about }); }); @@ -26,10 +27,33 @@ router.get('/comments/:id', async (req, res) => { var id = req.params.id; response = await G.getSubmissionComments(id); - var post = response.submission.data; - var comments = response.comments; - res.render('comments', { post, comments }); + res.render('comments', unescape_submission(response)); }); module.exports = router; + +function unescape_submission(response) { + var post = response.submission.data; + var comments = response.comments; + + if (post.selftext_html) { + post.selftext_html = he.decode(post.selftext_html); + } + comments.forEach(unescape_comment); + + return { post, comments }; +} + +function unescape_comment(comment) { + if (comment.data.body_html) { + comment.data.body_html = he.decode(comment.data.body_html); + } + if (comment.data.replies) { + if(comment.data.replies.data) { + if(comment.data.replies.data.children) { + comment.data.replies.data.children.forEach(unescape_comment); + } + } + } +} -- cgit v1.2.3