From 19e250a3c671757f256c77a41fd91804a30eead6 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 12 Jul 2022 23:25:06 +0100 Subject: [PATCH] fix: og image urls (#712) --- api/_build.mjs | 7 ++----- api/_chromium.ts | 3 +-- api/_parser.ts | 2 +- api/_utils.js | 8 ++++++++ scripts/global.mjs | 9 --------- scripts/publish.mjs | 3 ++- 6 files changed, 14 insertions(+), 18 deletions(-) create mode 100644 api/_utils.js diff --git a/api/_build.mjs b/api/_build.mjs index 2ec4fce4..c039dab1 100644 --- a/api/_build.mjs +++ b/api/_build.mjs @@ -1,10 +1,7 @@ import grayMatter from 'gray-matter' -import { - iterateOnDirectory, - getMatchOrReturn, - writeFile, -} from '../scripts/global.mjs' +import { iterateOnDirectory, writeFile } from '../scripts/global.mjs' +import { getMatchOrReturn } from './_utils.js' const metadata = {} diff --git a/api/_chromium.ts b/api/_chromium.ts index 90a03991..dfcaab8a 100644 --- a/api/_chromium.ts +++ b/api/_chromium.ts @@ -1,5 +1,4 @@ import chrome from 'chrome-aws-lambda' -import { launch } from 'puppeteer-core' const exePath = '/usr/bin/google-chrome-unstable' @@ -35,7 +34,7 @@ function sleep(ms: number) { export async function getScreenshot(url: string, isDev: boolean) { const options = await getOptions(isDev) - const browser = await launch(options) + const browser = await chrome.puppeteer.launch(options) const page = await browser.newPage() await page.setViewport({ width: 1200, height: 630 }) await page.goto(url) diff --git a/api/_parser.ts b/api/_parser.ts index 49e7a644..6978b77f 100644 --- a/api/_parser.ts +++ b/api/_parser.ts @@ -1,6 +1,6 @@ import { IncomingMessage } from 'http' -import { getMatchOrReturn } from '../scripts/global.mjs' +import { getMatchOrReturn } from './_utils' import { metadata } from './metadata.json' diff --git a/api/_utils.js b/api/_utils.js new file mode 100644 index 00000000..6621ff6a --- /dev/null +++ b/api/_utils.js @@ -0,0 +1,8 @@ +/** + * @type {(haystack: string, needle: string | RegExp, index: number ) => string} getMatchOrReturn + */ +module.exports.getMatchOrReturn = (haystack, needle, index = 0) => { + const matches = haystack.match(needle) + if (!matches || !matches.length || matches.length < index + 1) return '' + return matches[index] +} diff --git a/scripts/global.mjs b/scripts/global.mjs index 0fddfef5..e3ae0819 100644 --- a/scripts/global.mjs +++ b/scripts/global.mjs @@ -21,15 +21,6 @@ export function iterateOnDirectory(directory, callback) { ) } -/** - * @type {(haystack: string, needle: string | RegExp, index: number ) => string} getMatchOrReturn - */ -export function getMatchOrReturn(haystack, needle, index = 0) { - const matches = haystack.match(needle) - if (!matches || !matches.length || matches.length < index + 1) return '' - return matches[index] -} - /** * @type {(message: string, filename: string, contents: string) => void} writeFile */ diff --git a/scripts/publish.mjs b/scripts/publish.mjs index 22122cd2..d8b0921b 100644 --- a/scripts/publish.mjs +++ b/scripts/publish.mjs @@ -2,7 +2,8 @@ import { $fetch } from 'ohmyfetch' -import { iterateOnDirectory, getMatchOrReturn } from './global.mjs' +import { iterateOnDirectory } from './global.mjs' +import { getMatchOrReturn } from './api/_utils.js' const url = 'https://dev.to/api' const token = process.env.DEVTO_TOKEN || 'CYgR6zbcVgtKDRkawFYZKrCT' -- 2.51.2