@chrisburnell/eleventy-cache-webmentions 1.0.0-beta.2 → 1.0.0-beta.5

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.
@@ -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: 2000,
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,30 @@ const getWebmentions = async (options, url, allowedTypes = {}) => {
144
159
  return []
145
160
  }
146
161
 
147
- const results = webmentions[url]
148
- // filter webmentions by allowed response post types
149
- .filter((entry) => {
150
- return typeof allowedTypes === "object" && Object.keys(allowedTypes).length ? allowedTypes.includes(getType(entry)) : true
151
- })
152
- // sanitize content of webmentions against HTML limit
153
- .map((entry) => {
154
- if (!("content" in entry) || !("data" in entry)) {
155
- return entry
156
- }
157
- const html = getContent(entry)
158
- if (html && html.length > options.maximumHtmlLength) {
159
- entry.content = `${options.maximumHtmlText} <a href="${getSource(entry)}">${getSource(entry)}</a>`
160
- } else if (Object.keys(options.allowedHTML).length) {
161
- entry.content = sanitizeHTML(html, options.allowedHTML)
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
- })
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) {
173
+ entry.content = sanitizeHTML(html, options.allowedHTML)
174
+ if (html.length > options.maximumHtmlLength) {
175
+ entry.content = `${options.maximumHtmlText} <a href="${getSource(entry)}">${getSource(entry)}</a>`
176
+ }
177
+ }
171
178
 
172
- return results
179
+ return entry
180
+ })
181
+ // sort by published
182
+ .sort((a, b) => {
183
+ return epoch(getPublished(a)) - epoch(getPublished(b))
184
+ })
185
+ )
173
186
  }
174
187
 
175
188
  const getWebmentionsFilter = async (options, url, allowedTypes, callback) => {
@@ -184,18 +197,6 @@ const getWebmentionsFilter = async (options, url, allowedTypes, callback) => {
184
197
  module.exports = (eleventyConfig, options = {}) => {
185
198
  options = Object.assign(defaults, options)
186
199
 
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
200
  if (eleventyConfig) {
200
201
  // Global Data
201
202
  eleventyConfig.addGlobalData("webmentions", filteredWebmentions(options))
@@ -209,7 +210,6 @@ module.exports = (eleventyConfig, options = {}) => {
209
210
 
210
211
  return {
211
212
  defaults: defaults,
212
- webmentions: filteredWebmentions(options),
213
213
  fetchWebmentions: fetchWebmentions,
214
214
  filteredWebmentions: filteredWebmentions,
215
215
  getWebmentions: getWebmentions,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chrisburnell/eleventy-cache-webmentions",
3
- "version": "1.0.0-beta.2",
3
+ "version": "1.0.0-beta.5",
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.6.5",
22
- "sanitize-html": "^2.5.2"
21
+ "node-fetch": "3.2.10",
22
+ "sanitize-html": "^2.7.1"
23
23
  },
24
24
  "keywords": [
25
25
  "eleventy",