@chrisburnell/eleventy-cache-webmentions 2.0.2 → 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.
@@ -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,17 +106,22 @@ 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 published date of the most recent Webmention, if it exists
109
- const since = webmentions.length ? getPublished(webmentions[0]) : false
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
120
  await fetch(url)
113
121
  .then(async (response) => {
114
122
  if (response.ok) {
115
123
  const feed = await response.json()
124
+
116
125
  if (feed[options.key].length) {
117
126
  // Combine newly-fetched Webmentions with cached Webmentions
118
127
  webmentions = feed[options.key].concat(webmentions)
@@ -144,8 +153,13 @@ const fetchWebmentions = async (options) => {
144
153
  return false
145
154
  })
146
155
  }
147
- if (webmentions.length) {
148
- console.log(`[${hostname(options.domain)}] ${webmentions.length} new Webmentions fetched into cache.`)
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))
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.`)
149
163
  }
150
164
  }
151
165
  await asset.save(webmentions, "json")
@@ -241,6 +255,7 @@ const eleventyCacheWebmentions = (eleventyConfig, options = {}) => {
241
255
  eleventyConfig.addLiquidFilter("getWebmentions", getWebmentionsFilter)
242
256
  eleventyConfig.addLiquidFilter("getWebmentionsByTypes", getByTypes)
243
257
  eleventyConfig.addLiquidFilter("getWebmentionPublished", getPublished)
258
+ eleventyConfig.addLiquidFilter("getWebmentionReceived", getReceived)
244
259
  eleventyConfig.addLiquidFilter("getWebmentionContent", getContent)
245
260
  eleventyConfig.addLiquidFilter("getWebmentionSource", getSource)
246
261
  eleventyConfig.addLiquidFilter("getWebmentionTarget", getTarget)
@@ -250,6 +265,7 @@ const eleventyCacheWebmentions = (eleventyConfig, options = {}) => {
250
265
  eleventyConfig.addNunjucksAsyncFilter("getWebmentions", getWebmentionsFilter)
251
266
  eleventyConfig.addNunjucksFilter("getWebmentionsByTypes", getByTypes)
252
267
  eleventyConfig.addNunjucksFilter("getWebmentionPublished", getPublished)
268
+ eleventyConfig.addNunjucksFilter("getWebmentionReceived", getReceived)
253
269
  eleventyConfig.addNunjucksFilter("getWebmentionContent", getContent)
254
270
  eleventyConfig.addNunjucksFilter("getWebmentionSource", getSource)
255
271
  eleventyConfig.addNunjucksFilter("getWebmentionTarget", getTarget)
@@ -266,6 +282,8 @@ module.exports.getByTypes = getByTypes
266
282
  module.exports.getWebmentionsByTypes = getByTypes
267
283
  module.exports.getPublished = getPublished
268
284
  module.exports.getWebmentionPublished = getPublished
285
+ module.exports.getReceived = getReceived
286
+ module.exports.getWebmentionReceived = getReceived
269
287
  module.exports.getContent = getContent
270
288
  module.exports.getWebmentionContent = getContent
271
289
  module.exports.getSource = getSource
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chrisburnell/eleventy-cache-webmentions",
3
- "version": "2.0.2",
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": "^8.56.0"
23
+ "eslint": "^9.2.0"
24
24
  },
25
25
  "dependencies": {
26
- "@11ty/eleventy-fetch": "^4.0.0",
26
+ "@11ty/eleventy-fetch": "^4.0.1",
27
27
  "lodash": "^4.17.21",
28
- "node-fetch": "^2.6.7",
29
- "sanitize-html": "^2.11.0"
28
+ "node-fetch": "^3.3.2",
29
+ "sanitize-html": "^2.13.0"
30
30
  },
31
31
  "main": "eleventy-cache-webmentions.js",
32
32
  "scripts": {