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.pug117
-rw-r--r--src/mixins/postUtils.pug43
4 files changed, 121 insertions, 54 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..c4f3bc5 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,81 @@ 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 - var onclick = query.view != "card" ? `toggleDetails('${p.id}')` : ``
40 if query.view == "card" && (p.spoiler || p.over_18) && (isPostGallery(p) || isPostImage(p) || isPostVideo(p))
41 div.spoiler(id=`spoiler_${p.id}`, onclick=`javascript:document.getElementById('spoiler_${p.id}').style.display = 'none';`)
42 h2
43 != p.over_18 ? 'nsfw' : 'spoiler'
29 if isPostGallery(p) 44 if isPostGallery(p)
30 - var item = postGalleryItems(p)[0] 45 - var item = postGalleryItems(p)[0]
31 img(src=item.url onclick=`toggleDetails('${p.id}')`) 46 if query.view == "card"
47 div.gallery(class=`${query.view}`)
48 each item in postGalleryItems(p)
49 div.gallery-item(class=`${query.view}`)
50 a(href=`/media/${item.url}`)
51 img(src=item.url loading="lazy")
52 div.gallery-item-idx(class=`${query.view}`)
53 | #{`${item.idx}/${item.total}`}
54 else
55 img(src=item.url onclick=onclick)
32 else if isPostImage(p) 56 else if isPostImage(p)
33 - var url = postThumbnail(p) 57 - var url = query.view == "card" ? p.url : postThumbnail(p)
34 img(src=url onclick=`toggleDetails('${p.id}')`) 58 #{query.view == "card" ? "a href=/media/" + url : span}
59 img(src=url onclick=onclick)
35 else if isPostVideo(p) 60 else if isPostVideo(p)
36 - var url = p.secure_media.reddit_video.scrubber_media_url 61 - var decodedVideos = decodePostVideoUrls(p)
37 video(src=url data-dashjs-player width='100px' height='100px' onclick=`toggleDetails('${p.id}')`) 62 if query.view == "card"
63 video(controls="" muted="" data-dashjs-player="" preload="metadata" poster=decodedVideos[4])
64 // HLS
65 source(src=decodedVideos[0])
66 // Dash
67 source(src=decodedVideos[1])
68 // Fallback
69 source(src=decodedVideos[2])
70 else
71 video(autoplay="" muted="" data-dashjs-player="" onclick=`toggleDetails('${p.id}')` width="100px" height="100px")
72 // Scrubber
73 source(src=decodedVideos[3])
38 else if isPostLink(p) 74 else if isPostLink(p)
39 a(href=p.url) 75 a(href=p.url)
76 if (query.view == 'card')
77 | #{p.domain}
40 | ↗ 78 | ↗
41 79
42 if isPostGallery(p) 80 if query.view == "compact" && (isPostGallery(p) || isPostImage(p) || isPostVideo(p))
43 details(id=`${p.id}`) 81 details(id=`${p.id}`)
44 summary.expand-post expand gallery 82 summary.expand-post expand media
45 div.gallery 83 div.image-viewer
46 each item in postGalleryItems(p) 84 if isPostGallery(p)
47 div.gallery-item 85 div.gallery
48 div.gallery-item-idx 86 each item in postGalleryItems(p)
49 | #{`${item.idx}/${item.total}`} 87 div.gallery-item
50 a(href=`/media/${item.url}`) 88 div.gallery-item-idx
51 img(src=item.url loading="lazy") 89 | #{`${item.idx}/${item.total}`}
52 button(onclick=`toggleDetails('${p.id}')`) close 90 a(href=`/media/${item.url}`)
53 else if isPostImage(p) 91 img(src=item.url loading="lazy")
54 details(id=`${p.id}`) 92 else if isPostImage(p)
55 summary.expand-post expand image 93 a(href=`/media/${p.url}`)
56 a(href=`/media/${p.url}`) 94 img(src=p.url loading="lazy").post-media
57 img(src=p.url loading="lazy").post-media 95 else if isPostVideo(p)
58 button(onclick=`toggleDetails('${p.id}')`) close 96 video(controls="" muted="" data-dashjs-player="" preload="metadata" playsinline="" poster=decodedVideos[4] objectfit="contain" loading="lazy").post-media
59 else if isPostVideo(p) 97 //HLS
60 details(id=`${p.id}`) 98 source(src=decodedVideos[0])
61 summary.expand-post expand video 99 // Dash
62 - var url = p.secure_media.reddit_video.dash_url 100 source(src=decodedVideos[1])
63 video(src=url controls data-dashjs-player loading="lazy").post-media 101 // Fallback
64 button(onclick=`toggleDetails('${p.id}')`) close 102 source(src=decodedVideos[2])
65 else if isPostLink(p) 103 button(onclick=`toggleDetails('${p.id}')`)
66 details(id=`${p.id}`) 104 | close
67 summary.expand-post expand link
68 a(href=`${p.url}`)
69 | #{p.url}
70 br
71 button(onclick=`toggleDetails('${p.id}')`) close
diff --git a/src/mixins/postUtils.pug b/src/mixins/postUtils.pug
index 816adf7..b96ff1e 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 class="inline" 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