rehype-og-card 0.3.1 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +2 -2
- package/dist/types.d.ts +12 -1
- package/dist/util/cache.js +1 -26
- package/dist/util/file.d.ts +13 -0
- package/dist/util/file.js +40 -0
- package/dist/util/hast.d.ts +37 -0
- package/dist/util/hast.js +68 -0
- package/dist/util/network.d.ts +35 -0
- package/dist/{util.js → util/network.js} +36 -35
- package/package.json +1 -1
- package/dist/openGraphUtil.d.ts +0 -31
- package/dist/openGraphUtil.js +0 -91
- package/dist/util.d.ts +0 -58
package/dist/index.js
CHANGED
|
@@ -7,8 +7,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import { convertTextToAnchorElement,
|
|
11
|
-
import {
|
|
10
|
+
import { convertTextToAnchorElement, createOGCard, isAnchorElement, isTextNode } from "./util/hast.js";
|
|
11
|
+
import { downloadImage, getOGData, isValidURL } from "./util/network.js";
|
|
12
12
|
import { restoreBuildCache, saveBuildCache } from "./util/cache.js";
|
|
13
13
|
import { isElement } from "hast-util-is-element";
|
|
14
14
|
import path from "path";
|
package/dist/types.d.ts
CHANGED
|
@@ -54,4 +54,15 @@ interface RehypeOGCardOptions {
|
|
|
54
54
|
*/
|
|
55
55
|
crawlerUserAgent?: string;
|
|
56
56
|
}
|
|
57
|
-
|
|
57
|
+
interface OGCardData {
|
|
58
|
+
url: string;
|
|
59
|
+
title: string;
|
|
60
|
+
description?: string;
|
|
61
|
+
displayURL: string;
|
|
62
|
+
faviconURL?: string;
|
|
63
|
+
OGImageURL?: string;
|
|
64
|
+
OGImageAlt?: string;
|
|
65
|
+
OGImageWidth?: number;
|
|
66
|
+
OGImageHeight?: number;
|
|
67
|
+
}
|
|
68
|
+
export { RehypeOGCardOptions, OGCardData };
|
package/dist/util/cache.js
CHANGED
|
@@ -7,32 +7,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import {
|
|
11
|
-
import fs from "fs/promises";
|
|
12
|
-
import path from "path";
|
|
13
|
-
/**
|
|
14
|
-
* Copy directory from source to destination.
|
|
15
|
-
* @param source Source directory.
|
|
16
|
-
* @param destination Destination directory.
|
|
17
|
-
*/
|
|
18
|
-
// eslint-disable-next-line max-statements
|
|
19
|
-
const copyDirectory = (source, destination) => __awaiter(void 0, void 0, void 0, function* () {
|
|
20
|
-
const sourceExists = yield checkFileExists(source);
|
|
21
|
-
if (!sourceExists)
|
|
22
|
-
return;
|
|
23
|
-
const destinationExists = yield checkFileExists(destination);
|
|
24
|
-
if (!destinationExists) {
|
|
25
|
-
yield fs.mkdir(destination, { recursive: true });
|
|
26
|
-
}
|
|
27
|
-
const files = yield fs.readdir(source);
|
|
28
|
-
const promises = [];
|
|
29
|
-
for (const file of files) {
|
|
30
|
-
const sourceFile = path.join(source, file);
|
|
31
|
-
const destFile = path.join(destination, file);
|
|
32
|
-
promises.push(fs.copyFile(sourceFile, destFile));
|
|
33
|
-
}
|
|
34
|
-
yield Promise.all(promises);
|
|
35
|
-
});
|
|
10
|
+
import { copyDirectory } from "./file.js";
|
|
36
11
|
/**
|
|
37
12
|
* Save build cache if server cache exists.
|
|
38
13
|
* @param serverCachePath Server cache path.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check if the file exists.
|
|
3
|
+
* @param filePath Path to check.
|
|
4
|
+
* @returns `true` if the file exists, `false` otherwise.
|
|
5
|
+
*/
|
|
6
|
+
declare const checkFileExists: (filePath: string) => Promise<boolean>;
|
|
7
|
+
/**
|
|
8
|
+
* Copy directory from source to destination.
|
|
9
|
+
* @param source Source directory.
|
|
10
|
+
* @param destination Destination directory.
|
|
11
|
+
*/
|
|
12
|
+
declare const copyDirectory: (source: string, destination: string) => Promise<void>;
|
|
13
|
+
export { checkFileExists, copyDirectory };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import fs from "fs/promises";
|
|
11
|
+
/**
|
|
12
|
+
* Check if the file exists.
|
|
13
|
+
* @param filePath Path to check.
|
|
14
|
+
* @returns `true` if the file exists, `false` otherwise.
|
|
15
|
+
*/
|
|
16
|
+
const checkFileExists = (filePath) => __awaiter(void 0, void 0, void 0, function* () {
|
|
17
|
+
try {
|
|
18
|
+
yield fs.access(filePath);
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
catch (_a) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
/**
|
|
26
|
+
* Copy directory from source to destination.
|
|
27
|
+
* @param source Source directory.
|
|
28
|
+
* @param destination Destination directory.
|
|
29
|
+
*/
|
|
30
|
+
const copyDirectory = (source, destination) => __awaiter(void 0, void 0, void 0, function* () {
|
|
31
|
+
const sourceExists = yield checkFileExists(source);
|
|
32
|
+
if (!sourceExists)
|
|
33
|
+
return;
|
|
34
|
+
const destinationExists = yield checkFileExists(destination);
|
|
35
|
+
if (!destinationExists) {
|
|
36
|
+
yield fs.mkdir(destination, { recursive: true });
|
|
37
|
+
}
|
|
38
|
+
yield fs.cp(source, destination, { recursive: true });
|
|
39
|
+
});
|
|
40
|
+
export { checkFileExists, copyDirectory };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Element, ElementContent, Text } from "hast";
|
|
2
|
+
import { OGCardData, RehypeOGCardOptions } from "../types.js";
|
|
3
|
+
interface AnchorElement extends Element {
|
|
4
|
+
tagName: "a";
|
|
5
|
+
properties: {
|
|
6
|
+
href: string;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Check if the node is a text node.
|
|
11
|
+
* @param node Node to check.
|
|
12
|
+
* @returns `true` if the node is a text node, `false` otherwise.
|
|
13
|
+
*/
|
|
14
|
+
declare const isTextNode: (node: ElementContent) => node is Text;
|
|
15
|
+
/**
|
|
16
|
+
* Check if the node is an anchor element.
|
|
17
|
+
* @param node Node to check.
|
|
18
|
+
* @returns `true` if the node is an anchor element, `false` otherwise.
|
|
19
|
+
*/
|
|
20
|
+
declare const isAnchorElement: (node: unknown) => node is AnchorElement;
|
|
21
|
+
/**
|
|
22
|
+
* Convert text node to anchor element. **This function does not check if the text is a valid URL.**
|
|
23
|
+
* @param text Text node to convert.
|
|
24
|
+
* @returns Anchor element.
|
|
25
|
+
*/
|
|
26
|
+
declare const convertTextToAnchorElement: (text: Text) => AnchorElement;
|
|
27
|
+
/**
|
|
28
|
+
* Create OG card element from given data.
|
|
29
|
+
* @param data Data to create OG card.
|
|
30
|
+
* @param options Options to create OG card.
|
|
31
|
+
* @returns OG card element.
|
|
32
|
+
*/
|
|
33
|
+
declare const createOGCard: (data: OGCardData, options: Required<{
|
|
34
|
+
loading: RehypeOGCardOptions["loading"];
|
|
35
|
+
decoding: RehypeOGCardOptions["decoding"];
|
|
36
|
+
}>) => Element;
|
|
37
|
+
export { AnchorElement, isTextNode, isAnchorElement, convertTextToAnchorElement, createOGCard };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { h } from "hastscript";
|
|
2
|
+
import { isElement } from "hast-util-is-element";
|
|
3
|
+
/**
|
|
4
|
+
* Check if the node is a text node.
|
|
5
|
+
* @param node Node to check.
|
|
6
|
+
* @returns `true` if the node is a text node, `false` otherwise.
|
|
7
|
+
*/
|
|
8
|
+
const isTextNode = (node) => Boolean(node) && typeof node === "object" && "type" in node && node.type === "text";
|
|
9
|
+
/**
|
|
10
|
+
* Check if the node is an anchor element.
|
|
11
|
+
* @param node Node to check.
|
|
12
|
+
* @returns `true` if the node is an anchor element, `false` otherwise.
|
|
13
|
+
*/
|
|
14
|
+
const isAnchorElement = (node) => Boolean(node) && isElement(node, "a") && "href" in node.properties && typeof node.properties.href === "string";
|
|
15
|
+
/**
|
|
16
|
+
* Convert text node to anchor element. **This function does not check if the text is a valid URL.**
|
|
17
|
+
* @param text Text node to convert.
|
|
18
|
+
* @returns Anchor element.
|
|
19
|
+
*/
|
|
20
|
+
const convertTextToAnchorElement = (text) => ({
|
|
21
|
+
children: [text],
|
|
22
|
+
properties: {
|
|
23
|
+
href: text.value.trim()
|
|
24
|
+
},
|
|
25
|
+
tagName: "a",
|
|
26
|
+
type: "element"
|
|
27
|
+
});
|
|
28
|
+
/**
|
|
29
|
+
* Create OG card element from given data.
|
|
30
|
+
* @param data Data to create OG card.
|
|
31
|
+
* @param options Options to create OG card.
|
|
32
|
+
* @returns OG card element.
|
|
33
|
+
*/
|
|
34
|
+
const createOGCard = (data, options) => {
|
|
35
|
+
const card = h("div.og-card-container", [
|
|
36
|
+
h("a", { href: data.url }, [
|
|
37
|
+
h("div.og-card-info", [
|
|
38
|
+
h("div.og-card-title", data.title),
|
|
39
|
+
data.description ? h("div.og-card-description", data.description) : null,
|
|
40
|
+
h("div.og-card-url-container", [
|
|
41
|
+
h("img.og-card-favicon", {
|
|
42
|
+
alt: "favicon",
|
|
43
|
+
decoding: options.decoding,
|
|
44
|
+
height: 16,
|
|
45
|
+
loading: options.loading,
|
|
46
|
+
src: data.faviconURL,
|
|
47
|
+
width: 16
|
|
48
|
+
}),
|
|
49
|
+
h("span.og-card-url", data.displayURL)
|
|
50
|
+
])
|
|
51
|
+
]),
|
|
52
|
+
data.OGImageURL
|
|
53
|
+
? h("div.og-card-image-container", [
|
|
54
|
+
h("img.og-card-image", {
|
|
55
|
+
alt: data.OGImageAlt || data.OGImageURL,
|
|
56
|
+
decoding: options.decoding,
|
|
57
|
+
height: data.OGImageHeight,
|
|
58
|
+
loading: options.loading,
|
|
59
|
+
src: data.OGImageURL,
|
|
60
|
+
width: data.OGImageWidth
|
|
61
|
+
})
|
|
62
|
+
])
|
|
63
|
+
: null
|
|
64
|
+
])
|
|
65
|
+
]);
|
|
66
|
+
return card;
|
|
67
|
+
};
|
|
68
|
+
export { isTextNode, isAnchorElement, convertTextToAnchorElement, createOGCard };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { OGCardData } from "../types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Check if the URL is valid.
|
|
4
|
+
* @param url URL to check.
|
|
5
|
+
* @returns `true` if the URL is valid, `false` otherwise.
|
|
6
|
+
*/
|
|
7
|
+
declare const isValidURL: (url: string) => boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Get OG card data from given URL.
|
|
10
|
+
* @param url URL to get OG card data.
|
|
11
|
+
* @param userAgent User agent to use for fetching.
|
|
12
|
+
* @returns OG card data.
|
|
13
|
+
*/
|
|
14
|
+
declare const getOGData: (url: string, userAgent: string) => Promise<OGCardData | null>;
|
|
15
|
+
interface DownloadImageOptions {
|
|
16
|
+
/**
|
|
17
|
+
* URL to download image.
|
|
18
|
+
*/
|
|
19
|
+
url: string;
|
|
20
|
+
/**
|
|
21
|
+
* Directory to save the downloaded image.
|
|
22
|
+
*/
|
|
23
|
+
directly: string;
|
|
24
|
+
/**
|
|
25
|
+
* User agent to use for fetching the image.
|
|
26
|
+
*/
|
|
27
|
+
userAgent: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Download image from given URL.
|
|
31
|
+
* @param options Options to download image.
|
|
32
|
+
* @returns Path to the downloaded image.
|
|
33
|
+
*/
|
|
34
|
+
declare const downloadImage: (options: DownloadImageOptions) => Promise<string | null>;
|
|
35
|
+
export { isValidURL, getOGData, downloadImage };
|
|
@@ -7,22 +7,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
+
import { checkFileExists } from "./file.js";
|
|
10
11
|
import { createHash } from "crypto";
|
|
11
12
|
import fs from "fs/promises";
|
|
12
|
-
import { isElement } from "hast-util-is-element";
|
|
13
13
|
import path from "path";
|
|
14
|
-
|
|
15
|
-
* Check if the node is a text node.
|
|
16
|
-
* @param node Node to check.
|
|
17
|
-
* @returns `true` if the node is a text node, `false` otherwise.
|
|
18
|
-
*/
|
|
19
|
-
const isTextNode = (node) => Boolean(node) && typeof node === "object" && "type" in node && node.type === "text";
|
|
20
|
-
/**
|
|
21
|
-
* Check if the node is an anchor element.
|
|
22
|
-
* @param node Node to check.
|
|
23
|
-
* @returns `true` if the node is an anchor element, `false` otherwise.
|
|
24
|
-
*/
|
|
25
|
-
const isAnchorElement = (node) => Boolean(node) && isElement(node, "a") && "href" in node.properties && typeof node.properties.href === "string";
|
|
14
|
+
import scraper from "open-graph-scraper";
|
|
26
15
|
/**
|
|
27
16
|
* Check if the URL is valid.
|
|
28
17
|
* @param url URL to check.
|
|
@@ -39,30 +28,42 @@ const isValidURL = (url) => {
|
|
|
39
28
|
}
|
|
40
29
|
};
|
|
41
30
|
/**
|
|
42
|
-
*
|
|
43
|
-
* @param
|
|
44
|
-
* @
|
|
45
|
-
|
|
46
|
-
const convertTextToAnchorElement = (text) => ({
|
|
47
|
-
children: [text],
|
|
48
|
-
properties: {
|
|
49
|
-
href: text.value.trim()
|
|
50
|
-
},
|
|
51
|
-
tagName: "a",
|
|
52
|
-
type: "element"
|
|
53
|
-
});
|
|
54
|
-
/**
|
|
55
|
-
* Check if the file exists.
|
|
56
|
-
* @param filePath Path to check.
|
|
57
|
-
* @returns `true` if the file exists, `false` otherwise.
|
|
31
|
+
* Get OG card data from given URL.
|
|
32
|
+
* @param url URL to get OG card data.
|
|
33
|
+
* @param userAgent User agent to use for fetching.
|
|
34
|
+
* @returns OG card data.
|
|
58
35
|
*/
|
|
59
|
-
const
|
|
36
|
+
const getOGData = (url, userAgent) => __awaiter(void 0, void 0, void 0, function* () {
|
|
60
37
|
try {
|
|
61
|
-
yield
|
|
62
|
-
|
|
38
|
+
const { result } = yield scraper({
|
|
39
|
+
fetchOptions: {
|
|
40
|
+
headers: {
|
|
41
|
+
"user-agent": userAgent
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
url
|
|
45
|
+
});
|
|
46
|
+
const OGImage = result.ogImage ? result.ogImage[0] : null;
|
|
47
|
+
const faviconURL = result.favicon
|
|
48
|
+
? `https://www.google.com/s2/favicons?domain=${new URL(url).hostname}`
|
|
49
|
+
: // eslint-disable-next-line no-undefined
|
|
50
|
+
undefined;
|
|
51
|
+
return {
|
|
52
|
+
OGImageAlt: OGImage === null || OGImage === void 0 ? void 0 : OGImage.alt,
|
|
53
|
+
OGImageHeight: OGImage === null || OGImage === void 0 ? void 0 : OGImage.height,
|
|
54
|
+
OGImageURL: OGImage === null || OGImage === void 0 ? void 0 : OGImage.url,
|
|
55
|
+
OGImageWidth: OGImage === null || OGImage === void 0 ? void 0 : OGImage.width,
|
|
56
|
+
description: result.ogDescription,
|
|
57
|
+
displayURL: url,
|
|
58
|
+
faviconURL,
|
|
59
|
+
title: result.ogTitle || url,
|
|
60
|
+
url
|
|
61
|
+
};
|
|
63
62
|
}
|
|
64
|
-
catch (
|
|
65
|
-
|
|
63
|
+
catch (error) {
|
|
64
|
+
// eslint-disable-next-line no-console
|
|
65
|
+
console.error("[rehype-og-card] Error fetching OG data:", error);
|
|
66
|
+
return null;
|
|
66
67
|
}
|
|
67
68
|
});
|
|
68
69
|
/**
|
|
@@ -103,4 +104,4 @@ const downloadImage = (options) => __awaiter(void 0, void 0, void 0, function* (
|
|
|
103
104
|
return null;
|
|
104
105
|
}
|
|
105
106
|
});
|
|
106
|
-
export {
|
|
107
|
+
export { isValidURL, getOGData, downloadImage };
|
package/package.json
CHANGED
package/dist/openGraphUtil.d.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type { Element } from "hast";
|
|
2
|
-
import { RehypeOGCardOptions } from "./types.js";
|
|
3
|
-
interface OGCardData {
|
|
4
|
-
url: string;
|
|
5
|
-
title: string;
|
|
6
|
-
description?: string;
|
|
7
|
-
displayURL: string;
|
|
8
|
-
faviconURL?: string;
|
|
9
|
-
OGImageURL?: string;
|
|
10
|
-
OGImageAlt?: string;
|
|
11
|
-
OGImageWidth?: number;
|
|
12
|
-
OGImageHeight?: number;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Get OG card data from given URL.
|
|
16
|
-
* @param url URL to get OG card data.
|
|
17
|
-
* @param userAgent User agent to use for fetching.
|
|
18
|
-
* @returns OG card data.
|
|
19
|
-
*/
|
|
20
|
-
declare const getOGData: (url: string, userAgent: string) => Promise<OGCardData | null>;
|
|
21
|
-
/**
|
|
22
|
-
* Create OG card element from given data.
|
|
23
|
-
* @param data Data to create OG card.
|
|
24
|
-
* @param options Options to create OG card.
|
|
25
|
-
* @returns OG card element.
|
|
26
|
-
*/
|
|
27
|
-
declare const createOGCard: (data: OGCardData, options: Required<{
|
|
28
|
-
loading: RehypeOGCardOptions["loading"];
|
|
29
|
-
decoding: RehypeOGCardOptions["decoding"];
|
|
30
|
-
}>) => Element;
|
|
31
|
-
export { OGCardData, createOGCard, getOGData };
|
package/dist/openGraphUtil.js
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import { h } from "hastscript";
|
|
11
|
-
import scraper from "open-graph-scraper";
|
|
12
|
-
/**
|
|
13
|
-
* Get OG card data from given URL.
|
|
14
|
-
* @param url URL to get OG card data.
|
|
15
|
-
* @param userAgent User agent to use for fetching.
|
|
16
|
-
* @returns OG card data.
|
|
17
|
-
*/
|
|
18
|
-
const getOGData = (url, userAgent) => __awaiter(void 0, void 0, void 0, function* () {
|
|
19
|
-
try {
|
|
20
|
-
const { result } = yield scraper({
|
|
21
|
-
fetchOptions: {
|
|
22
|
-
headers: {
|
|
23
|
-
"user-agent": userAgent
|
|
24
|
-
}
|
|
25
|
-
},
|
|
26
|
-
url
|
|
27
|
-
});
|
|
28
|
-
const OGImage = result.ogImage ? result.ogImage[0] : null;
|
|
29
|
-
const faviconURL = result.favicon
|
|
30
|
-
? `https://www.google.com/s2/favicons?domain=${new URL(url).hostname}`
|
|
31
|
-
: // eslint-disable-next-line no-undefined
|
|
32
|
-
undefined;
|
|
33
|
-
return {
|
|
34
|
-
OGImageAlt: OGImage === null || OGImage === void 0 ? void 0 : OGImage.alt,
|
|
35
|
-
OGImageHeight: OGImage === null || OGImage === void 0 ? void 0 : OGImage.height,
|
|
36
|
-
OGImageURL: OGImage === null || OGImage === void 0 ? void 0 : OGImage.url,
|
|
37
|
-
OGImageWidth: OGImage === null || OGImage === void 0 ? void 0 : OGImage.width,
|
|
38
|
-
description: result.ogDescription,
|
|
39
|
-
displayURL: url,
|
|
40
|
-
faviconURL,
|
|
41
|
-
title: result.ogTitle || url,
|
|
42
|
-
url
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
catch (error) {
|
|
46
|
-
// eslint-disable-next-line no-console
|
|
47
|
-
console.error("[rehype-og-card] Error fetching OG data:", error);
|
|
48
|
-
return null;
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
/**
|
|
52
|
-
* Create OG card element from given data.
|
|
53
|
-
* @param data Data to create OG card.
|
|
54
|
-
* @param options Options to create OG card.
|
|
55
|
-
* @returns OG card element.
|
|
56
|
-
*/
|
|
57
|
-
const createOGCard = (data, options) => {
|
|
58
|
-
const card = h("div.og-card-container", [
|
|
59
|
-
h("a", { href: data.url }, [
|
|
60
|
-
h("div.og-card-info", [
|
|
61
|
-
h("div.og-card-title", data.title),
|
|
62
|
-
data.description ? h("div.og-card-description", data.description) : null,
|
|
63
|
-
h("div.og-card-url-container", [
|
|
64
|
-
h("img.og-card-favicon", {
|
|
65
|
-
alt: "favicon",
|
|
66
|
-
decoding: options.decoding,
|
|
67
|
-
height: 16,
|
|
68
|
-
loading: options.loading,
|
|
69
|
-
src: data.faviconURL,
|
|
70
|
-
width: 16
|
|
71
|
-
}),
|
|
72
|
-
h("span.og-card-url", data.displayURL)
|
|
73
|
-
])
|
|
74
|
-
]),
|
|
75
|
-
data.OGImageURL
|
|
76
|
-
? h("div.og-card-image-container", [
|
|
77
|
-
h("img.og-card-image", {
|
|
78
|
-
alt: data.OGImageAlt || data.OGImageURL,
|
|
79
|
-
decoding: options.decoding,
|
|
80
|
-
height: data.OGImageHeight,
|
|
81
|
-
loading: options.loading,
|
|
82
|
-
src: data.OGImageURL,
|
|
83
|
-
width: data.OGImageWidth
|
|
84
|
-
})
|
|
85
|
-
])
|
|
86
|
-
: null
|
|
87
|
-
])
|
|
88
|
-
]);
|
|
89
|
-
return card;
|
|
90
|
-
};
|
|
91
|
-
export { createOGCard, getOGData };
|
package/dist/util.d.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import type { Element, ElementContent, Text } from "hast";
|
|
2
|
-
interface AnchorElement extends Element {
|
|
3
|
-
tagName: "a";
|
|
4
|
-
properties: {
|
|
5
|
-
href: string;
|
|
6
|
-
};
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* Check if the node is a text node.
|
|
10
|
-
* @param node Node to check.
|
|
11
|
-
* @returns `true` if the node is a text node, `false` otherwise.
|
|
12
|
-
*/
|
|
13
|
-
declare const isTextNode: (node: ElementContent) => node is Text;
|
|
14
|
-
/**
|
|
15
|
-
* Check if the node is an anchor element.
|
|
16
|
-
* @param node Node to check.
|
|
17
|
-
* @returns `true` if the node is an anchor element, `false` otherwise.
|
|
18
|
-
*/
|
|
19
|
-
declare const isAnchorElement: (node: unknown) => node is AnchorElement;
|
|
20
|
-
/**
|
|
21
|
-
* Check if the URL is valid.
|
|
22
|
-
* @param url URL to check.
|
|
23
|
-
* @returns `true` if the URL is valid, `false` otherwise.
|
|
24
|
-
*/
|
|
25
|
-
declare const isValidURL: (url: string) => boolean;
|
|
26
|
-
/**
|
|
27
|
-
* Convert text node to anchor element. **This function does not check if the text is a valid URL.**
|
|
28
|
-
* @param text Text node to convert.
|
|
29
|
-
* @returns Anchor element.
|
|
30
|
-
*/
|
|
31
|
-
declare const convertTextToAnchorElement: (text: Text) => AnchorElement;
|
|
32
|
-
/**
|
|
33
|
-
* Check if the file exists.
|
|
34
|
-
* @param filePath Path to check.
|
|
35
|
-
* @returns `true` if the file exists, `false` otherwise.
|
|
36
|
-
*/
|
|
37
|
-
declare const checkFileExists: (filePath: string) => Promise<boolean>;
|
|
38
|
-
interface DownloadImageOptions {
|
|
39
|
-
/**
|
|
40
|
-
* URL to download image.
|
|
41
|
-
*/
|
|
42
|
-
url: string;
|
|
43
|
-
/**
|
|
44
|
-
* Directory to save the downloaded image.
|
|
45
|
-
*/
|
|
46
|
-
directly: string;
|
|
47
|
-
/**
|
|
48
|
-
* User agent to use for fetching the image.
|
|
49
|
-
*/
|
|
50
|
-
userAgent: string;
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Download image from given URL.
|
|
54
|
-
* @param options Options to download image.
|
|
55
|
-
* @returns Path to the downloaded image.
|
|
56
|
-
*/
|
|
57
|
-
declare const downloadImage: (options: DownloadImageOptions) => Promise<string | null>;
|
|
58
|
-
export { AnchorElement, isTextNode, isAnchorElement, isValidURL, convertTextToAnchorElement, checkFileExists, downloadImage };
|