-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
178 lines (152 loc) · 4.14 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { consola } from "consola";
import he from "he";
import type { PhrasingContent, Root, Text } from "mdast";
import { ofetch } from "ofetch";
import ogs from "open-graph-scraper-lite";
import { parseURL } from "ufo";
import type { Plugin } from "unified";
import { visit } from "unist-util-visit";
const LOG_PREFIX = "[remark-link-to-card]";
type RemarkLinkToCardOptions = {
/**
* Timeout for the fetch request in milliseconds.
*
* @default 5000
*/
timeout?: number;
/**
* Prefix for the class names.
*
* @default 'markdown-link-card'
*/
classPrefix?: string;
};
const isValidURL = (text: string): boolean => {
try {
const url = new URL(text);
return url.protocol === "http:" || url.protocol === "https:";
} catch (error) {
return false;
}
};
const RemarkLinkToCard: Plugin<RemarkLinkToCardOptions[], Root> = (
options = {},
) => {
const { classPrefix = "markdown-link-card", timeout = 5000 } = options;
return async (tree) => {
const promises: (() => Promise<void>)[] = [];
visit(tree, "paragraph", (node, _, parent) => {
// Check if the parent is a root node
if (parent?.type !== "root") {
return;
}
// Check if the paragraph has only one child
if (node.children.length !== 1) {
return;
}
const linkOrTextNode = node.children?.at(0);
// Check if the child is a link or text node
if (linkOrTextNode?.type !== "link" && linkOrTextNode?.type !== "text") {
return;
}
let isValid = false;
let textNode: Text | PhrasingContent | undefined;
let url: string | undefined;
switch (linkOrTextNode.type) {
case "link":
textNode = linkOrTextNode.children?.at(0);
isValid = isValidURL(linkOrTextNode.url);
url = linkOrTextNode.url;
break;
case "text":
textNode = linkOrTextNode;
isValid = isValidURL(linkOrTextNode.value);
url = linkOrTextNode.value;
break;
}
if (textNode?.type !== "text") {
return;
}
// Check if the URL is valid
if (!isValid) {
return;
}
if (url === undefined) {
return;
}
let title = textNode.value ?? url;
const { host } = parseURL(url);
promises.push(async () => {
let description = "";
let ogImageUrl: string | undefined;
try {
const raw = await ofetch.raw<string>(url, {
timeout,
});
const contentType = raw.headers.get("content-type");
// Only process HTML content
if (contentType?.indexOf("text/html") === -1) {
throw new Error("Content type is not HTML");
}
const html = raw._data;
if (html === undefined || html === "") {
throw new Error("HTML content is empty");
}
const ogData = await ogs({ html });
const { result } = ogData;
if (result.success === false) {
throw new Error(result.errorDetails?.message);
}
title = he.encode(result.ogTitle ?? title);
description = he.encode(result.ogDescription ?? description);
ogImageUrl = result.ogImage?.at(0)?.url;
} catch (error) {
const message =
error instanceof Error ? error.message : "An error occurred";
consola.error(LOG_PREFIX, message);
}
node.data = {
hName: "a",
hProperties: {
class: `${classPrefix}-link`,
href: url,
target: "_blank",
rel: "noopener noreferrer nofollow",
},
};
const thumbnail = ogImageUrl
? `
<div class="${classPrefix}-thumbnail">
<img src="${ogImageUrl}" alt="${title}" class="${classPrefix}-thumbnail-image">
</div>
`
: "";
node.children = [
{
type: "html",
value: `
<div class="${classPrefix}-main">
<div class="${classPrefix}-title">${title}</div>
<div class="${classPrefix}-description">
${description}
</div>
<div class="${classPrefix}-meta">
<img class="${classPrefix}-favicon" src="https://www.google.com/s2/favicons?domain=${host}" alt="${host} favicon image" width="14" height="14">
${host}
</div>
</div>
${thumbnail}
`,
},
];
});
});
try {
await Promise.all(promises.map((promise) => promise()));
} catch (error) {
consola.error(LOG_PREFIX, error);
}
return tree;
};
};
export default RemarkLinkToCard;