aboutsummaryrefslogtreecommitdiff
path: root/src/mixins/postUtils.pug
blob: b96ff1e978999c6506ed8bc527a682ed6d7d95c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
-
  function isPostGallery(p) {
    return (p.is_gallery && p.is_gallery == true);
  }
-
  function isPostImage(p) {
    const imgRe = /\.(png|jpg|jpeg|gif|webp|bmp|tiff|svg)$/i;
    return (p.post_hint == "image" && p.thumbnail && p.thumbnail != "self" && p.thumbnail != "default")
      || imgRe.test(p.url);
  }
-
  function postThumbnail(p) {
    if (p.over_18) {
      return "/nsfw.svg";
    } else if (p.thumbnail == "spoiler") {
      return "/spoiler.svg";
    } else if (p.thumbnail == "image" || p.thumbnail == "") {
      return p.url;
    } else {
      return p.thumbnail;
    }
  }
-
  function isPostVideo(p) {
    return (p.post_hint == "hosted:video");
  }
-
  function isPostLink(p) {
    return (p.post_hint == "link");
  }
-
  function imgExt(p, id) {
    var metadata = p.media_metadata;
    if (metadata[id].status == 'valid') {
      return stripPrefix(metadata[id].m, "image/");
    } else {
      // dosent matter
      return 'jpg';
    }
  }
-
  function postGalleryItems(p) {
    if (p.gallery_data && p.gallery_data.items) {
      return p.gallery_data.items.map((item, idx) => ({
        id: item.media_id,
        url: `https://i.redd.it/${item.media_id}.${imgExt(p, item.media_id)}`,
        total: p.gallery_data.items.length,
        idx: idx+1,
      }));
    } else {
      return null;
    }
  }
-
  function convertInlineImageLinks(html) {
    // Find all anchors that href to preview.redd.it, i.redd.it, i.imgur.com
    // and contain just a link to the same href
    const expression = /<a href="(http[s]?:\/\/(?:preview\.redd\.it|i\.redd\.it|i\.imgur\.com).*?)">\1?<\/a>/g;
    const matches = html.matchAll(expression);
    var result = html;
    matches.forEach((match) => {
      // Replace each occurrence with an actual img tag
      result = result.replace(match[0], '<a href="' + match[1] + '"><img class="inline" src="' + match[1] + '"></a>');
    })

    return result;
  }
-
  function decodePostVideoUrls(p) {
    // Video URLs have querystring separators that are HTML-encoded, so replace them.
    const expression = /&amp;/g;

    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, '&') : '';

    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, '&') : '';

    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, '&') : '';

    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, '&') : '';

    var poster_url = p.preview && p.preview.images ? p.preview.images[0].source.url.replace(expression, '&') : '';

    return [hls_url, dash_url, fallback_url, scrubber_url, poster_url];
  }