@chrisburnell/eleventy-cache-webmentions 0.0.2 → 0.1.2

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # eleventy-cache-assets
2
2
 
3
- > Work-in-progress! ⚠️ Cache webmentions using eleventy-cache-assets and make them available to use in collections, templates, pages, etc.
3
+ > Cache webmentions using eleventy-cache-assets and make them available to use in collections, templates, pages, etc.
4
4
 
5
5
  ## Installation
6
6
 
@@ -52,8 +52,9 @@ module.exports = (config, options = {}) => {
52
52
  a: ["href"],
53
53
  },
54
54
  },
55
- urlReplacements: [],
55
+ urlReplacements: {},
56
56
  maximumHtmlLength: 2000,
57
+ maximumHtmlText: "mentioned this in",
57
58
  },
58
59
  options
59
60
  )
@@ -78,18 +79,22 @@ module.exports = (config, options = {}) => {
78
79
  if (!asset.isCacheValid(options.duration)) {
79
80
  const since = asset._cache.getKey(options.key) ? asset._cache.getKey(options.key).cachedAt : false
80
81
  const url = `https://webmention.io/api/mentions.jf2?domain=${hostname(options.domain)}&token=${TOKEN}&per-page=9001${since ? `&since=${since}` : ``}`
81
- const response = await fetch(url)
82
- if (response.ok) {
83
- const feed = await response.json()
84
- if (feed.children.length) {
85
- console.log(`[${hostname(options.domain)}] ${feed.children.length} new Webmentions fetched into cache.`)
86
- }
87
- webmentions.children = [...feed.children, ...webmentions.children].sort((a, b) => {
88
- return epoch(b.published || b["wm-received"]) - epoch(a.published || a["wm-received"])
82
+ await fetch(url)
83
+ .then(async (response) => {
84
+ if (response.ok) {
85
+ const feed = await response.json()
86
+ if (feed.children.length) {
87
+ console.log(`[${hostname(options.domain)}] ${feed.children.length} new Webmentions fetched into cache.`)
88
+ }
89
+ webmentions.children = [...feed.children, ...webmentions.children]
90
+ await asset.save(webmentions, "json")
91
+ return webmentions
92
+ }
93
+ return Promise.reject(response)
94
+ })
95
+ .catch((error) => {
96
+ console.log("[${hostname(options.domain)}] Something went wrong with your request to webmention.io!", error)
89
97
  })
90
- await asset.save(webmentions, "json")
91
- return webmentions
92
- }
93
98
  }
94
99
 
95
100
  return webmentions
@@ -121,7 +126,7 @@ module.exports = (config, options = {}) => {
121
126
  return webmentions
122
127
  }
123
128
 
124
- const getWebmentions = async (url, allowedTypes) => {
129
+ const getWebmentions = async (url, allowedTypes = {}) => {
125
130
  const webmentions = await filteredWebmentions()
126
131
  url = absoluteURL(url, options.domain)
127
132
 
@@ -130,9 +135,9 @@ module.exports = (config, options = {}) => {
130
135
  }
131
136
 
132
137
  const results = webmentions[url]
133
- // filter webmentions by allowedTypes only if passed
138
+ // filter webmentions by allowed response post types
134
139
  .filter((entry) => {
135
- return Array.isArray(allowedTypes) ? allowedTypes.includes(entry["wm-property"]) : true
140
+ return typeof allowedTypes === "object" && Object.keys(allowedTypes).length ? allowedTypes.includes(entry["wm-property"]) : true
136
141
  })
137
142
  // remove webmentions without an author name
138
143
  .filter((entry) => {
@@ -146,7 +151,7 @@ module.exports = (config, options = {}) => {
146
151
  }
147
152
  const { html, text } = entry.content
148
153
  if (html && html.length > options.maximumHtmlLength) {
149
- entry.content.value = `mentioned this in <a href="${entry["wm-source"]}">${entry["wm-source"]}</a>`
154
+ entry.content.value = `${options.maximumHtmlText} <a href="${entry["wm-source"]}">${entry["wm-source"]}</a>`
150
155
  } else {
151
156
  entry.content.value = sanitizeHTML(html || text, options.allowedHTML)
152
157
  }
@@ -161,12 +166,12 @@ module.exports = (config, options = {}) => {
161
166
  }
162
167
 
163
168
  const getWebmentionsFilter = async (url, allowedTypes, callback) => {
164
- const webmentions = await getWebmentions(url, allowedTypes)
165
- if (typeof callback === "function") {
166
- callback(null, webmentions)
167
- } else {
168
- return webmentions
169
+ if (typeof callback !== "function") {
170
+ callback = allowedTypes
171
+ allowedTypes = {}
169
172
  }
173
+ const webmentions = await getWebmentions(url, allowedTypes)
174
+ callback(null, webmentions)
170
175
  }
171
176
 
172
177
  if (config && options) {
@@ -177,6 +182,6 @@ module.exports = (config, options = {}) => {
177
182
  config.addLiquidFilter("getWebmentions", getWebmentionsFilter)
178
183
  config.addNunjucksAsyncFilter("getWebmentions", getWebmentionsFilter)
179
184
  } else {
180
- return filteredWebmentions()
185
+ return filteredWebmentions
181
186
  }
182
187
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chrisburnell/eleventy-cache-webmentions",
3
- "version": "0.0.2",
3
+ "version": "0.1.2",
4
4
  "description": "Cache webmentions from webmention.io using eleventy-cache-assets.",
5
5
  "main": "eleventy-cache-webmentions.js",
6
6
  "author": "Chris Burnell <me@chrisburnell.com>",