@chrisburnell/eleventy-cache-webmentions 0.1.12 → 0.2.0

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.
@@ -3,11 +3,6 @@ const sanitizeHTML = require("sanitize-html")
3
3
  const uniqBy = require("lodash/uniqBy")
4
4
  const { AssetCache } = require("@11ty/eleventy-fetch")
5
5
 
6
- // Load .env variables with dotenv
7
- require("dotenv").config()
8
-
9
- const TOKEN = process.env.WEBMENTION_IO_TOKEN
10
-
11
6
  const absoluteURL = (url, domain) => {
12
7
  try {
13
8
  return new URL(url, domain).toString()
@@ -45,7 +40,7 @@ const epoch = (value) => {
45
40
  module.exports = (eleventyConfig, options = {}) => {
46
41
  options = Object.assign(
47
42
  {
48
- key: "webmentions",
43
+ uniqueKey: "webmentions",
49
44
  allowedHTML: {
50
45
  allowedTags: ["b", "i", "em", "strong", "a"],
51
46
  allowedAttributes: {
@@ -60,14 +55,10 @@ module.exports = (eleventyConfig, options = {}) => {
60
55
  )
61
56
 
62
57
  const fetchWebmentions = async () => {
63
- let asset = new AssetCache(options.uniqueKey || options.key)
58
+ let asset = new AssetCache(options.uniqueKey)
64
59
  asset.ensureDir()
65
60
 
66
- let webmentions = {
67
- type: "feed",
68
- name: "Webmentions",
69
- children: [],
70
- }
61
+ let webmentions = []
71
62
 
72
63
  // If there is a cached file at all, grab its contents now
73
64
  if (asset.isCacheValid("9001y")) {
@@ -75,25 +66,27 @@ module.exports = (eleventyConfig, options = {}) => {
75
66
  }
76
67
 
77
68
  // If there is a cached file but it is outside of expiry, fetch fresh
78
- // results since the most recent
69
+ // results since the most recent webmention
79
70
  if (!asset.isCacheValid(options.duration)) {
80
- const since = webmentions.children.length ? webmentions.children[0]["wm-received"] : false
81
- const url = `https://webmention.io/api/mentions.jf2?domain=${hostname(options.domain)}&token=${TOKEN}&per-page=9001${since ? `&since=${since}` : ``}`
71
+ const since = webmentions.length ? webmentions[0]["wm-received"] || webmentions[0]["published"] : false
72
+ const url = `${options.feed}${since && options.feed.includes("https://webmention.io") ? `&since=${since}` : ""}`
82
73
  await fetch(url)
83
74
  .then(async (response) => {
84
75
  if (response.ok) {
85
76
  const feed = await response.json()
86
- if (feed.children.length) {
87
- console.log(`[${hostname(options.domain)}] ${feed.children.length} new Webmentions fetched into cache.`)
77
+ if (feed[options.key].length) {
78
+ webmentions = uniqBy([...feed[options.key], ...webmentions], (item) => {
79
+ return item["wm-source"] || item["source"]
80
+ })
81
+ console.log(`[${hostname(options.domain)}] ${feed[options.key].length} new Webmentions fetched into cache.`)
88
82
  }
89
- webmentions.children = [...feed.children, ...webmentions.children]
90
83
  await asset.save(webmentions, "json")
91
84
  return webmentions
92
85
  }
93
86
  return Promise.reject(response)
94
87
  })
95
88
  .catch((error) => {
96
- console.log(`[${hostname(options.domain)}] Something went wrong with your request to webmention.io!`, error)
89
+ console.log(`[${hostname(options.domain)}] Something went wrong with your request to ${hostname(options.feed)}!`, error)
97
90
  })
98
91
  }
99
92
 
@@ -104,10 +97,10 @@ module.exports = (eleventyConfig, options = {}) => {
104
97
  const rawWebmentions = await fetchWebmentions()
105
98
  let webmentions = {}
106
99
 
107
- // Sort Webmentions into groups by `wm-target`
108
- rawWebmentions.children.forEach((webmention) => {
100
+ // Sort Webmentions into groups by `wm-target` || `target`
101
+ rawWebmentions.forEach((webmention) => {
109
102
  // Get the target of the Webmention and fix it up
110
- let url = baseUrl(fixUrl(webmention["wm-target"].replace(/\/?$/, "/"), options.urlReplacements))
103
+ let url = baseUrl(fixUrl((webmention["wm-target"] || webmention["target"]).replace(/\/?$/, "/"), options.urlReplacements))
111
104
 
112
105
  if (!webmentions[url]) {
113
106
  webmentions[url] = []
@@ -137,7 +130,7 @@ module.exports = (eleventyConfig, options = {}) => {
137
130
  const results = webmentions[url]
138
131
  // filter webmentions by allowed response post types
139
132
  .filter((entry) => {
140
- return typeof allowedTypes === "object" && Object.keys(allowedTypes).length ? allowedTypes.includes(entry["wm-property"]) : true
133
+ return typeof allowedTypes === "object" && Object.keys(allowedTypes).length ? allowedTypes.includes(entry["wm-property"] || entry["type"]) : true
141
134
  })
142
135
  // remove webmentions without an author name
143
136
  .filter((entry) => {
@@ -151,7 +144,7 @@ module.exports = (eleventyConfig, options = {}) => {
151
144
  }
152
145
  const { html, text } = entry.content
153
146
  if (html && html.length > options.maximumHtmlLength) {
154
- entry.content.value = `${options.maximumHtmlText} <a href="${entry["wm-source"]}">${entry["wm-source"]}</a>`
147
+ entry.content.value = `${options.maximumHtmlText} <a href="${entry["wm-source"] || entry["source"]}">${entry["wm-source"] || entry["source"]}</a>`
155
148
  } else if (Object.keys(options.allowedHTML).length) {
156
149
  entry.content.value = sanitizeHTML(html || text, options.allowedHTML)
157
150
  } else {
@@ -159,9 +152,9 @@ module.exports = (eleventyConfig, options = {}) => {
159
152
  }
160
153
  return entry
161
154
  })
162
- // sort by published/wm-received
155
+ // sort by `wm-received` || `published`
163
156
  .sort((a, b) => {
164
- return epoch(a.published || a["wm-received"]) - epoch(b.published || b["wm-received"])
157
+ return epoch(a["wm-received"] || a.published) - epoch(b["wm-received"] || b.published)
165
158
  })
166
159
 
167
160
  return results
@@ -178,14 +171,24 @@ module.exports = (eleventyConfig, options = {}) => {
178
171
 
179
172
  if (eleventyConfig && options) {
180
173
  if (!options.domain) {
181
- throw new Error("domain is a required option to be passed when adding the plugin to your eleventyConfig using addPlugin.")
174
+ 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.")
175
+ }
176
+
177
+ if (!options.feed) {
178
+ 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.")
179
+ }
180
+
181
+ if (!options.key) {
182
+ 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.")
182
183
  }
183
184
 
184
- // Liquid/Nunjucks Filters
185
+ // Liquid Filter
185
186
  eleventyConfig.addLiquidFilter("getWebmentions", getWebmentionsFilter)
187
+
188
+ // Nunjucks Filter
186
189
  eleventyConfig.addNunjucksAsyncFilter("getWebmentions", getWebmentionsFilter)
187
190
 
188
- // Eleventy Data
191
+ // Global Data
189
192
  eleventyConfig.addGlobalData("webmentions", filteredWebmentions)
190
193
  } else {
191
194
  // JavaScript
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@chrisburnell/eleventy-cache-webmentions",
3
- "version": "0.1.12",
4
- "description": "Cache webmentions from webmention.io using eleventy-fetch.",
3
+ "version": "0.2.0",
4
+ "description": "Fetch and cache webmentions using eleventy-fetch.",
5
5
  "main": "eleventy-cache-webmentions.js",
6
6
  "author": "Chris Burnell <me@chrisburnell.com>",
7
7
  "license": "MIT",
@@ -17,7 +17,6 @@
17
17
  },
18
18
  "dependencies": {
19
19
  "@11ty/eleventy-fetch": "^3.0.0",
20
- "dotenv": "^10.0.0",
21
20
  "lodash": "^4.17.21",
22
21
  "node-fetch": "2.6.5",
23
22
  "sanitize-html": "^2.5.2"