aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay <[email protected]>2024-09-01 19:18:23 +0100
committerAkshay <[email protected]>2024-09-01 19:18:23 +0100
commit8b5db5e1954de865933fd4de003fdd21d57add4a (patch)
tree2c03ba09a51f8d472c1e815942318d5447fb16b2
parent55ef1a63136c2ff0ec808ba44160f5503cc4416b (diff)
finish subscriptions
-rw-r--r--flake.nix2
-rw-r--r--src/mixins/comment.pug (renamed from src/views/comment.pug)11
-rw-r--r--src/mixins/post.pug (renamed from src/views/post.pug)2
-rw-r--r--src/mixins/sub.pug32
-rw-r--r--src/public/styles.css67
-rw-r--r--src/routes/index.js5
-rw-r--r--src/utils.pug (renamed from src/views/utils.pug)0
-rw-r--r--src/views/comments.pug6
-rw-r--r--src/views/index.pug59
-rw-r--r--src/views/subs.pug32
10 files changed, 162 insertions, 54 deletions
diff --git a/flake.nix b/flake.nix
index d35173d..0f3be4f 100644
--- a/flake.nix
+++ b/flake.nix
@@ -36,7 +36,7 @@
36 cp -R ./node_modules/* $out/node_modules 36 cp -R ./node_modules/* $out/node_modules
37 ls -la $out/node_modules 37 ls -la $out/node_modules
38 ''; 38 '';
39 outputHash = "sha256-rDMFY/D7rRWj6PDhZu2vRST12fyNpYUMl1a1LBB6/Jw="; 39 outputHash = "sha256-k77Ht47QBQUmoGp2zxBwVIjQ9fwnIGCqcqBLK6/d6jM=";
40 outputHashAlgo = "sha256"; 40 outputHashAlgo = "sha256";
41 outputHashMode = "recursive"; 41 outputHashMode = "recursive";
42 }; 42 };
diff --git a/src/views/comment.pug b/src/mixins/comment.pug
index 8a60590..ec01dad 100644
--- a/src/views/comment.pug
+++ b/src/mixins/comment.pug
@@ -1,16 +1,17 @@
1include utils 1include ../utils
2mixin comment(com, isfirst) 2mixin comment(com, isfirst)
3 - var data = com.data 3 - var data = com.data
4 - var kind = com.kind 4 - var kind = com.kind
5 if kind == "more" 5 if kind == "more"
6 div.more #{data.count} more comments 6 div(class=`${isfirst?'first':''}`)
7 div.more #{data.count} more comments
7 else 8 else
8 div(class=`comment ${isfirst?'first':''}`) 9 div(class=`comment ${isfirst?'first':''}`)
10 div.comment-info-container
11 div.info-item u/#{data.author}
12 div.info-item ↑ #{fmtnum(data.ups)}
9 div.comment-body 13 div.comment-body
10 != data.body_html 14 != data.body_html
11 div.info-container
12 div.info-item by u/#{data.author}
13 div.info-item ↑ #{fmtnum(data.ups)}
14 div.replies 15 div.replies
15 if data.replies 16 if data.replies
16 if data.replies.data 17 if data.replies.data
diff --git a/src/views/post.pug b/src/mixins/post.pug
index 960ad53..f5819d4 100644
--- a/src/views/post.pug
+++ b/src/mixins/post.pug
@@ -1,4 +1,4 @@
1include utils 1include ../utils
2mixin post(p) 2mixin post(p)
3 article.post 3 article.post
4 div.post-container 4 div.post-container
diff --git a/src/mixins/sub.pug b/src/mixins/sub.pug
new file mode 100644
index 0000000..a40aa68
--- /dev/null
+++ b/src/mixins/sub.pug
@@ -0,0 +1,32 @@
1mixin subMgmt()
2 script.
3 function getSubs() {
4 var store = localStorage.getItem('subs');
5 if (store) {
6 return store.split(',').map((n)=>n.replace(/\/?r\//,''));
7 } else {
8 return [];
9 }
10 }
11
12 function subscribe(newsub) {
13 var subs = getSubs();
14 if (!subs.includes(newsub)) {
15 localStorage.setItem('subs',[...subs,newsub]);
16 updateButton(newsub);
17 }
18 }
19
20 function unsubscribe(sub) {
21 var subs = getSubs();
22 if (subs.includes(sub)) {
23 localStorage.setItem('subs',subs.filter((s)=>s!=sub));
24 updateButton(sub);
25 }
26 }
27
28 function issub(sub) {
29 return getSubs().includes(sub);
30 }
31
32
diff --git a/src/public/styles.css b/src/public/styles.css
index 8add1a5..60cd2c3 100644
--- a/src/public/styles.css
+++ b/src/public/styles.css
@@ -1,3 +1,9 @@
1@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
2
3body {
4 font-family: 'Inter', sans-serif;
5}
6
1main { 7main {
2 display: flex; 8 display: flex;
3 flex-direction: column; 9 flex-direction: column;
@@ -5,14 +11,24 @@ main {
5 align-items: center; 11 align-items: center;
6} 12}
7 13
8.post, .comments-container, .header { 14.header {
15 display: flex;
16 flex-direction: row;
17}
18
19nav {
20 display: flex;
21 align-items: stretch;
22}
23
24.post, .comments-container, .hero, .header {
9 padding: 0.3rem; 25 padding: 0.3rem;
10 flex: 1 1 100%; 26 flex: 1 1 100%;
11 font-size: 1rem; 27 font-size: 1rem;
12 width: 100%; 28 width: 100%;
13} 29}
14 30
15.info-container { 31.info-container, .comment-info-container {
16 color: #777; 32 color: #777;
17 font-size: 0.7rem; 33 font-size: 0.7rem;
18 display: flex; 34 display: flex;
@@ -23,26 +39,40 @@ main {
23 font-size: 0.7rem; 39 font-size: 0.7rem;
24} 40}
25 41
26.info-item { 42.info-item, .header-item {
27 margin-right: 14px; 43 margin-right: 14px;
28} 44}
29 45
46.header-item {
47 position: relative; /* Needed for positioning the pseudo-element */
48}
49
50.header-item:not(:last-child)::after {
51 content: "·"; /* Middle dot as the separator */
52 position: absolute;
53 right: -10px; /* Adjust position as needed */
54 top: 50%;
55 transform: translateY(-50%); /* Center vertically */
56 color: #888; /* Separator color */
57 font-size: 20px; /* Adjust size of the separator */
58}
59
30@media (min-width: 768px) { 60@media (min-width: 768px) {
31 .post, .comments-container, .header { 61 .post, .comments-container, .hero, .header {
32 flex: 1 1 65%; 62 flex: 1 1 65%;
33 width: 65%; 63 width: 65%;
34 } 64 }
35 .info-container { 65 .info-container, .comment-info-container {
36 font-size: 0.8rem; 66 font-size: 0.8rem;
37 } 67 }
38} 68}
39 69
40@media (min-width: 1080px) { 70@media (min-width: 1080px) {
41 .post, .comments-container, .header { 71 .post, .comments-container, .hero, .header {
42 flex: 1 1 50%; 72 flex: 1 1 50%;
43 width: 50%; 73 width: 50%;
44 } 74 }
45 .info-container { 75 .info-container, .comment-info-container {
46 font-size: 0.8rem; 76 font-size: 0.8rem;
47 } 77 }
48} 78}
@@ -95,14 +125,19 @@ main {
95 display: block; 125 display: block;
96 margin: 0 auto; 126 margin: 0 auto;
97 max-width: 100%; 127 max-width: 100%;
128 max-height: 400px;
98} 129}
99 130
100.title-container,.info-container { 131.title-container,.info-container, .comment-info-container {
101 flex: 1; 132 flex: 1;
102 margin-top: 10px; 133 margin-top: 10px;
103 margin-bottom: 10px; 134 margin-bottom: 10px;
104} 135}
105 136
137.comment-info-container {
138 margin-bottom: -12px;
139}
140
106hr { 141hr {
107 border 1px solid #000; 142 border 1px solid #000;
108} 143}
@@ -113,3 +148,17 @@ blockquote {
113 border-left: 4px solid green; 148 border-left: 4px solid green;
114 color: green; 149 color: green;
115} 150}
151
152pre, code {
153 background: #eee;
154}
155
156pre {
157 padding: 10px;
158 width: 100%;
159 overflow: auto;
160}
161
162code {
163 overflow-x: auto;
164}
diff --git a/src/routes/index.js b/src/routes/index.js
index 77ff118..f43ee6f 100644
--- a/src/routes/index.js
+++ b/src/routes/index.js
@@ -31,6 +31,11 @@ router.get('/comments/:id', async (req, res) => {
31 res.render('comments', unescape_submission(response)); 31 res.render('comments', unescape_submission(response));
32}); 32});
33 33
34// GET /subs
35router.get('/subs', async (req, res) => {
36 res.render('subs');
37});
38
34module.exports = router; 39module.exports = router;
35 40
36function unescape_submission(response) { 41function unescape_submission(response) {
diff --git a/src/views/utils.pug b/src/utils.pug
index f3f61bb..f3f61bb 100644
--- a/src/views/utils.pug
+++ b/src/utils.pug
diff --git a/src/views/comments.pug b/src/views/comments.pug
index ff90554..0539b27 100644
--- a/src/views/comments.pug
+++ b/src/views/comments.pug
@@ -1,3 +1,5 @@
1include ../mixins/comment
2
1doctype html 3doctype html
2html 4html
3 head 5 head
@@ -6,7 +8,7 @@ html
6 link(rel='stylesheet', href='/styles.css') 8 link(rel='stylesheet', href='/styles.css')
7 body 9 body
8 main#content 10 main#content
9 div.header 11 div.hero
10 a(href=`/r/${post.subreddit}`) 12 a(href=`/r/${post.subreddit}`)
11 h4 ← r/#{post.subreddit} 13 h4 ← r/#{post.subreddit}
12 h2 #{post.title} 14 h2 #{post.title}
@@ -15,11 +17,9 @@ html
15 else if post.post_hint == 'hosted:video' 17 else if post.post_hint == 'hosted:video'
16 video(src=post.url).post-media 18 video(src=post.url).post-media
17 p.self-text !{post.selftext_html} 19 p.self-text !{post.selftext_html}
18 hr
19 20
20 div.comments-container 21 div.comments-container
21 each child in comments 22 each child in comments
22 include comment
23 +comment(child, true) 23 +comment(child, true)
24 24
25 script(src='https://unpkg.com/[email protected]') 25 script(src='https://unpkg.com/[email protected]')
diff --git a/src/views/index.pug b/src/views/index.pug
index f23405f..526d800 100644
--- a/src/views/index.pug
+++ b/src/views/index.pug
@@ -1,3 +1,5 @@
1include ../mixins/post
2include ../mixins/sub
1- var subs = [] 3- var subs = []
2doctype html 4doctype html
3html 5html
@@ -5,55 +7,42 @@ html
5 meta(charset='UTF-8') 7 meta(charset='UTF-8')
6 title reddit 8 title reddit
7 link(rel='stylesheet', href='/styles.css') 9 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 10
18 function subscribe(newsub) { 11 +subMgmt()
19 var subs = getSubs(); 12 script.
20 if (!subs.includes(newsub)) { 13 function updateButton(sub) {
21 localStorage.setItem('subs',[...subs,newsub]); 14 var b = document.getElementById("button-container");
22 } 15 b.innerHTML = '';
23 }
24 16
25 function newSubItem(newsub) { 17 const button = document.createElement("button");
26 var p = document.createElement("p");
27 var text = document.createTextNode(newsub);
28 p.appendChild(text);
29 return p;
30 }
31 18
32 function genSubListing() { 19 if (issub(sub)) {
33 const body = document.body; 20 button.innerText = "unsubscribe";
34 const subbar = document.createElement('div'); 21 button.onclick = ()=>unsubscribe(sub);
35 subbar.id = 'subscriptions'; 22 } else {
36 body.insertBefore(subbar, body.firstChild); 23 button.innerText = "subscribe";
37 getSubs().forEach((item) => { 24 button.onclick = ()=>subscribe(sub);
38 subbar.appendChild(newSubItem(item)); 25 }
39 }); 26 b.appendChild(button);
40 } 27 }
41 28
42 document.addEventListener('DOMContentLoaded', function() { 29 document.addEventListener('DOMContentLoaded', ()=>updateButton("#{subreddit}"));
43 genSubListing();
44 });
45 body 30 body
46
47 main#content 31 main#content
48 div.header 32 div.header
33 div.header-item
34 a(href=`/`) home
35 div.header-item
36 a(href=`/subs`) subscriptions
37
38 div.hero
49 a(href=`/r/${subreddit}`) 39 a(href=`/r/${subreddit}`)
50 h1 r/#{subreddit} 40 h1 r/#{subreddit}
51 if about 41 if about
52 p #{about.public_description} 42 p #{about.public_description}
53 button(onclick=`subscribe("${subreddit}")`) subscribe 43 div#button-container
54 44
55 each child in posts.posts 45 each child in posts.posts
56 include post
57 +post(child.data) 46 +post(child.data)
58 47
59 script(src='https://unpkg.com/[email protected]') 48 script(src='https://unpkg.com/[email protected]')
diff --git a/src/views/subs.pug b/src/views/subs.pug
new file mode 100644
index 0000000..038cd15
--- /dev/null
+++ b/src/views/subs.pug
@@ -0,0 +1,32 @@
1include ../mixins/sub
2
3doctype html
4html
5 head
6 meta(charset='UTF-8')
7 title reddit
8 link(rel='stylesheet', href='/styles.css')
9
10 +subMgmt()
11 script.
12 function newSubItem(sub) {
13 const p = document.createElement("p");
14 const a = document.createElement("a");
15 a.href = `/r/${sub}`;
16 a.innerText = `r/${sub}`;
17 p.appendChild(a);
18 return p;
19 }
20
21 function buildSubList() {
22 var subList = document.getElementById('subList');
23 getSubs().forEach((sub)=>{
24 subList.appendChild(newSubItem(sub));
25 });
26 }
27
28 document.addEventListener('DOMContentLoaded', buildSubList);
29 body
30 main#content
31 h1 subscriptions
32 div#subList