aboutsummaryrefslogtreecommitdiff
path: root/src/mixins
diff options
context:
space:
mode:
Diffstat (limited to 'src/mixins')
-rw-r--r--src/mixins/comment.pug3
-rw-r--r--src/mixins/header.pug12
-rw-r--r--src/mixins/post.pug107
-rw-r--r--src/mixins/postUtils.pug43
4 files changed, 112 insertions, 53 deletions
diff --git a/src/mixins/comment.pug b/src/mixins/comment.pug
index 5b6f097..0609587 100644
--- a/src/mixins/comment.pug
+++ b/src/mixins/comment.pug
@@ -1,4 +1,5 @@
1include ../utils 1include ../utils
2include postUtils
2 3
3mixin infoContainer(data, next_id, prev_id) 4mixin infoContainer(data, next_id, prev_id)
4 - var hats = (data.is_submitter?['op']:[]).concat(data.distinguished=="moderator"?['mod']:[]) 5 - var hats = (data.is_submitter?['op']:[]).concat(data.distinguished=="moderator"?['mod']:[])
@@ -51,7 +52,7 @@ mixin comment(com, isfirst, parent_id, next_id, prev_id)
51 summary.expand-comments 52 summary.expand-comments
52 +infoContainer(data, next_id, prev_id) 53 +infoContainer(data, next_id, prev_id)
53 div.comment-body 54 div.comment-body
54 != data.body_html 55 != convertInlineImageLinks(data.body_html)
55 if hasReplyData 56 if hasReplyData
56 div.replies 57 div.replies
57 - var total = data.replies.data.children.length 58 - var total = data.replies.data.children.length
diff --git a/src/mixins/header.pug b/src/mixins/header.pug
index 9cf1e1a..7d56ff5 100644
--- a/src/mixins/header.pug
+++ b/src/mixins/header.pug
@@ -1,16 +1,18 @@
1mixin header(user) 1mixin header(user)
2 - var viewQuery = 'view=' + (query && query.view ? query.view : 'compact')
3 - var sortQuery = 'sort=' + (query ? (query.sort ? query.sort + (query.t ? '&t=' + query.t : '') : 'hot') : 'hot')
2 div.header 4 div.header
3 div.header-item 5 div.header-item
4 a(href=`/`) home 6 a(href=`/?${sortQuery}&${viewQuery}`) home
5 div.header-item 7 div.header-item
6 a(href=`/r/all`) all 8 a(href=`/r/all?${sortQuery}&${viewQuery}`) all
7 div.header-item 9 div.header-item
8 a(href=`/search`) search 10 a(href=`/search?${sortQuery}&${viewQuery}`) search
9 div.header-item 11 div.header-item
10 a(href=`/subs`) subs 12 a(href=`/subs?${sortQuery}&${viewQuery}`) subs
11 if user 13 if user
12 div.header-item 14 div.header-item
13 a(href='/dashboard') #{user.username} 15 a(href=`/dashboard?${sortQuery}&${viewQuery}`) #{user.username}
14 |  16 | 
15 a(href='/logout') (logout) 17 a(href='/logout') (logout)
16 else 18 else
diff --git a/src/mixins/post.pug b/src/mixins/post.pug
index 722a353..2e77402 100644
--- a/src/mixins/post.pug
+++ b/src/mixins/post.pug
@@ -2,11 +2,13 @@ include ../utils
2include postUtils 2include postUtils
3mixin post(p, currentUrl) 3mixin post(p, currentUrl)
4 - var from = encodeURIComponent(currentUrl) 4 - var from = encodeURIComponent(currentUrl)
5 article(class=`post ${p.stickied?"sticky":""}`) 5 - var viewQuery = query && query.view ? query.view : 'compact'
6 div.post-container 6 - var sortQuery = query && query.sort ? query.sort + (query.t ? '&t=' + query.t : '') : 'hot'
7 div.post-text 7 article(class=`post`)
8 div.title-container 8 div.post-container(class=`${query.view} ${p.stickied?"sticky":""}`)
9 a(href=`/comments/${p.id}?from=${from}`) 9 div.post-text(class=`${query.view}`)
10 div.title-container(class=`${query.view}`)
11 a(class=`${query.view}`, href=`/comments/${p.id}?from=${from}&sort=${sortQuery}&view=${viewQuery}`)
10 != p.title 12 != p.title
11 span.domain (#{p.domain}) 13 span.domain (#{p.domain})
12 div.info-container 14 div.info-container
@@ -22,50 +24,73 @@ mixin post(p, currentUrl)
22 |  ·  24 |  · 
23 | #{timeDifference(Date.now(), p.created * 1000)} 25 | #{timeDifference(Date.now(), p.created * 1000)}
24 |  ·  26 |  · 
25 a(href=`/r/${p.subreddit}`) r/#{p.subreddit} 27 a(href=`/r/${p.subreddit}?sort=${sortQuery}&view=${viewQuery}`) r/#{p.subreddit}
26 |  ·  28 |  · 
27 a(href=`/comments/${p.id}?from=${from}`) #{fmtnum (p.num_comments)} ↩ 29 a(href=`/comments/${p.id}?from=${from}&sort=${sortQuery}&view=${viewQuery}`) #{fmtnum (p.num_comments)} ↩
28 div.media-preview 30 if (query.view == "card" && !isPostGallery(p) && !isPostImage(p) && !isPostVideo(p) && p.selftext_html)
31 div.self-text-overflow(class='card')
32 if query.view == "card" && (p.spoiler || p.over_18)
33 div.spoiler(id=`spoiler_${p.id}`, onclick=`javascript:document.getElementById('spoiler_${p.id}').style.display = 'none';`)
34 h2
35 != p.over_18 ? 'nsfw' : 'spoiler'
36 div.self-text(class='card')
37 != convertInlineImageLinks(p.selftext_html)
38 div.media-preview(class=`${query.view}`)
39 if query.view == "card" && (p.spoiler || p.over_18) && (isPostGallery(p) || isPostImage(p) || isPostVideo(p))
40 div.spoiler(id=`spoiler_${p.id}`, onclick=`javascript:document.getElementById('spoiler_${p.id}').style.display = 'none';`)
41 h2
42 != p.over_18 ? 'nsfw' : 'spoiler'
29 if isPostGallery(p) 43 if isPostGallery(p)
30 - var item = postGalleryItems(p)[0] 44 - var item = postGalleryItems(p)[0]
31 img(src=item.url onclick=`toggleDetails('${p.id}')`) 45 img(src=item.url onclick=`toggleDetails('${p.id}')`)
32 else if isPostImage(p) 46 else if isPostImage(p)
33 - var url = postThumbnail(p) 47 - var url = query.view == "card" ? p.url : postThumbnail(p)
34 img(src=url onclick=`toggleDetails('${p.id}')`) 48 img(src=url onclick=`toggleDetails('${p.id}')`)
35 else if isPostVideo(p) 49 else if isPostVideo(p)
36 - var url = p.secure_media.reddit_video.scrubber_media_url 50 - var decodedVideos = decodePostVideoUrls(p)
37 video(src=url data-dashjs-player width='100px' height='100px' onclick=`toggleDetails('${p.id}')`) 51 if query.view == "card"
52 video(controls="" muted="" data-dashjs-player="" preload="metadata" poster=decodedVideos[4])
53 // HLS
54 source(src=decodedVideos[0])
55 // Dash
56 source(src=decodedVideos[1])
57 // Fallback
58 source(src=decodedVideos[2])
59 else
60 video(autoplay="" muted="" data-dashjs-player="" onclick=`toggleDetails('${p.id}')` width="100px" height="100px")
61 // Scrubber
62 source(src=decodedVideos[3])
38 else if isPostLink(p) 63 else if isPostLink(p)
39 a(href=p.url) 64 a(href=p.url)
65 if (query.view == 'card')
66 | #{p.domain}
40 | ↗ 67 | ↗
41 68
42 if isPostGallery(p) 69 if (isPostGallery(p) || isPostImage(p) || isPostVideo(p))
43 details(id=`${p.id}`) 70 details(id=`${p.id}`,class=`${query.view}`)
44 summary.expand-post expand gallery 71 summary.expand-post expand media
45 div.gallery 72 div.image-viewer(class=`${query.view}`)
46 each item in postGalleryItems(p) 73 if isPostGallery(p)
47 div.gallery-item 74 div.gallery(class=`${query.view}`)
48 div.gallery-item-idx 75 each item in postGalleryItems(p)
49 | #{`${item.idx}/${item.total}`} 76 div.gallery-item(class=`${query.view}`)
50 a(href=`/media/${item.url}`) 77 div.gallery-item-idx(class=`${query.view}`)
51 img(src=item.url loading="lazy") 78 | #{`${item.idx}/${item.total}`}
52 button(onclick=`toggleDetails('${p.id}')`) close 79 a(href=`/media/${item.url}`)
53 else if isPostImage(p) 80 img(src=item.url loading="lazy")
54 details(id=`${p.id}`) 81 else if isPostImage(p)
55 summary.expand-post expand image 82 a(href=`/media/${p.url}`)
56 a(href=`/media/${p.url}`) 83 img(src=p.url loading="lazy").post-media
57 img(src=p.url loading="lazy").post-media 84 else if isPostVideo(p)
58 button(onclick=`toggleDetails('${p.id}')`) close 85 video(controls="" muted="" data-dashjs-player="" preload="metadata" playsinline="" poster=decodedVideos[4] objectfit="contain" loading="lazy").post-media
59 else if isPostVideo(p) 86 //HLS
60 details(id=`${p.id}`) 87 source(src=decodedVideos[0])
61 summary.expand-post expand video 88 // Dash
62 - var url = p.secure_media.reddit_video.dash_url 89 source(src=decodedVideos[1])
63 video(src=url controls data-dashjs-player loading="lazy").post-media 90 // Fallback
64 button(onclick=`toggleDetails('${p.id}')`) close 91 source(src=decodedVideos[2])
65 else if isPostLink(p) 92 button(onclick=`toggleDetails('${p.id}')`,class=`${query.view}`)
66 details(id=`${p.id}`) 93 if (query.view == 'card')
67 summary.expand-post expand link 94 | ╳
68 a(href=`${p.url}`) 95 else
69 | #{p.url} 96 | close
70 br
71 button(onclick=`toggleDetails('${p.id}')`) close
diff --git a/src/mixins/postUtils.pug b/src/mixins/postUtils.pug
index 816adf7..b778cbd 100644
--- a/src/mixins/postUtils.pug
+++ b/src/mixins/postUtils.pug
@@ -10,14 +10,14 @@
10 } 10 }
11- 11-
12 function postThumbnail(p) { 12 function postThumbnail(p) {
13 if (p.thumbnail == "image" || p.thumbnail == "") { 13 if (p.over_18) {
14 return p.url; 14 return "/nsfw.svg";
15 } else if (p.over_18) {
16 return "/nsfw.svg";
17 } else if (p.thumbnail == "spoiler") { 15 } else if (p.thumbnail == "spoiler") {
18 return "/spoiler.svg"; 16 return "/spoiler.svg";
17 } else if (p.thumbnail == "image" || p.thumbnail == "") {
18 return p.url;
19 } else { 19 } else {
20 return p.thumbnail; 20 return p.thumbnail;
21 } 21 }
22 } 22 }
23- 23-
@@ -51,3 +51,34 @@
51 return null; 51 return null;
52 } 52 }
53 } 53 }
54-
55 function convertInlineImageLinks(html) {
56 // Find all anchors that href to preview.redd.it, i.redd.it, i.imgur.com
57 // and contain just a link to the same href
58 const expression = /<a href="(http[s]?:\/\/(?:preview\.redd\.it|i\.redd\.it|i\.imgur\.com).*?)">\1?<\/a>/g;
59 const matches = html.matchAll(expression);
60 var result = html;
61 matches.forEach((match) => {
62 // Replace each occurrence with an actual img tag
63 result = result.replace(match[0], '<a href="' + match[1] + '"><img src="' + match[1] + '"></a>');
64 })
65
66 return result;
67 }
68-
69 function decodePostVideoUrls(p) {
70 // Video URLs have querystring separators that are HTML-encoded, so replace them.
71 const expression = /&amp;/g;
72
73 var hls_url = p.secure_media && p.secure_media.reddit_video && p.secure_media.reddit_video.hls_url ? p.secure_media.reddit_video.hls_url.replace(expression, '&') : '';
74
75 var dash_url = p.secure_media && p.secure_media.reddit_video && p.secure_media.reddit_video.dash_url ? p.secure_media.reddit_video.dash_url.replace(expression, '&') : '';
76
77 var fallback_url = p.secure_media && p.secure_media.reddit_video && p.secure_media.reddit_video.fallback_url ? p.secure_media.reddit_video.fallback_url.replace(expression, '&') : '';
78
79 var scrubber_url = p.secure_media && p.secure_media.reddit_video && p.secure_media.reddit_video.scrubber_media_url ? p.secure_media.reddit_video.scrubber_media_url.replace(expression, '&') : '';
80
81 var poster_url = p.preview && p.preview.images ? p.preview.images[0].source.url.replace(expression, '&') : '';
82
83 return [hls_url, dash_url, fallback_url, scrubber_url, poster_url];
84 } \ No newline at end of file