@chrisburnell/eleventy-cache-webmentions 0.1.0 → 0.1.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/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
 
@@ -41,7 +41,7 @@ const epoch = (value) => {
41
41
  return new Date(value).getTime()
42
42
  }
43
43
 
44
- module.exports = (config, options = {}) => {
44
+ module.exports = (, options = {}) => {
45
45
  options = Object.assign(
46
46
  {
47
47
  duration: "23h",
@@ -79,18 +79,22 @@ module.exports = (config, options = {}) => {
79
79
  if (!asset.isCacheValid(options.duration)) {
80
80
  const since = asset._cache.getKey(options.key) ? asset._cache.getKey(options.key).cachedAt : false
81
81
  const url = `https://webmention.io/api/mentions.jf2?domain=${hostname(options.domain)}&token=${TOKEN}&per-page=9001${since ? `&since=${since}` : ``}`
82
- const response = await fetch(url)
83
- if (response.ok) {
84
- const feed = await response.json()
85
- if (feed.children.length) {
86
- console.log(`[${hostname(options.domain)}] ${feed.children.length} new Webmentions fetched into cache.`)
87
- }
88
- webmentions.children = [...feed.children, ...webmentions.children].sort((a, b) => {
89
- 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)
90
97
  })
91
- await asset.save(webmentions, "json")
92
- return webmentions
93
- }
94
98
  }
95
99
 
96
100
  return webmentions
@@ -122,7 +126,7 @@ module.exports = (config, options = {}) => {
122
126
  return webmentions
123
127
  }
124
128
 
125
- const getWebmentions = async (url, allowedTypes) => {
129
+ const getWebmentions = async (url, allowedTypes = {}) => {
126
130
  const webmentions = await filteredWebmentions()
127
131
  url = absoluteURL(url, options.domain)
128
132
 
@@ -131,9 +135,9 @@ module.exports = (config, options = {}) => {
131
135
  }
132
136
 
133
137
  const results = webmentions[url]
134
- // filter webmentions by allowedTypes only if passed
138
+ // filter webmentions by allowed response post types
135
139
  .filter((entry) => {
136
- 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
137
141
  })
138
142
  // remove webmentions without an author name
139
143
  .filter((entry) => {
@@ -148,8 +152,10 @@ module.exports = (config, options = {}) => {
148
152
  const { html, text } = entry.content
149
153
  if (html && html.length > options.maximumHtmlLength) {
150
154
  entry.content.value = `${options.maximumHtmlText} <a href="${entry["wm-source"]}">${entry["wm-source"]}</a>`
151
- } else {
155
+ } else if (Object.keys(options.allowedHTML).length) {
152
156
  entry.content.value = sanitizeHTML(html || text, options.allowedHTML)
157
+ } else {
158
+ entry.content.value = html || text
153
159
  }
154
160
  return entry
155
161
  })
@@ -162,22 +168,22 @@ module.exports = (config, options = {}) => {
162
168
  }
163
169
 
164
170
  const getWebmentionsFilter = async (url, allowedTypes, callback) => {
165
- const webmentions = await getWebmentions(url, allowedTypes)
166
- if (typeof callback === "function") {
167
- callback(null, webmentions)
168
- } else {
169
- return webmentions
171
+ if (typeof callback !== "function") {
172
+ callback = allowedTypes
173
+ allowedTypes = {}
170
174
  }
175
+ const webmentions = await getWebmentions(url, allowedTypes)
176
+ callback(null, webmentions)
171
177
  }
172
178
 
173
- if (config && options) {
179
+ if (eleventyConfig && options) {
174
180
  if (!options.domain) {
175
- throw new Error("domain is a required option to be passed when adding the plugin to your config using addPlugin.")
181
+ throw new Error("domain is a required option to be passed when adding the plugin to your eleventyConfig using addPlugin.")
176
182
  }
177
183
 
178
- config.addLiquidFilter("getWebmentions", getWebmentionsFilter)
179
- config.addNunjucksAsyncFilter("getWebmentions", getWebmentionsFilter)
184
+ eleventyConfig.addLiquidFilter("getWebmentions", getWebmentionsFilter)
185
+ eleventyConfig.addNunjucksAsyncFilter("getWebmentions", getWebmentionsFilter)
180
186
  } else {
181
- return filteredWebmentions()
187
+ return filteredWebmentions
182
188
  }
183
189
  }
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "@chrisburnell/eleventy-cache-webmentions",
3
- "version": "0.1.0",
3
+ "version": "0.1.4",
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>",
7
7
  "license": "MIT",
8
8
  "repository": {
9
9
  "type": "git",
10
- "url": "git@github.com:chrisburnell/chrisburnell.com.git"
10
+ "url": "git@github.com:chrisburnell/eleventy-cache-webmentions.git"
11
11
  },
12
12
  "bugs": {
13
- "url": "https://github.com/chrisburnell/chrisburnell.com/issues"
13
+ "url": "https://github.com/chrisburnell/eleventy-cache-webmentions/issues"
14
14
  },
15
15
  "engines": {
16
16
  "node": ">=10"