Skip to content

Commit

Permalink
add: parseOgp function
Browse files Browse the repository at this point in the history
  • Loading branch information
TakahiroHimi committed Oct 16, 2021
1 parent 80d7635 commit 2859434
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ogp-fetcher",
"version": "0.0.1",
"version": "1.0.0",
"description": "Library to fetch OGB",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
47 changes: 35 additions & 12 deletions src/fetchOgp.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import axios, { AxiosRequestHeaders } from "axios";
import { JSDOM } from "jsdom";

/**
*
* @param targetUrls
* @returns ogp list
*/
export const fetchOgp = async (targetUrls: string[]) => {
const headers: AxiosRequestHeaders = { "User-Agent": "bot" };

Expand All @@ -9,24 +14,40 @@ export const fetchOgp = async (targetUrls: string[]) => {
});

const responses = await Promise.all(fetches);
const htmlList = responses.reduce((prev: string[], res) => {
if (typeof res.data !== "string") return prev;
return [...prev, res.data];
}, []);

const ogpsList = responses.reduce(
(prev: { [key: string]: string }[], res) => {
if (typeof res.data !== "string") return prev;
const dom = new JSDOM(res.data);
const meta = dom.window.document.head.querySelectorAll("meta");
const ogps = parseOgp(htmlList);

const ogps = ogpFilter(meta);
return ogps;
};

return [...prev, ogps];
},
[]
);
/**
*
* @param htmlList
* @returns ogp list
*/
export const parseOgp = (htmlList: string[]) => {
const ogps = htmlList.reduce((prev: { [key: string]: string }[], html) => {
const dom = new JSDOM(html);
const meta = dom.window.document.head.querySelectorAll("meta");

return ogpsList;
const ogps = ogpFilter(meta);

return [...prev, ogps];
}, []);

return ogps;
};

const ogpFilter = (metaElements: NodeListOf<HTMLMetaElement>) => {
/**
*
* @param metaElements
* @returns ogp object
*/
export const ogpFilter = (metaElements: NodeListOf<HTMLMetaElement>) => {
const ogps = [...Array(metaElements.length).keys()].reduce(
(prev: { [key: string]: string }, i) => {
const property = metaElements.item(i).getAttribute("property")?.trim();
Expand All @@ -39,3 +60,5 @@ const ogpFilter = (metaElements: NodeListOf<HTMLMetaElement>) => {
);
return ogps;
};

export default fetchOgp;
4 changes: 1 addition & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import { fetchOgp } from "./fetchOgp";

export default fetchOgp;
export * from "./fetchOgp";
105 changes: 102 additions & 3 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import fetchOGP from "../src/index";
import { JSDOM } from "jsdom";
import fetchOgp, { ogpFilter, parseOgp } from "../src/fetchOgp";

describe("test", () => {
test("fetch ogp", () => {
return expect(
fetchOGP(["https://takahirohimi.github.io/ogp-fetcher/"])
fetchOgp(["https://takahirohimi.github.io/ogp-fetcher/"])
).resolves.toStrictEqual([
{
["og:title"]: "title",
Expand All @@ -20,7 +21,7 @@ describe("test", () => {

test("fetch multiple ogp", () => {
return expect(
fetchOGP([
fetchOgp([
"https://takahirohimi.github.io/ogp-fetcher/",
"https://takahirohimi.github.io/ogp-fetcher/foo.html",
])
Expand All @@ -47,4 +48,102 @@ describe("test", () => {
},
]);
});

test("parse ogp", () => {
return expect(parseOgp([testHtmlTextIndex])).toStrictEqual([
{
["og:title"]: "title",
["og:description"]: "description",
["og:locale"]: "locale",
["og:type"]: "type",
["og:url"]: "https://example.com",
["og:image:width"]: "200",
["og:image:height"]: "100",
["og:image"]: "https://example.com/image.png",
},
]);
});

test("parse multiple ogp", () => {
return expect(parseOgp([testHtmlTextIndex, testHtmlTextFoo])).toStrictEqual(
[
{
["og:title"]: "title",
["og:description"]: "description",
["og:locale"]: "locale",
["og:type"]: "type",
["og:url"]: "https://example.com",
["og:image:width"]: "200",
["og:image:height"]: "100",
["og:image"]: "https://example.com/image.png",
},
{
["og:title"]: "footitle",
["og:description"]: "foodescription",
["og:locale"]: "foolocale",
["og:type"]: "footype",
["og:url"]: "https://example.com/foo",
["og:image:width"]: "300",
["og:image:height"]: "200",
["og:image"]: "https://example.com/foo.png",
},
]
);
});

test("ogp filter", () => {
const dom = new JSDOM(testHtmlTextIndex);
const meta = dom.window.document.head.querySelectorAll("meta");

return expect(ogpFilter(meta)).toStrictEqual({
["og:title"]: "title",
["og:description"]: "description",
["og:locale"]: "locale",
["og:type"]: "type",
["og:url"]: "https://example.com",
["og:image:width"]: "200",
["og:image:height"]: "100",
["og:image"]: "https://example.com/image.png",
});
});
});

const testHtmlTextIndex = `
<html lang="ja">
<head>
<meta charset="UTF-8" />
<meta property="og:title" content="title" />
<meta property="og:description" content="description" />
<meta property="og:locale" content="locale" />
<meta property="og:type" content="type" />
<meta property="og:url" content="https://example.com" />
<meta property="og:image:width" content="200" />
<meta property="og:image:height" content="100" />
<meta property="og:image" content="https://example.com/image.png" />
<title>Hello World</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
`;

const testHtmlTextFoo = `
<html lang="ja">
<head>
<meta charset="UTF-8" />
<meta property="og:title" content="footitle" />
<meta property="og:description" content="foodescription" />
<meta property="og:locale" content="foolocale" />
<meta property="og:type" content="footype" />
<meta property="og:url" content="https://example.com/foo" />
<meta property="og:image:width" content="300" />
<meta property="og:image:height" content="200" />
<meta property="og:image" content="https://example.com/foo.png" />
<title>foo</title>
</head>
<body>
<h1>foo</h1>
</body>
</html>
`;

0 comments on commit 2859434

Please sign in to comment.