diff options
author | Akshay <[email protected]> | 2024-08-31 08:25:09 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2024-08-31 08:28:11 +0100 |
commit | 55ef1a63136c2ff0ec808ba44160f5503cc4416b (patch) | |
tree | b14d287ca284963d7469c48957380778da7182b2 | |
parent | 17cb2b62fd8f31f49347ae5818acfc8c589fdbea (diff) |
add markdown rendering, lay out localStorage subscriptions
-rw-r--r-- | package.json | 1 | ||||
-rw-r--r-- | src/public/styles.css | 17 | ||||
-rw-r--r-- | src/routes/index.js | 32 | ||||
-rw-r--r-- | src/views/comment.pug | 3 | ||||
-rw-r--r-- | src/views/comments.pug | 2 | ||||
-rw-r--r-- | src/views/index.pug | 42 | ||||
-rw-r--r-- | src/views/post.pug | 9 |
7 files changed, 94 insertions, 12 deletions
diff --git a/package.json b/package.json index 3e68c4c..6c94715 100644 --- a/package.json +++ b/package.json | |||
@@ -11,6 +11,7 @@ | |||
11 | }, | 11 | }, |
12 | "dependencies": { | 12 | "dependencies": { |
13 | "express": "^4.19.2", | 13 | "express": "^4.19.2", |
14 | "he": "^1.2.0", | ||
14 | "pug": "^3.0.3", | 15 | "pug": "^3.0.3", |
15 | "timeago.js": "^4.0.2" | 16 | "timeago.js": "^4.0.2" |
16 | } | 17 | } |
diff --git a/src/public/styles.css b/src/public/styles.css index c366e46..8add1a5 100644 --- a/src/public/styles.css +++ b/src/public/styles.css | |||
@@ -24,7 +24,7 @@ main { | |||
24 | } | 24 | } |
25 | 25 | ||
26 | .info-item { | 26 | .info-item { |
27 | margin-right: 10px; | 27 | margin-right: 14px; |
28 | } | 28 | } |
29 | 29 | ||
30 | @media (min-width: 768px) { | 30 | @media (min-width: 768px) { |
@@ -58,6 +58,10 @@ main { | |||
58 | box-sizing: border-box; | 58 | box-sizing: border-box; |
59 | } | 59 | } |
60 | 60 | ||
61 | .comment { | ||
62 | padding: 0px 0px 0px 24px; | ||
63 | } | ||
64 | |||
61 | .more { | 65 | .more { |
62 | margin-bottom: 0px; | 66 | margin-bottom: 0px; |
63 | font-size: 0.7rem; | 67 | font-size: 0.7rem; |
@@ -68,7 +72,7 @@ main { | |||
68 | border-left: none; | 72 | border-left: none; |
69 | padding-left: 0; | 73 | padding-left: 0; |
70 | margin: 0; | 74 | margin: 0; |
71 | margin-top: 12px; | 75 | margin-top: 40px; |
72 | } | 76 | } |
73 | 77 | ||
74 | .post-container { | 78 | .post-container { |
@@ -88,6 +92,8 @@ main { | |||
88 | } | 92 | } |
89 | 93 | ||
90 | .post-media { | 94 | .post-media { |
95 | display: block; | ||
96 | margin: 0 auto; | ||
91 | max-width: 100%; | 97 | max-width: 100%; |
92 | } | 98 | } |
93 | 99 | ||
@@ -100,3 +106,10 @@ main { | |||
100 | hr { | 106 | hr { |
101 | border 1px solid #000; | 107 | border 1px solid #000; |
102 | } | 108 | } |
109 | |||
110 | blockquote { | ||
111 | margin: 0px; | ||
112 | padding-left: 10px; | ||
113 | border-left: 4px solid green; | ||
114 | color: green; | ||
115 | } | ||
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 @@ | |||
1 | const express = require('express'); | 1 | const express = require('express'); |
2 | const he = require('he'); | ||
2 | const router = express.Router(); | 3 | const router = express.Router(); |
3 | const geddit = require('../geddit.js'); | 4 | const geddit = require('../geddit.js'); |
4 | const G = new geddit.Geddit(); | 5 | const G = new geddit.Geddit(); |
5 | const fs = require('fs/promises'); | ||
6 | 6 | ||
7 | 7 | ||
8 | // GET / | 8 | // GET / |
@@ -18,6 +18,7 @@ router.get('/r/:subreddit', async (req, res) => { | |||
18 | var aboutReq = G.getSubreddit(`${subreddit}`); | 18 | var aboutReq = G.getSubreddit(`${subreddit}`); |
19 | 19 | ||
20 | var [posts, about] = await Promise.all([postsReq, aboutReq]); | 20 | var [posts, about] = await Promise.all([postsReq, aboutReq]); |
21 | |||
21 | res.render('index', { subreddit, posts, about }); | 22 | res.render('index', { subreddit, posts, about }); |
22 | }); | 23 | }); |
23 | 24 | ||
@@ -26,10 +27,33 @@ router.get('/comments/:id', async (req, res) => { | |||
26 | var id = req.params.id; | 27 | var id = req.params.id; |
27 | 28 | ||
28 | response = await G.getSubmissionComments(id); | 29 | response = await G.getSubmissionComments(id); |
29 | var post = response.submission.data; | ||
30 | var comments = response.comments; | ||
31 | 30 | ||
32 | res.render('comments', { post, comments }); | 31 | res.render('comments', unescape_submission(response)); |
33 | }); | 32 | }); |
34 | 33 | ||
35 | module.exports = router; | 34 | module.exports = router; |
35 | |||
36 | function unescape_submission(response) { | ||
37 | var post = response.submission.data; | ||
38 | var comments = response.comments; | ||
39 | |||
40 | if (post.selftext_html) { | ||
41 | post.selftext_html = he.decode(post.selftext_html); | ||
42 | } | ||
43 | comments.forEach(unescape_comment); | ||
44 | |||
45 | return { post, comments }; | ||
46 | } | ||
47 | |||
48 | function unescape_comment(comment) { | ||
49 | if (comment.data.body_html) { | ||
50 | comment.data.body_html = he.decode(comment.data.body_html); | ||
51 | } | ||
52 | if (comment.data.replies) { | ||
53 | if(comment.data.replies.data) { | ||
54 | if(comment.data.replies.data.children) { | ||
55 | comment.data.replies.data.children.forEach(unescape_comment); | ||
56 | } | ||
57 | } | ||
58 | } | ||
59 | } | ||
diff --git a/src/views/comment.pug b/src/views/comment.pug index 24e1a9b..8a60590 100644 --- a/src/views/comment.pug +++ b/src/views/comment.pug | |||
@@ -6,7 +6,8 @@ mixin comment(com, isfirst) | |||
6 | div.more #{data.count} more comments | 6 | div.more #{data.count} more comments |
7 | else | 7 | else |
8 | div(class=`comment ${isfirst?'first':''}`) | 8 | div(class=`comment ${isfirst?'first':''}`) |
9 | div.comment-body !{data.body} | 9 | div.comment-body |
10 | != data.body_html | ||
10 | div.info-container | 11 | div.info-container |
11 | div.info-item by u/#{data.author} | 12 | div.info-item by u/#{data.author} |
12 | div.info-item ↑ #{fmtnum(data.ups)} | 13 | div.info-item ↑ #{fmtnum(data.ups)} |
diff --git a/src/views/comments.pug b/src/views/comments.pug index f7964a3..ff90554 100644 --- a/src/views/comments.pug +++ b/src/views/comments.pug | |||
@@ -14,7 +14,7 @@ html | |||
14 | img(src=post.url).post-media | 14 | img(src=post.url).post-media |
15 | else if post.post_hint == 'hosted:video' | 15 | else if post.post_hint == 'hosted:video' |
16 | video(src=post.url).post-media | 16 | video(src=post.url).post-media |
17 | p.self-text !{post.selftext} | 17 | p.self-text !{post.selftext_html} |
18 | hr | 18 | hr |
19 | 19 | ||
20 | div.comments-container | 20 | div.comments-container |
diff --git a/src/views/index.pug b/src/views/index.pug index d017570..f23405f 100644 --- a/src/views/index.pug +++ b/src/views/index.pug | |||
@@ -1,16 +1,56 @@ | |||
1 | - var subs = [] | ||
1 | doctype html | 2 | doctype html |
2 | html | 3 | html |
3 | head | 4 | head |
4 | meta(charset='UTF-8') | 5 | meta(charset='UTF-8') |
5 | title reddit | 6 | title reddit |
6 | link(rel='stylesheet', href='/styles.css') | 7 | link(rel='stylesheet', href='/styles.css') |
8 | script. | ||
9 | function getSubs() { | ||
10 | var store = localStorage.getItem('subs'); | ||
11 | if (store) { | ||
12 | return store.split(',').map((n)=>n.replace(/\/?r\//,'')); | ||
13 | } else { | ||
14 | return []; | ||
15 | } | ||
16 | } | ||
17 | |||
18 | function subscribe(newsub) { | ||
19 | var subs = getSubs(); | ||
20 | if (!subs.includes(newsub)) { | ||
21 | localStorage.setItem('subs',[...subs,newsub]); | ||
22 | } | ||
23 | } | ||
24 | |||
25 | function newSubItem(newsub) { | ||
26 | var p = document.createElement("p"); | ||
27 | var text = document.createTextNode(newsub); | ||
28 | p.appendChild(text); | ||
29 | return p; | ||
30 | } | ||
31 | |||
32 | function genSubListing() { | ||
33 | const body = document.body; | ||
34 | const subbar = document.createElement('div'); | ||
35 | subbar.id = 'subscriptions'; | ||
36 | body.insertBefore(subbar, body.firstChild); | ||
37 | getSubs().forEach((item) => { | ||
38 | subbar.appendChild(newSubItem(item)); | ||
39 | }); | ||
40 | } | ||
41 | |||
42 | document.addEventListener('DOMContentLoaded', function() { | ||
43 | genSubListing(); | ||
44 | }); | ||
7 | body | 45 | body |
46 | |||
8 | main#content | 47 | main#content |
9 | div.header | 48 | div.header |
10 | a(href=`/r/#{subreddit}`) | 49 | a(href=`/r/${subreddit}`) |
11 | h1 r/#{subreddit} | 50 | h1 r/#{subreddit} |
12 | if about | 51 | if about |
13 | p #{about.public_description} | 52 | p #{about.public_description} |
53 | button(onclick=`subscribe("${subreddit}")`) subscribe | ||
14 | 54 | ||
15 | each child in posts.posts | 55 | each child in posts.posts |
16 | include post | 56 | include post |
diff --git a/src/views/post.pug b/src/views/post.pug index 183f33f..960ad53 100644 --- a/src/views/post.pug +++ b/src/views/post.pug | |||
@@ -7,8 +7,8 @@ mixin post(p) | |||
7 | != p.title | 7 | != p.title |
8 | span.domain (#{p.domain}) | 8 | span.domain (#{p.domain}) |
9 | div.info-container | 9 | div.info-container |
10 | div.info-item by u/#{p.author} | ||
11 | div.info-item ↑ #{fmtnum(p.ups)} | 10 | div.info-item ↑ #{fmtnum(p.ups)} |
11 | div.info-item by u/#{p.author} | ||
12 | div.info-item | 12 | div.info-item |
13 | a(href=`/r/${p.subreddit}`) r/#{p.subreddit} | 13 | a(href=`/r/${p.subreddit}`) r/#{p.subreddit} |
14 | div.info-item | 14 | div.info-item |
@@ -18,5 +18,8 @@ mixin post(p) | |||
18 | if p.thumbnail && p.thumbnail != "self" || p.thumbnail != "default" | 18 | if p.thumbnail && p.thumbnail != "self" || p.thumbnail != "default" |
19 | a(href=p.url) | 19 | a(href=p.url) |
20 | img(src=p.thumbnail width='100px') | 20 | img(src=p.thumbnail width='100px') |
21 | else if p.post_hint == "hosted:video" | 21 | else if p.post_hint == "hosted:video" |
22 | video(src=p.secure_media.reddit_video.scrubber_media_url width='100px') | 22 | - var url = p.secure_media.reddit_video.fallback_url |
23 | a(href=url) | ||
24 | video(src=url height='100px') | ||
25 | |||