@chrisburnell/eleventy-cache-webmentions 1.0.0-beta.1 → 1.0.0-beta.4
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/eleventy-cache-webmentions.js +47 -41
- package/package.json +3 -3
|
@@ -2,7 +2,6 @@ const fetch = require("node-fetch")
|
|
|
2
2
|
const sanitizeHTML = require("sanitize-html")
|
|
3
3
|
const uniqBy = require("lodash/uniqBy")
|
|
4
4
|
const { AssetCache } = require("@11ty/eleventy-fetch")
|
|
5
|
-
const { defaultsDeep } = require("lodash")
|
|
6
5
|
|
|
7
6
|
const absoluteURL = (url, domain) => {
|
|
8
7
|
try {
|
|
@@ -43,7 +42,7 @@ const getPublished = (webmention) => {
|
|
|
43
42
|
}
|
|
44
43
|
|
|
45
44
|
const getContent = (webmention) => {
|
|
46
|
-
return webmention?.["content"]["html"] || webmention["content"] || webmention?.["data"]["content"]
|
|
45
|
+
return webmention?.["content"]?.["html"] || webmention?.["content"] || webmention?.["data"]?.["content"] || ""
|
|
47
46
|
}
|
|
48
47
|
|
|
49
48
|
const getSource = (webmention) => {
|
|
@@ -67,11 +66,27 @@ const defaults = {
|
|
|
67
66
|
},
|
|
68
67
|
},
|
|
69
68
|
urlReplacements: {},
|
|
70
|
-
maximumHtmlLength:
|
|
69
|
+
maximumHtmlLength: 1000,
|
|
71
70
|
maximumHtmlText: "mentioned this in",
|
|
72
71
|
}
|
|
73
72
|
|
|
74
73
|
const fetchWebmentions = async (options) => {
|
|
74
|
+
if (!options.domain) {
|
|
75
|
+
throw new Error("domain is a required field when attempting to retrieve Webmentions. See https://chrisburnell.com/eleventy-cache-webmentions/#installation for more information.")
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (!options.feed) {
|
|
79
|
+
throw new Error("feed is a required field when attempting to retrieve Webmentions. See https://chrisburnell.com/eleventy-cache-webmentions/#installation for more information.")
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (!options.key) {
|
|
83
|
+
throw new Error("key is a required field when attempting to retrieve Webmentions. See https://chrisburnell.com/eleventy-cache-webmentions/#installation for more information.")
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (!options.uniqueKey) {
|
|
87
|
+
throw new Error("uniqueKey is a required field when attempting to retrieve Webmentions. See https://chrisburnell.com/eleventy-cache-webmentions/#installation for more information.")
|
|
88
|
+
}
|
|
89
|
+
|
|
75
90
|
let asset = new AssetCache(options.uniqueKey)
|
|
76
91
|
asset.ensureDir()
|
|
77
92
|
|
|
@@ -144,32 +159,33 @@ const getWebmentions = async (options, url, allowedTypes = {}) => {
|
|
|
144
159
|
return []
|
|
145
160
|
}
|
|
146
161
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
162
|
+
return (
|
|
163
|
+
webmentions[url]
|
|
164
|
+
// filter webmentions by allowed response post types
|
|
165
|
+
.filter((entry) => {
|
|
166
|
+
return typeof allowedTypes === "object" && Object.keys(allowedTypes).length ? allowedTypes.includes(getType(entry)) : true
|
|
167
|
+
})
|
|
168
|
+
// sanitize content of webmentions against HTML limit
|
|
169
|
+
.map((entry) => {
|
|
170
|
+
const html = getContent(entry)
|
|
171
|
+
|
|
172
|
+
if (html.length > options.maximumHtmlLength) {
|
|
173
|
+
console.log(entry.data.url, "too long")
|
|
174
|
+
entry.content = `${options.maximumHtmlText} <a href="${getSource(entry)}">${getSource(entry)}</a>`
|
|
175
|
+
} else if (html.length && Object.keys(options.allowedHTML).length) {
|
|
176
|
+
// console.log("sanitize")
|
|
177
|
+
entry.content = sanitizeHTML(html, options.allowedHTML)
|
|
178
|
+
} else {
|
|
179
|
+
// console.log("default")
|
|
180
|
+
entry.content = html
|
|
181
|
+
}
|
|
155
182
|
return entry
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
} else {
|
|
163
|
-
entry.content = html
|
|
164
|
-
}
|
|
165
|
-
return entry
|
|
166
|
-
})
|
|
167
|
-
// sort by published
|
|
168
|
-
.sort((a, b) => {
|
|
169
|
-
return epoch(getPublished(a)) - epoch(getPublished(b))
|
|
170
|
-
})
|
|
171
|
-
|
|
172
|
-
return results
|
|
183
|
+
})
|
|
184
|
+
// sort by published
|
|
185
|
+
.sort((a, b) => {
|
|
186
|
+
return epoch(getPublished(a)) - epoch(getPublished(b))
|
|
187
|
+
})
|
|
188
|
+
)
|
|
173
189
|
}
|
|
174
190
|
|
|
175
191
|
const getWebmentionsFilter = async (options, url, allowedTypes, callback) => {
|
|
@@ -184,18 +200,6 @@ const getWebmentionsFilter = async (options, url, allowedTypes, callback) => {
|
|
|
184
200
|
module.exports = (eleventyConfig, options = {}) => {
|
|
185
201
|
options = Object.assign(defaults, options)
|
|
186
202
|
|
|
187
|
-
if (!options.domain) {
|
|
188
|
-
throw new Error("domain is a required field when adding the plugin to your eleventyConfig using addPlugin. See https://chrisburnell.com/eleventy-cache-webmentions/#installation for more information.")
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
if (!options.feed) {
|
|
192
|
-
throw new Error("feed is a required field when adding the plugin to your eleventyConfig using addPlugin. See https://chrisburnell.com/eleventy-cache-webmentions/#installation for more information.")
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
if (!options.key) {
|
|
196
|
-
throw new Error("key is a required field when adding the plugin to your eleventyConfig using addPlugin. See https://chrisburnell.com/eleventy-cache-webmentions/#installation for more information.")
|
|
197
|
-
}
|
|
198
|
-
|
|
199
203
|
if (eleventyConfig) {
|
|
200
204
|
// Global Data
|
|
201
205
|
eleventyConfig.addGlobalData("webmentions", filteredWebmentions(options))
|
|
@@ -208,7 +212,9 @@ module.exports = (eleventyConfig, options = {}) => {
|
|
|
208
212
|
}
|
|
209
213
|
|
|
210
214
|
return {
|
|
211
|
-
|
|
215
|
+
defaults: defaults,
|
|
216
|
+
fetchWebmentions: fetchWebmentions,
|
|
217
|
+
filteredWebmentions: filteredWebmentions,
|
|
212
218
|
getWebmentions: getWebmentions,
|
|
213
219
|
}
|
|
214
220
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chrisburnell/eleventy-cache-webmentions",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.4",
|
|
4
4
|
"description": "Fetch and cache webmentions using eleventy-fetch.",
|
|
5
5
|
"main": "eleventy-cache-webmentions.js",
|
|
6
6
|
"author": "Chris Burnell <me@chrisburnell.com>",
|
|
@@ -18,8 +18,8 @@
|
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"@11ty/eleventy-fetch": "^3.0.0",
|
|
20
20
|
"lodash": "^4.17.21",
|
|
21
|
-
"node-fetch": "2.
|
|
22
|
-
"sanitize-html": "^2.
|
|
21
|
+
"node-fetch": "3.2.10",
|
|
22
|
+
"sanitize-html": "^2.7.1"
|
|
23
23
|
},
|
|
24
24
|
"keywords": [
|
|
25
25
|
"eleventy",
|