@chrisburnell/eleventy-cache-webmentions 2.0.1 → 2.0.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/README.md +4 -4
- package/eleventy-cache-webmentions.js +72 -47
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -297,11 +297,11 @@ If you would rather get Webmentions for a given page directly from a Layout/Incl
|
|
|
297
297
|
Instead of getting all the Webmentions for a given page, you may want to grab only certain types of Webmentions. This is useful if you want to display different types of Webmentions separately, e.g.:
|
|
298
298
|
|
|
299
299
|
```twig
|
|
300
|
-
{% set bookmarks = webmentions |
|
|
301
|
-
{% set likes = webmentions |
|
|
302
|
-
{% set reposts = webmentions |
|
|
300
|
+
{% set bookmarks = webmentions | getWebmentionsByTypes(['bookmark-of']) %}
|
|
301
|
+
{% set likes = webmentions | getWebmentionsByTypes(['like-of']) %}
|
|
302
|
+
{% set reposts = webmentions | getWebmentionsByTypes(['repost-of']) %}
|
|
303
303
|
|
|
304
|
-
{% set replies = webmentions |
|
|
304
|
+
{% set replies = webmentions | getWebmentionsByTypes(['mention-of', 'in-reply-to']) %}
|
|
305
305
|
```
|
|
306
306
|
|
|
307
307
|
## Get all Webmentions at once
|
|
@@ -41,6 +41,10 @@ const getPublished = (webmention) => {
|
|
|
41
41
|
return webmention?.["data"]?.["published"] || webmention["published"] || webmention["wm-received"] || webmention["verified_date"]
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
+
const getReceived = (webmention) => {
|
|
45
|
+
return webmention["wm-received"] || webmention["verified_date"] || webmention?.["data"]?.["published"] || webmention["published"]
|
|
46
|
+
}
|
|
47
|
+
|
|
44
48
|
const getContent = (webmention) => {
|
|
45
49
|
return webmention?.["contentSanitized"] || webmention?.["content"]?.["html"] || webmention?.["content"]?.["value"] || webmention?.["content"] || webmention?.["data"]?.["content"] || ""
|
|
46
50
|
}
|
|
@@ -81,15 +85,15 @@ const defaults = {
|
|
|
81
85
|
|
|
82
86
|
const fetchWebmentions = async (options) => {
|
|
83
87
|
if (!options.domain) {
|
|
84
|
-
throw new Error("domain is a required field when attempting to retrieve Webmentions. See https://www.npmjs.com/package/@chrisburnell/eleventy-cache-webmentions#installation for more information.")
|
|
88
|
+
throw new Error("`domain` is a required field when attempting to retrieve Webmentions. See https://www.npmjs.com/package/@chrisburnell/eleventy-cache-webmentions#installation for more information.")
|
|
85
89
|
}
|
|
86
90
|
|
|
87
91
|
if (!options.feed) {
|
|
88
|
-
throw new Error("feed is a required field when attempting to retrieve Webmentions. See https://www.npmjs.com/package/@chrisburnell/eleventy-cache-webmentions#installation for more information.")
|
|
92
|
+
throw new Error("`feed` is a required field when attempting to retrieve Webmentions. See https://www.npmjs.com/package/@chrisburnell/eleventy-cache-webmentions#installation for more information.")
|
|
89
93
|
}
|
|
90
94
|
|
|
91
95
|
if (!options.key) {
|
|
92
|
-
throw new Error("key is a required field when attempting to retrieve Webmentions. See https://www.npmjs.com/package/@chrisburnell/eleventy-cache-webmentions#installation for more information.")
|
|
96
|
+
throw new Error("`key` is a required field when attempting to retrieve Webmentions. See https://www.npmjs.com/package/@chrisburnell/eleventy-cache-webmentions#installation for more information.")
|
|
93
97
|
}
|
|
94
98
|
|
|
95
99
|
let asset = new AssetCache(options.uniqueKey, options.directory)
|
|
@@ -102,59 +106,70 @@ const fetchWebmentions = async (options) => {
|
|
|
102
106
|
webmentions = await asset.getCachedValue()
|
|
103
107
|
}
|
|
104
108
|
|
|
109
|
+
// Get the number of cached Webmentions for diffing against fetched
|
|
110
|
+
// Webmentions later
|
|
111
|
+
const webmentionsCachedLength = webmentions.length
|
|
112
|
+
|
|
105
113
|
// If there is a cached file but it is outside of expiry, fetch fresh
|
|
106
114
|
// results since the most recent Webmention
|
|
107
115
|
if (!asset.isCacheValid(options.duration)) {
|
|
108
|
-
// Get the
|
|
109
|
-
const since = webmentions.length ?
|
|
116
|
+
// Get the received date of the most recent Webmention, if it exists
|
|
117
|
+
const since = webmentions.length ? getReceived(webmentions[0]) : false
|
|
110
118
|
// Build the URL for the fetch request
|
|
111
119
|
const url = `${options.feed}${since ? `${options.feed.includes("?") ? "&" : "?"}since=${since}` : ""}`
|
|
112
|
-
await fetch(url)
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
if (options.blocklist.length) {
|
|
124
|
-
webmentions = webmentions.filter((webmention) => {
|
|
125
|
-
let sourceUrl = getSource(webmention)
|
|
126
|
-
for (let url of options.blocklist) {
|
|
127
|
-
if (sourceUrl.includes(url.replace(/\/?$/, "/"))) {
|
|
128
|
-
return false
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
return true
|
|
120
|
+
await fetch(url)
|
|
121
|
+
.then(async (response) => {
|
|
122
|
+
if (response.ok) {
|
|
123
|
+
const feed = await response.json()
|
|
124
|
+
|
|
125
|
+
if (feed[options.key].length) {
|
|
126
|
+
// Combine newly-fetched Webmentions with cached Webmentions
|
|
127
|
+
webmentions = feed[options.key].concat(webmentions)
|
|
128
|
+
// Remove duplicates by source URL
|
|
129
|
+
webmentions = uniqBy([...feed[options.key], ...webmentions], (webmention) => {
|
|
130
|
+
return getSource(webmention)
|
|
132
131
|
})
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
132
|
+
// Process the blocklist, if it has any entries
|
|
133
|
+
if (options.blocklist.length) {
|
|
134
|
+
webmentions = webmentions.filter((webmention) => {
|
|
135
|
+
let sourceUrl = getSource(webmention)
|
|
136
|
+
for (let url of options.blocklist) {
|
|
137
|
+
if (sourceUrl.includes(url.replace(/\/?$/, "/"))) {
|
|
138
|
+
return false
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return true
|
|
142
|
+
})
|
|
143
|
+
}
|
|
144
|
+
// Process the allowlist, if it has any entries
|
|
145
|
+
if (options.allowlist.length) {
|
|
146
|
+
webmentions = webmentions.filter((webmention) => {
|
|
147
|
+
let sourceUrl = getSource(webmention)
|
|
148
|
+
for (let url of options.allowlist) {
|
|
149
|
+
if (sourceUrl.includes(url.replace(/\/?$/, "/"))) {
|
|
150
|
+
return true
|
|
151
|
+
}
|
|
141
152
|
}
|
|
142
|
-
|
|
143
|
-
|
|
153
|
+
return false
|
|
154
|
+
})
|
|
155
|
+
}
|
|
156
|
+
// Sort webmentions by received date for getting most recent Webmention on subsequent requests
|
|
157
|
+
webmentions = webmentions.sort((a, b) => {
|
|
158
|
+
return epoch(getReceived(b)) - epoch(getReceived(a))
|
|
144
159
|
})
|
|
160
|
+
// Add a console message with the number of fetched and processed Webmentions, if any
|
|
161
|
+
if (webmentionsCachedLength < webmentions.length) {
|
|
162
|
+
console.log(`[${hostname(options.domain)}] ${webmentions.length - webmentionsCachedLength} new Webmentions fetched into cache.`)
|
|
163
|
+
}
|
|
145
164
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}
|
|
165
|
+
await asset.save(webmentions, "json")
|
|
166
|
+
return webmentions
|
|
149
167
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
.catch((error) => {
|
|
156
|
-
console.log(`[${hostname(options.domain)}] Something went wrong with your request to ${hostname(options.feed)}!`, error)
|
|
157
|
-
})
|
|
168
|
+
return Promise.reject(response)
|
|
169
|
+
})
|
|
170
|
+
.catch((error) => {
|
|
171
|
+
console.log(`[${hostname(options.domain)}] Something went wrong with your request to ${hostname(options.feed)}!`, error)
|
|
172
|
+
})
|
|
158
173
|
}
|
|
159
174
|
|
|
160
175
|
return webmentions
|
|
@@ -238,17 +253,19 @@ const eleventyCacheWebmentions = (eleventyConfig, options = {}) => {
|
|
|
238
253
|
|
|
239
254
|
// Liquid Filters
|
|
240
255
|
eleventyConfig.addLiquidFilter("getWebmentions", getWebmentionsFilter)
|
|
256
|
+
eleventyConfig.addLiquidFilter("getWebmentionsByTypes", getByTypes)
|
|
241
257
|
eleventyConfig.addLiquidFilter("getWebmentionPublished", getPublished)
|
|
258
|
+
eleventyConfig.addLiquidFilter("getWebmentionReceived", getReceived)
|
|
242
259
|
eleventyConfig.addLiquidFilter("getWebmentionContent", getContent)
|
|
243
260
|
eleventyConfig.addLiquidFilter("getWebmentionSource", getSource)
|
|
244
261
|
eleventyConfig.addLiquidFilter("getWebmentionTarget", getTarget)
|
|
245
262
|
eleventyConfig.addLiquidFilter("getWebmentionType", getType)
|
|
246
|
-
eleventyConfig.addLiquidFilter("getWebmentionsByTypes", getByTypes)
|
|
247
263
|
|
|
248
264
|
// Nunjucks Filters
|
|
249
265
|
eleventyConfig.addNunjucksAsyncFilter("getWebmentions", getWebmentionsFilter)
|
|
250
266
|
eleventyConfig.addNunjucksFilter("getWebmentionsByTypes", getByTypes)
|
|
251
267
|
eleventyConfig.addNunjucksFilter("getWebmentionPublished", getPublished)
|
|
268
|
+
eleventyConfig.addNunjucksFilter("getWebmentionReceived", getReceived)
|
|
252
269
|
eleventyConfig.addNunjucksFilter("getWebmentionContent", getContent)
|
|
253
270
|
eleventyConfig.addNunjucksFilter("getWebmentionSource", getSource)
|
|
254
271
|
eleventyConfig.addNunjucksFilter("getWebmentionTarget", getTarget)
|
|
@@ -262,8 +279,16 @@ module.exports.webmentionsByUrl = filteredWebmentions
|
|
|
262
279
|
module.exports.fetchWebmentions = fetchWebmentions
|
|
263
280
|
module.exports.getWebmentions = getWebmentions
|
|
264
281
|
module.exports.getByTypes = getByTypes
|
|
282
|
+
module.exports.getWebmentionsByTypes = getByTypes
|
|
265
283
|
module.exports.getPublished = getPublished
|
|
284
|
+
module.exports.getWebmentionPublished = getPublished
|
|
285
|
+
module.exports.getReceived = getReceived
|
|
286
|
+
module.exports.getWebmentionReceived = getReceived
|
|
266
287
|
module.exports.getContent = getContent
|
|
288
|
+
module.exports.getWebmentionContent = getContent
|
|
267
289
|
module.exports.getSource = getSource
|
|
290
|
+
module.exports.getWebmentionSource = getSource
|
|
268
291
|
module.exports.getTarget = getTarget
|
|
292
|
+
module.exports.getWebmentionTarget = getTarget
|
|
269
293
|
module.exports.getType = getType
|
|
294
|
+
module.exports.getWebmentionType = getType
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chrisburnell/eleventy-cache-webmentions",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.3",
|
|
4
4
|
"description": "Cache webmentions using eleventy-fetch and make them available to use in collections, layouts, pages, etc. in Eleventy.",
|
|
5
5
|
"author": "Chris Burnell <me@chrisburnell.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -20,13 +20,13 @@
|
|
|
20
20
|
"webmention"
|
|
21
21
|
],
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"eslint": "^
|
|
23
|
+
"eslint": "^9.2.0"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@11ty/eleventy-fetch": "^4.0.
|
|
26
|
+
"@11ty/eleventy-fetch": "^4.0.1",
|
|
27
27
|
"lodash": "^4.17.21",
|
|
28
|
-
"node-fetch": "^
|
|
29
|
-
"sanitize-html": "^2.
|
|
28
|
+
"node-fetch": "^3.3.2",
|
|
29
|
+
"sanitize-html": "^2.13.0"
|
|
30
30
|
},
|
|
31
31
|
"main": "eleventy-cache-webmentions.js",
|
|
32
32
|
"scripts": {
|