-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
89 lines (81 loc) · 2.58 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
import { unfurl } from 'unfurl.js'
const fs = require('fs')
const path = require('path')
import { selectPossibleLinkNodes } from './selectPossibleLinkNodes'
import logResults from './logResults.js'
import { tranformsLinkNodeToUnfurledNode } from './transformLinkToUnfurledNode'
import { MetadataInterface } from './interfaces'
import { Node } from 'unist'
module.exports = async (
{
markdownAST,
markdownNode,
cache,
reporter,
}: { markdownAST: any; markdownNode: any; cache: any; reporter: any },
rawOptions: any
) => {
try {
const options = rawOptions
let processedUrlsJSON = {}
if (fs.existsSync(options.processedUrlsFile)) {
const processedUrlsFile = fs.readFileSync(options.processedUrlsFile)
processedUrlsJSON = JSON.parse(processedUrlsFile)
} else {
fs.mkdirSync(path.dirname(options.processedUrlsFile), { recursive: true })
fs.writeFileSync(options.processedUrlsFile, '{}')
}
const nodes = selectPossibleLinkNodes(markdownAST)
if (nodes.length > 0) {
const results = await Promise.all(
nodes.map((node: any) => processNode(node, options, processedUrlsJSON))
)
fs.writeFileSync(
options.processedUrlsFile,
JSON.stringify(processedUrlsJSON, null, 2)
)
logResults(results, markdownNode, reporter)
}
} catch (error) {
reporter.error(`gatsby-remark-link-unfurl: Error processing links`, error)
}
}
// For each node this is the process
const processNode = async (
node: any,
options: any,
processedUrl: { [key: string]: MetadataInterface }
): Promise<Node> => {
try {
const metaData = await unfurl(node.url)
if (!processedUrl[node.url]) {
processedUrl[node.url] = {
title:
metaData.twitter_card?.title ??
metaData.open_graph?.title ??
metaData.title,
description:
metaData.twitter_card?.description ??
metaData.open_graph?.description ??
metaData.description,
url: metaData.twitter_card?.url ?? metaData.open_graph?.url,
video: metaData.open_graph?.videos?.[0] || undefined,
// audio: ,
image:
metaData.twitter_card?.images?.[0] ||
metaData.open_graph?.images?.[0] ||
undefined,
logo: metaData.favicon,
site:
metaData.oEmbed?.provider_name ||
metaData.open_graph?.site_name ||
metaData.twitter_card?.site ||
undefined,
}
}
return tranformsLinkNodeToUnfurledNode(node, processedUrl[node.url])
} catch (error: any) {
error.url = node.url
return error
}
}