diff --git a/lib/utils/readable-social.js b/lib/utils/readable-social.ts similarity index 91% rename from lib/utils/readable-social.js rename to lib/utils/readable-social.ts index 0e87b4b12..2f63979b8 100644 --- a/lib/utils/readable-social.js +++ b/lib/utils/readable-social.ts @@ -39,8 +39,4 @@ const queryToInteger = (s) => { return Number.parseInt(s); }; -module.exports = { - fallback, - queryToBoolean, - queryToInteger, -}; +export { fallback, queryToBoolean, queryToInteger }; diff --git a/lib/utils/timezone.js b/lib/utils/timezone.ts similarity index 71% rename from lib/utils/timezone.js rename to lib/utils/timezone.ts index 4e56f47b4..1e967b697 100644 --- a/lib/utils/timezone.js +++ b/lib/utils/timezone.ts @@ -3,12 +3,12 @@ const assert = require('assert').strict; const millisInAnHour = 60 * 60 * 1000; const serverTimezone = -new Date().getTimezoneOffset() / 60; -module.exports = (date, timezone = serverTimezone) => { - if (typeof date === 'string' || date instanceof String) { +export default function (date, timezone = serverTimezone) { + if (typeof date === 'string') { date = new Date(date); } assert(date instanceof Date); return new Date(date.getTime() - millisInAnHour * (timezone - serverTimezone)); -}; +} diff --git a/lib/utils/valid-host.js b/lib/utils/valid-host.ts similarity index 89% rename from lib/utils/valid-host.js rename to lib/utils/valid-host.ts index 1a4868f60..b6e2ee663 100644 --- a/lib/utils/valid-host.js +++ b/lib/utils/valid-host.ts @@ -11,6 +11,4 @@ const isValidHost = (hostname) => { return regex.test(hostname); }; -module.exports = { - isValidHost, -}; +export { isValidHost }; diff --git a/lib/utils/wechat-mp.js b/lib/utils/wechat-mp.ts similarity index 88% rename from lib/utils/wechat-mp.js rename to lib/utils/wechat-mp.ts index 091a5368f..4ea1c811e 100644 --- a/lib/utils/wechat-mp.js +++ b/lib/utils/wechat-mp.ts @@ -25,9 +25,9 @@ * For more details of these functions, please refer to the jsDoc in the source code. */ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); -const { parseDate } = require('@/utils/parse-date'); +import got from '@/utils/got'; +import { load } from 'cheerio'; +import { parseDate } from '@/utils/parse-date'; const replaceTag = ($, oldTag, newTagName) => { oldTag = $(oldTag); @@ -81,24 +81,24 @@ const fixArticleContent = (html, skipImg = false) => { if (!html) { return ''; } - const $ = cheerio.load(html, undefined, false); + const $ = load(html, undefined, false); if (!skipImg) { // fix img lazy loading $('img[data-src]').each((_, img) => { - img = $(img); - const realSrc = img.attr('data-src'); + const $img = $(img); + const realSrc = $img.attr('data-src'); if (realSrc) { - img.attr('src', realSrc); - img.removeAttr('data-src'); + $img.attr('src', realSrc); + $img.removeAttr('data-src'); } }); } // fix section $('section').each((_, section) => { - section = $(section); - const p_count = section.find('p').length; - const div_count = section.find('div').length; - const section_count = section.find('section').length; + const $section = $(section); + const p_count = $section.find('p').length; + const div_count = $section.find('div').length; + const section_count = $section.find('section').length; if (p_count + div_count + section_count === 0) { // make it a p replaceTag($, section, 'p'); @@ -119,11 +119,11 @@ const fixArticleContent = (html, skipImg = false) => { // fix single picture article // example: https://mp.weixin.qq.com/s/4p5YmYuASiQSYFiy7KqydQ $('script').each((_, script) => { - script = $(script); - const matchs = script.html().match(/document\.getElementById\('js_image_desc'\)\.innerHTML = "(.*)"\.replace/); + const $script = $(script); + const matchs = $script.html()?.match(/document\.getElementById\('js_image_desc'\)\.innerHTML = "(.*)"\.replace/); if (matchs) { - script.replaceWith(matchs[1].replaceAll('\r', '').replaceAll('\n', '
').replaceAll('\\x0d', '').replaceAll('\\x0a', '
')); + $script.replaceWith(matchs[1].replaceAll('\r', '').replaceAll('\n', '
').replaceAll('\\x0d', '').replaceAll('\\x0a', '
')); } }); @@ -191,7 +191,8 @@ const fetchArticle = (ctx, url, bypassHostCheck = false) => { url = normalizeUrl(url, bypassHostCheck); return ctx.cache.tryGet(url, async () => { const response = await got(url); - const $ = cheerio.load(response.data); + // @ts-expect-error custom field + const $ = load(response.data); const title = ($('meta[property="og:title"]').attr('content') || '').replaceAll('\\r', '').replaceAll('\\n', ' '); const author = $('meta[name=author]').attr('content'); @@ -203,7 +204,8 @@ const fetchArticle = (ctx, url, bypassHostCheck = false) => { if (originalUrl) { // try to fetch the description from the original article const originalResponse = await got(normalizeUrl(originalUrl, bypassHostCheck)); - const original$ = cheerio.load(originalResponse.data); + // @ts-expect-error custom field + const original$ = load(originalResponse.data); description += fixArticleContent(original$('#js_content')); } @@ -217,7 +219,7 @@ const fetchArticle = (ctx, url, bypassHostCheck = false) => { const publish_time_match = publish_time_script && publish_time_script.match(/var ct *= *"?(\d{10})"?/); const publish_timestamp = publish_time_match && publish_time_match[1]; if (publish_timestamp) { - pubDate = parseDate(publish_timestamp * 1000); + pubDate = parseDate(Number.parseInt(publish_timestamp) * 1000); } let mpName = $('.profile_nickname').first().text(); @@ -257,11 +259,4 @@ const finishArticleItem = async (ctx, item, setMpNameAsAuthor = false, skipLink return item; }; -module.exports = { - fixArticleContent, - fetchArticle, - finishArticleItem, // a new route SHOULD use this function instead of manually calling the above functions - _internal: { - normalizeUrl, // for internal use only, exported for testing - }, -}; +export { fixArticleContent, fetchArticle, finishArticleItem, normalizeUrl };