From fb202af1e5d0146f70fc8a016478692179fafdb7 Mon Sep 17 00:00:00 2001 From: StevenNMeza Date: Mon, 21 Dec 2020 12:33:43 +0100 Subject: [PATCH] Render link flairs --- dist/css/styles.css | 24 +++++++----------------- inc/commons.js | 23 +++++++++++++++++++++++ inc/processJsonPost.js | 1 + inc/processJsonSubreddit.js | 1 + views/post.pug | 3 +-- views/subreddit.pug | 6 ++---- 6 files changed, 35 insertions(+), 23 deletions(-) diff --git a/dist/css/styles.css b/dist/css/styles.css index 8816657..7bccea4 100644 --- a/dist/css/styles.css +++ b/dist/css/styles.css @@ -172,11 +172,6 @@ body.dark #search form input[type="text"] { background: #0f0f0f; color: white; } -body.dark #links .link .entry .title span.postflair, -body.dark #post .info .title span.postflair { - color: #eaeaea; - background-color: #404040; -} a { color: var(--linkcolor); text-decoration: none; @@ -402,16 +397,6 @@ input[type="submit"]:hover, cursor: pointer; text-decoration: none; } -#links .link .entry .title span.postflair, -#post .info .title span.postflair { - display: inline-block; - border-radius: 4px; - color: #404040; - background-color: #e8e8e8; - font-size: x-small; - margin-left: 10px; - padding: 0 2px; -} /* SUBREDDIT LINKS */ #links { float: left; @@ -995,8 +980,9 @@ input[type="submit"]:hover, margin-right: 10px; } /* FLAIR */ -.flair { - /* Todo: Merge this with post flairs (.postflair) */ +.flair, +#links .link .entry .title span.flair, +#post .info .title span.flair { display: inline-block; border-radius: 4px; color: #404040; @@ -1005,6 +991,10 @@ input[type="submit"]:hover, margin-left: 10px; padding: 0 2px; } +body.dark .flair { + color: #eaeaea !important; + background-color: #404040 !important; +} #post .comments .flair, #user .comment .meta .flair { margin-left: 0 !important; diff --git a/inc/commons.js b/inc/commons.js index 5647e92..c3b6184 100644 --- a/inc/commons.js +++ b/inc/commons.js @@ -198,6 +198,29 @@ module.exports = function(request, fs) { } } + this.formatLinkFlair = (post) => { + const wrap = (inner) => `${inner}` + + if (post.link_flair_text === null) + return '' + + if (post.link_flair_type === 'text') + return wrap(post.link_flair_text) + + if (post.link_flair_type === 'richtext') { + let flair = '' + for (let fragment of post.link_flair_richtext) { + if (fragment.e === 'text') + flair += fragment.t + else if (fragment.e === 'emoji') + flair += `` + } + return wrap(flair) + } + + return '' + } + this.formatUserFlair = (post) => { // Generate the entire HTML here for consistency in both pug and HTML const wrap = (inner) => `${inner}` diff --git a/inc/processJsonPost.js b/inc/processJsonPost.js index 0d51b8c..d692363 100644 --- a/inc/processJsonPost.js +++ b/inc/processJsonPost.js @@ -33,6 +33,7 @@ module.exports = function(fetch) { images: null, crosspost: false, selftext: unescape(post.selftext_html), + link_flair: formatLinkFlair(post), user_flair: formatUserFlair(post) } diff --git a/inc/processJsonSubreddit.js b/inc/processJsonSubreddit.js index 8362b2d..1dc8e42 100644 --- a/inc/processJsonSubreddit.js +++ b/inc/processJsonSubreddit.js @@ -74,6 +74,7 @@ module.exports = function() { stickied: data.stickied, is_self_link: is_self_link, subreddit_front: subreddit_front, + link_flair: formatLinkFlair(data), user_flair: formatUserFlair(data) } ret.links.push(obj) diff --git a/views/post.pug b/views/post.pug index 03b7b75..96c0114 100644 --- a/views/post.pug +++ b/views/post.pug @@ -23,8 +23,7 @@ html .title a(href="" + post.url + "") h2 #{cleanTitle(post.title)} - if post.link_flair_text - span(class="postflair") #{post.link_flair_text} + != post.link_flair span(class="domain") (#{post.domain}) p.submitted span(title="" + toUTCString(post.created) + "") submitted #{timeDifference(post.created)} by diff --git a/views/subreddit.pug b/views/subreddit.pug index d4f7863..bbf90a0 100644 --- a/views/subreddit.pug +++ b/views/subreddit.pug @@ -84,14 +84,12 @@ html if link.is_self_link a(href="" + link.permalink + "") h2(class="" + (link.stickied ? 'green' : '') + "") #{cleanTitle(link.title)} - if link.link_flair_text - span(class="postflair") #{link.link_flair_text} + != link.link_flair span (#{link.domain}) else a(href="" + link.url + "") h2(class="" + (link.stickied ? 'green' : '') + "") #{cleanTitle(link.title)} - if link.link_flair_text - span(class="postflair") #{link.link_flair_text} + != link.link_flair span (#{link.domain}) .meta p.submitted submitted