From 0b96c071fcc8f7db2a857ee4ea28cfa990131b45 Mon Sep 17 00:00:00 2001 From: "Jocelyn Badgley (Twipped)" Date: Tue, 7 Apr 2020 10:34:21 -0700 Subject: [PATCH] Move parseTweetId into page-tweets --- build/page-tweets.js | 29 +++++++++++++++++++++++++++++ build/page.js | 15 +-------------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/build/page-tweets.js b/build/page-tweets.js index d073f26..e697e63 100644 --- a/build/page-tweets.js +++ b/build/page-tweets.js @@ -107,3 +107,32 @@ function getTwitterClient (config) { .get('statuses/lookup', { id: tweetids.join(','), tweet_mode: 'extended' }) .catch((e) => { log.error(e); return []; }); } + +const tweeturl = /https?:\/\/twitter\.com\/(?:#!\/)?(?:\w+)\/status(?:es)?\/(\d+)/i; +const tweetidcheck = /^\d+$/; +function parseTweetId (tweetid) { + // we can't trust an id that isn't a string + if (typeof tweetid !== 'string') return false; + + const match = tweetid.match(tweeturl); + if (match) return match[1]; + if (tweetid.match(tweetidcheck)) return tweetid; + return false; +} + +exports.parseTweetId = parseTweetId; + +exports.attachTweets = function (tweetids, tweets) { + function attachTweet (dict, tweetid) { + const tweet = tweets[tweetid]; + if (!tweet) return; + dict[tweetid] = tweet; + + if (tweet.quoted_status_id_str) attachTweet(dict, tweet.quoted_status_id_str); + } + + return tweetids.reduce((dict, tweetid) => { + attachTweet(dict, tweetid); + return dict; + }, {}); +}; diff --git a/build/page.js b/build/page.js index d2a384f..33fffb0 100644 --- a/build/page.js +++ b/build/page.js @@ -8,6 +8,7 @@ const actions = require('./actions'); const { URL } = require('url'); const { resolve, readFile, isCleanUrl, TYPE, ENGINE } = require('./resolve'); const { isObject, isString } = require('./lib/util'); +const { parseTweetId } = require('./page-tweets'); const pkg = require(resolve('package.json')); const frontmatter = require('front-matter'); @@ -131,17 +132,3 @@ module.exports = exports = class Page extends File { } }; - -/* Utility Functions **************************************************/ - -const tweeturl = /https?:\/\/twitter\.com\/(?:#!\/)?(?:\w+)\/status(?:es)?\/(\d+)/i; -const tweetidcheck = /^\d+$/; -function parseTweetId (tweetid) { - // we can't trust an id that isn't a string - if (typeof tweetid !== 'string') return false; - - const match = tweetid.match(tweeturl); - if (match) return match[1]; - if (tweetid.match(tweetidcheck)) return tweetid; - return false; -}