@chrisburnell/eleventy-cache-webmentions 2.1.2 → 2.1.3-beta.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 +3 -17
- package/eleventy-cache-webmentions.js +276 -183
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -205,9 +205,6 @@ webmentions.forEach((webmention) => {
|
|
|
205
205
|
<summary>Liquid / Nunjucks</summary>
|
|
206
206
|
|
|
207
207
|
```twig
|
|
208
|
-
{# get Array of Webmentions for a given URL #}
|
|
209
|
-
{% set webmentions = ("https://example.com" + page.url) | getWebmentions %}
|
|
210
|
-
|
|
211
208
|
{# filter Webmentions by their response type #}
|
|
212
209
|
{{ set responses = webmentions | getWebmentionsByTypes(['mention-of', 'in-reply-to']) }}
|
|
213
210
|
|
|
@@ -227,7 +224,7 @@ webmentions.forEach((webmention) => {
|
|
|
227
224
|
|
|
228
225
|
</details>
|
|
229
226
|
|
|
230
|
-
|
|
227
|
+
### Attach Webmentions to Pages using Directory Data
|
|
231
228
|
|
|
232
229
|
Using [Eleventy’s Data Cascade](https://www.11ty.dev/docs/data-cascade/), you can attach Webmentions to each page by using [Directory Specific Data Files](https://www.11ty.dev/docs/data-template-dir/).
|
|
233
230
|
|
|
@@ -281,18 +278,7 @@ module.exports = (eleventyConfig) => {
|
|
|
281
278
|
}
|
|
282
279
|
```
|
|
283
280
|
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
If you would rather get Webmentions for a given page directly from a Layout/Include/Page itself, you can do so using the Filter, `getWebmentions`:
|
|
287
|
-
|
|
288
|
-
```twig
|
|
289
|
-
{% set webmentions = ("https://example.com" + page.url) | getWebmentions %}
|
|
290
|
-
{% for webmention in webmentions %}
|
|
291
|
-
...
|
|
292
|
-
{% endfor %}
|
|
293
|
-
```
|
|
294
|
-
|
|
295
|
-
## Get specific types of Webmentions
|
|
281
|
+
### Get specific types of Webmentions
|
|
296
282
|
|
|
297
283
|
Instead of getting all the Webmentions for a given page, you may want to grab only certain types of Webmentions. This is useful if you want to display different types of Webmentions separately, e.g.:
|
|
298
284
|
|
|
@@ -304,7 +290,7 @@ Instead of getting all the Webmentions for a given page, you may want to grab on
|
|
|
304
290
|
{% set replies = webmentions | getWebmentionsByTypes(['mention-of', 'in-reply-to']) %}
|
|
305
291
|
```
|
|
306
292
|
|
|
307
|
-
|
|
293
|
+
### Get all Webmentions at once
|
|
308
294
|
|
|
309
295
|
If you need it, the plugin also makes available an Object containing your cached Webmentions organised in key:value pairs, where each key is a full URL on your website and its value is an Array of Webmentions sent to that URL:
|
|
310
296
|
|
|
@@ -1,71 +1,108 @@
|
|
|
1
|
-
const fetch = require("node-fetch")
|
|
2
|
-
const sanitizeHTML = require("sanitize-html")
|
|
3
|
-
const uniqBy = require("lodash/uniqBy")
|
|
4
|
-
const { AssetCache } = require("@11ty/eleventy-fetch")
|
|
1
|
+
const fetch = require("node-fetch");
|
|
2
|
+
const sanitizeHTML = require("sanitize-html");
|
|
3
|
+
const uniqBy = require("lodash/uniqBy");
|
|
4
|
+
const { AssetCache } = require("@11ty/eleventy-fetch");
|
|
5
5
|
|
|
6
6
|
const absoluteURL = (url, domain) => {
|
|
7
7
|
try {
|
|
8
|
-
return new URL(url, domain).toString()
|
|
8
|
+
return new URL(url, domain).toString();
|
|
9
9
|
} catch (e) {
|
|
10
|
-
console.log(
|
|
11
|
-
|
|
10
|
+
console.log(
|
|
11
|
+
`Trying to convert ${url} to be an absolute url with base ${domain} and failed.`,
|
|
12
|
+
);
|
|
13
|
+
return url;
|
|
12
14
|
}
|
|
13
|
-
}
|
|
15
|
+
};
|
|
14
16
|
|
|
15
17
|
const baseUrl = (url) => {
|
|
16
|
-
let hashSplit = url.split("#")
|
|
17
|
-
let queryparamSplit = hashSplit[0].split("?")
|
|
18
|
-
return queryparamSplit[0]
|
|
19
|
-
}
|
|
18
|
+
let hashSplit = url.split("#");
|
|
19
|
+
let queryparamSplit = hashSplit[0].split("?");
|
|
20
|
+
return queryparamSplit[0];
|
|
21
|
+
};
|
|
20
22
|
|
|
21
23
|
const fixUrl = (url, urlReplacements) => {
|
|
22
|
-
return Object.entries(urlReplacements).reduce(
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
24
|
+
return Object.entries(urlReplacements).reduce(
|
|
25
|
+
(accumulator, [key, value]) => {
|
|
26
|
+
const regex = new RegExp(key, "g");
|
|
27
|
+
return accumulator.replace(regex, value);
|
|
28
|
+
},
|
|
29
|
+
url,
|
|
30
|
+
);
|
|
31
|
+
};
|
|
27
32
|
|
|
28
33
|
const hostname = (value) => {
|
|
29
34
|
if (typeof value === "string" && value.includes("//")) {
|
|
30
|
-
const urlObject = new URL(value)
|
|
31
|
-
return urlObject.hostname
|
|
35
|
+
const urlObject = new URL(value);
|
|
36
|
+
return urlObject.hostname;
|
|
32
37
|
}
|
|
33
|
-
return value
|
|
34
|
-
}
|
|
38
|
+
return value;
|
|
39
|
+
};
|
|
35
40
|
|
|
36
41
|
const epoch = (value) => {
|
|
37
|
-
return new Date(value).getTime()
|
|
38
|
-
}
|
|
42
|
+
return new Date(value).getTime();
|
|
43
|
+
};
|
|
39
44
|
|
|
40
45
|
const getPublished = (webmention) => {
|
|
41
|
-
return
|
|
42
|
-
|
|
46
|
+
return (
|
|
47
|
+
webmention?.["data"]?.["published"] ||
|
|
48
|
+
webmention["published"] ||
|
|
49
|
+
webmention["wm-received"] ||
|
|
50
|
+
webmention["verified_date"]
|
|
51
|
+
);
|
|
52
|
+
};
|
|
43
53
|
|
|
44
54
|
const getReceived = (webmention) => {
|
|
45
|
-
return
|
|
46
|
-
|
|
55
|
+
return (
|
|
56
|
+
webmention["wm-received"] ||
|
|
57
|
+
webmention["verified_date"] ||
|
|
58
|
+
webmention?.["data"]?.["published"] ||
|
|
59
|
+
webmention["published"]
|
|
60
|
+
);
|
|
61
|
+
};
|
|
47
62
|
|
|
48
63
|
const getContent = (webmention) => {
|
|
49
|
-
return
|
|
50
|
-
|
|
64
|
+
return (
|
|
65
|
+
webmention?.["contentSanitized"] ||
|
|
66
|
+
webmention?.["content"]?.["html"] ||
|
|
67
|
+
webmention?.["content"]?.["value"] ||
|
|
68
|
+
webmention?.["content"] ||
|
|
69
|
+
webmention?.["data"]?.["content"] ||
|
|
70
|
+
""
|
|
71
|
+
);
|
|
72
|
+
};
|
|
51
73
|
|
|
52
74
|
const getSource = (webmention) => {
|
|
53
|
-
return
|
|
54
|
-
|
|
75
|
+
return (
|
|
76
|
+
webmention?.["data"]?.["url"] ||
|
|
77
|
+
webmention["url"] ||
|
|
78
|
+
webmention["wm-source"] ||
|
|
79
|
+
webmention["source"]
|
|
80
|
+
);
|
|
81
|
+
};
|
|
55
82
|
|
|
56
83
|
const getTarget = (webmention) => {
|
|
57
|
-
return webmention["wm-target"] || webmention["target"]
|
|
58
|
-
}
|
|
84
|
+
return webmention["wm-target"] || webmention["target"];
|
|
85
|
+
};
|
|
59
86
|
|
|
60
87
|
const getType = (webmention) => {
|
|
61
|
-
return
|
|
62
|
-
|
|
88
|
+
return (
|
|
89
|
+
webmention["wm-property"] ||
|
|
90
|
+
webmention?.["activity"]?.["type"] ||
|
|
91
|
+
webmention["type"]
|
|
92
|
+
);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const getByType = (webmentions, allowedType) => {
|
|
96
|
+
return webmentions.filter((webmention) => {
|
|
97
|
+
return allowedTypes === getType(webmention);
|
|
98
|
+
});
|
|
99
|
+
};
|
|
63
100
|
|
|
64
101
|
const getByTypes = (webmentions, allowedTypes) => {
|
|
65
102
|
return webmentions.filter((webmention) => {
|
|
66
|
-
return allowedTypes.includes(getType(webmention))
|
|
67
|
-
})
|
|
68
|
-
}
|
|
103
|
+
return allowedTypes.includes(getType(webmention));
|
|
104
|
+
});
|
|
105
|
+
};
|
|
69
106
|
|
|
70
107
|
const defaults = {
|
|
71
108
|
duration: "1d",
|
|
@@ -81,259 +118,315 @@ const defaults = {
|
|
|
81
118
|
urlReplacements: {},
|
|
82
119
|
maximumHtmlLength: 1000,
|
|
83
120
|
maximumHtmlText: "mentioned this in",
|
|
84
|
-
}
|
|
121
|
+
};
|
|
85
122
|
|
|
86
123
|
const performFetch = async (options, webmentions, url) => {
|
|
87
124
|
return await fetch(url)
|
|
88
125
|
.then(async (response) => {
|
|
89
|
-
if (response.ok) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
126
|
+
if (!response.ok) {
|
|
127
|
+
return Promise.reject(response);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const feed = await response.json();
|
|
131
|
+
|
|
132
|
+
if (!options.key in feed) {
|
|
133
|
+
console.log(
|
|
134
|
+
`[${hostname(options.domain)}] ${
|
|
135
|
+
options.key
|
|
136
|
+
} was not found as a key in the response from ${hostname(
|
|
137
|
+
options.feed,
|
|
138
|
+
)}!`,
|
|
139
|
+
);
|
|
140
|
+
return Promise.reject(response);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Combine newly-fetched Webmentions with cached Webmentions
|
|
144
|
+
webmentions = feed[options.key].concat(webmentions);
|
|
145
|
+
// Remove duplicates by source URL
|
|
146
|
+
webmentions = uniqBy(
|
|
147
|
+
[...feed[options.key], ...webmentions],
|
|
148
|
+
(webmention) => {
|
|
149
|
+
return getSource(webmention);
|
|
150
|
+
},
|
|
151
|
+
);
|
|
152
|
+
// Process the blocklist, if it has any entries
|
|
153
|
+
if (options.blocklist.length) {
|
|
154
|
+
webmentions = webmentions.filter((webmention) => {
|
|
155
|
+
let sourceUrl = getSource(webmention);
|
|
156
|
+
for (let url of options.blocklist) {
|
|
157
|
+
if (sourceUrl.includes(url.replace(/\/?$/, "/"))) {
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
109
160
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
return
|
|
120
|
-
}
|
|
161
|
+
return true;
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
// Process the allowlist, if it has any entries
|
|
165
|
+
if (options.allowlist.length) {
|
|
166
|
+
webmentions = webmentions.filter((webmention) => {
|
|
167
|
+
let sourceUrl = getSource(webmention);
|
|
168
|
+
for (let url of options.allowlist) {
|
|
169
|
+
if (sourceUrl.includes(url.replace(/\/?$/, "/"))) {
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
121
172
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
return epoch(getReceived(b)) - epoch(getReceived(a))
|
|
125
|
-
})
|
|
126
|
-
}
|
|
127
|
-
return {
|
|
128
|
-
found: feed[options.key].length,
|
|
129
|
-
filtered: webmentions,
|
|
130
|
-
}
|
|
173
|
+
return false;
|
|
174
|
+
});
|
|
131
175
|
}
|
|
132
|
-
|
|
176
|
+
// Sort webmentions by received date for getting most recent Webmention on subsequent requests
|
|
177
|
+
webmentions = webmentions.sort((a, b) => {
|
|
178
|
+
return epoch(getReceived(b)) - epoch(getReceived(a));
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
return {
|
|
182
|
+
found: feed[options.key].length,
|
|
183
|
+
filtered: webmentions,
|
|
184
|
+
};
|
|
133
185
|
})
|
|
134
186
|
.catch((error) => {
|
|
135
|
-
console.log(
|
|
136
|
-
|
|
137
|
-
|
|
187
|
+
console.log(
|
|
188
|
+
`[${hostname(
|
|
189
|
+
options.domain,
|
|
190
|
+
)}] Something went wrong with your request to ${hostname(
|
|
191
|
+
options.feed,
|
|
192
|
+
)}!`,
|
|
193
|
+
error,
|
|
194
|
+
);
|
|
195
|
+
});
|
|
196
|
+
};
|
|
138
197
|
|
|
139
198
|
const fetchWebmentions = async (options) => {
|
|
140
199
|
if (!options.domain) {
|
|
141
|
-
throw new Error(
|
|
200
|
+
throw new Error(
|
|
201
|
+
"`domain` is a required field when attempting to retrieve Webmentions. See https://www.npmjs.com/package/@chrisburnell/eleventy-cache-webmentions#installation for more information.",
|
|
202
|
+
);
|
|
142
203
|
}
|
|
143
204
|
|
|
144
205
|
if (!options.feed) {
|
|
145
|
-
throw new Error(
|
|
206
|
+
throw new Error(
|
|
207
|
+
"`feed` is a required field when attempting to retrieve Webmentions. See https://www.npmjs.com/package/@chrisburnell/eleventy-cache-webmentions#installation for more information.",
|
|
208
|
+
);
|
|
146
209
|
}
|
|
147
210
|
|
|
148
211
|
if (!options.key) {
|
|
149
|
-
throw new Error(
|
|
212
|
+
throw new Error(
|
|
213
|
+
"`key` is a required field when attempting to retrieve Webmentions. See https://www.npmjs.com/package/@chrisburnell/eleventy-cache-webmentions#installation for more information.",
|
|
214
|
+
);
|
|
150
215
|
}
|
|
151
216
|
|
|
152
|
-
let asset = new AssetCache(options.uniqueKey, options.directory)
|
|
153
|
-
asset.ensureDir()
|
|
217
|
+
let asset = new AssetCache(options.uniqueKey, options.directory);
|
|
218
|
+
asset.ensureDir();
|
|
154
219
|
|
|
155
|
-
let webmentions = []
|
|
220
|
+
let webmentions = [];
|
|
156
221
|
|
|
157
222
|
// If there is a cached file at all, grab its contents now
|
|
158
223
|
if (asset.isCacheValid("9001y")) {
|
|
159
|
-
webmentions = await asset.getCachedValue()
|
|
224
|
+
webmentions = await asset.getCachedValue();
|
|
160
225
|
}
|
|
161
226
|
|
|
162
227
|
// Get the number of cached Webmentions for diffing against fetched
|
|
163
228
|
// Webmentions later
|
|
164
|
-
const webmentionsCachedLength = webmentions.length
|
|
229
|
+
const webmentionsCachedLength = webmentions.length;
|
|
165
230
|
|
|
166
231
|
// If there is a cached file but it is outside of expiry, fetch fresh
|
|
167
232
|
// results since the most recent Webmention
|
|
168
|
-
if (!asset.isCacheValid(
|
|
233
|
+
if (!asset.isCacheValid("1s")) {
|
|
169
234
|
// Get the received date of the most recent Webmention, if it exists
|
|
170
|
-
const since = webmentions.length ? getReceived(webmentions[0]) : false
|
|
235
|
+
const since = webmentions.length ? getReceived(webmentions[0]) : false;
|
|
171
236
|
// Build the URL for the fetch request
|
|
172
|
-
const url = `${options.feed}${
|
|
237
|
+
const url = `${options.feed}${
|
|
238
|
+
since
|
|
239
|
+
? `${options.feed.includes("?") ? "&" : "?"}since=${since}`
|
|
240
|
+
: ""
|
|
241
|
+
}`;
|
|
173
242
|
|
|
174
243
|
// If using webmention.io, loop through pages until no results found
|
|
175
244
|
if (url.includes("https://webmention.io")) {
|
|
176
245
|
// Start on page 0, to increment per subsequent request
|
|
177
|
-
let page = 0
|
|
246
|
+
let page = 0;
|
|
178
247
|
// Loop until a break condition is hit
|
|
179
248
|
while (true) {
|
|
180
|
-
const perPage =
|
|
181
|
-
|
|
182
|
-
const
|
|
249
|
+
const perPage =
|
|
250
|
+
Number(new URL(url).searchParams.get("per-page")) || 1000;
|
|
251
|
+
const urlPaginated = url + `&per-page=${perPage}&page=${page}`;
|
|
252
|
+
const fetched = await performFetch(
|
|
253
|
+
options,
|
|
254
|
+
webmentions,
|
|
255
|
+
urlPaginated,
|
|
256
|
+
);
|
|
183
257
|
|
|
184
258
|
// An error occurred during fetching paged results → break
|
|
185
259
|
if (!fetched && !fetched.found && !fetched.filtered) {
|
|
186
|
-
break
|
|
260
|
+
break;
|
|
187
261
|
}
|
|
188
262
|
|
|
189
263
|
// Page has no Webmentions → break
|
|
190
264
|
if (fetched.found === 0) {
|
|
191
|
-
break
|
|
265
|
+
break;
|
|
192
266
|
}
|
|
193
267
|
|
|
194
|
-
webmentions = fetched.filtered
|
|
268
|
+
webmentions = fetched.filtered;
|
|
195
269
|
|
|
196
270
|
// If there are less Webmentions found than should be in each
|
|
197
271
|
// page → break
|
|
198
272
|
if (fetched.found < perPage) {
|
|
199
|
-
break
|
|
273
|
+
break;
|
|
200
274
|
}
|
|
201
275
|
|
|
202
276
|
// Increment page
|
|
203
|
-
page += 1
|
|
277
|
+
page += 1;
|
|
204
278
|
// Throttle next request
|
|
205
|
-
await new Promise(resolve => setTimeout(resolve, 1000))
|
|
279
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
206
280
|
}
|
|
207
281
|
} else {
|
|
208
|
-
const fetched = await performFetch(options, webmentions, url)
|
|
209
|
-
webmentions = fetched.filtered
|
|
282
|
+
const fetched = await performFetch(options, webmentions, url);
|
|
283
|
+
webmentions = fetched.filtered;
|
|
210
284
|
}
|
|
211
285
|
|
|
212
|
-
await asset.save(webmentions, "json")
|
|
286
|
+
await asset.save(webmentions, "json");
|
|
213
287
|
|
|
214
288
|
// Add a console message with the number of fetched and processed Webmentions, if any
|
|
215
289
|
if (webmentionsCachedLength < webmentions.length) {
|
|
216
|
-
console.log(
|
|
290
|
+
console.log(
|
|
291
|
+
`[${hostname(options.domain)}] ${
|
|
292
|
+
webmentions.length - webmentionsCachedLength
|
|
293
|
+
} new Webmentions fetched into cache.`,
|
|
294
|
+
);
|
|
217
295
|
}
|
|
218
296
|
}
|
|
219
297
|
|
|
220
|
-
return webmentions
|
|
221
|
-
}
|
|
298
|
+
return webmentions;
|
|
299
|
+
};
|
|
222
300
|
|
|
223
|
-
let filtered = {}
|
|
301
|
+
let filtered = {};
|
|
224
302
|
const filteredWebmentions = async (options) => {
|
|
225
303
|
if (Object.entries(filtered).length) {
|
|
226
|
-
return filtered
|
|
304
|
+
return filtered;
|
|
227
305
|
}
|
|
228
306
|
|
|
229
|
-
let rawWebmentions = await fetchWebmentions(options)
|
|
307
|
+
let rawWebmentions = await fetchWebmentions(options);
|
|
230
308
|
|
|
231
309
|
// Fix local URLs based on urlReplacements and sort Webmentions into groups
|
|
232
310
|
// by target base URL
|
|
233
311
|
rawWebmentions.forEach((webmention) => {
|
|
234
|
-
let url = baseUrl(
|
|
312
|
+
let url = baseUrl(
|
|
313
|
+
fixUrl(
|
|
314
|
+
getTarget(webmention).replace(/\/?$/, "/"),
|
|
315
|
+
options.urlReplacements,
|
|
316
|
+
),
|
|
317
|
+
);
|
|
235
318
|
|
|
236
319
|
if (!filtered[url]) {
|
|
237
|
-
filtered[url] = []
|
|
320
|
+
filtered[url] = [];
|
|
238
321
|
}
|
|
239
322
|
|
|
240
|
-
filtered[url].push(webmention)
|
|
241
|
-
})
|
|
323
|
+
filtered[url].push(webmention);
|
|
324
|
+
});
|
|
242
325
|
|
|
243
|
-
return filtered
|
|
244
|
-
}
|
|
326
|
+
return filtered;
|
|
327
|
+
};
|
|
245
328
|
|
|
246
329
|
const getWebmentions = async (options, url, allowedTypes = {}) => {
|
|
247
|
-
const webmentions = await filteredWebmentions(options)
|
|
248
|
-
url = absoluteURL(url, options.domain)
|
|
330
|
+
const webmentions = await filteredWebmentions(options);
|
|
331
|
+
url = absoluteURL(url, options.domain);
|
|
249
332
|
|
|
250
333
|
if (!url || !webmentions || !webmentions[url]) {
|
|
251
|
-
return []
|
|
334
|
+
return [];
|
|
252
335
|
}
|
|
253
336
|
|
|
254
337
|
return (
|
|
255
338
|
webmentions[url]
|
|
256
339
|
// Filter webmentions by allowed response post types
|
|
257
340
|
.filter((entry) => {
|
|
258
|
-
return typeof allowedTypes === "object" &&
|
|
341
|
+
return typeof allowedTypes === "object" &&
|
|
342
|
+
Object.keys(allowedTypes).length
|
|
343
|
+
? allowedTypes.includes(getType(entry))
|
|
344
|
+
: true;
|
|
259
345
|
})
|
|
260
346
|
// Sanitize content of webmentions against HTML limit
|
|
261
347
|
.map((entry) => {
|
|
262
|
-
const html = getContent(entry)
|
|
348
|
+
const html = getContent(entry);
|
|
263
349
|
|
|
264
350
|
if (html.length) {
|
|
265
|
-
entry.contentSanitized = sanitizeHTML(
|
|
351
|
+
entry.contentSanitized = sanitizeHTML(
|
|
352
|
+
html,
|
|
353
|
+
options.allowedHTML,
|
|
354
|
+
);
|
|
266
355
|
if (html.length > options.maximumHtmlLength) {
|
|
267
|
-
entry.contentSanitized = `${
|
|
356
|
+
entry.contentSanitized = `${
|
|
357
|
+
options.maximumHtmlText
|
|
358
|
+
} <a href="${getSource(entry)}">${getSource(
|
|
359
|
+
entry,
|
|
360
|
+
)}</a>`;
|
|
268
361
|
}
|
|
269
362
|
}
|
|
270
363
|
|
|
271
|
-
return entry
|
|
364
|
+
return entry;
|
|
272
365
|
})
|
|
273
366
|
// Sort by published
|
|
274
367
|
.sort((a, b) => {
|
|
275
|
-
return epoch(getPublished(a)) - epoch(getPublished(b))
|
|
368
|
+
return epoch(getPublished(a)) - epoch(getPublished(b));
|
|
276
369
|
})
|
|
277
|
-
)
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
const getWebmentionsFilter = async (options, url, allowedTypes, callback) => {
|
|
281
|
-
if (typeof callback !== "function") {
|
|
282
|
-
callback = allowedTypes
|
|
283
|
-
allowedTypes = {}
|
|
284
|
-
}
|
|
285
|
-
const webmentions = await getWebmentions(options, url, allowedTypes)
|
|
286
|
-
callback(null, webmentions)
|
|
287
|
-
}
|
|
370
|
+
);
|
|
371
|
+
};
|
|
288
372
|
|
|
289
373
|
const eleventyCacheWebmentions = (eleventyConfig, options = {}) => {
|
|
290
|
-
options = Object.assign(defaults, options)
|
|
374
|
+
options = Object.assign(defaults, options);
|
|
291
375
|
|
|
292
376
|
// Global Data
|
|
293
|
-
eleventyConfig.addGlobalData("webmentionsDefaults", defaults)
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
377
|
+
eleventyConfig.addGlobalData("webmentionsDefaults", defaults);
|
|
378
|
+
eleventyConfig.addGlobalData("webmentionsOptions", options);
|
|
379
|
+
const filtered = async () => await filteredWebmentions(options);
|
|
380
|
+
eleventyConfig.addGlobalData("webmentionsByUrl", filtered);
|
|
381
|
+
const unfiltered = async () =>
|
|
382
|
+
await filteredWebmentions(options).then((filtered) =>
|
|
383
|
+
Object.values(filtered).reduce(
|
|
384
|
+
(array, webmentions) => [...array, ...webmentions],
|
|
385
|
+
[],
|
|
386
|
+
),
|
|
387
|
+
);
|
|
388
|
+
eleventyConfig.addGlobalData("webmentionsAll", unfiltered);
|
|
298
389
|
|
|
299
390
|
// Liquid Filters
|
|
300
|
-
eleventyConfig.addLiquidFilter("
|
|
301
|
-
eleventyConfig.addLiquidFilter("getWebmentionsByTypes", getByTypes)
|
|
302
|
-
eleventyConfig.addLiquidFilter("getWebmentionPublished", getPublished)
|
|
303
|
-
eleventyConfig.addLiquidFilter("getWebmentionReceived", getReceived)
|
|
304
|
-
eleventyConfig.addLiquidFilter("getWebmentionContent", getContent)
|
|
305
|
-
eleventyConfig.addLiquidFilter("getWebmentionSource", getSource)
|
|
306
|
-
eleventyConfig.addLiquidFilter("getWebmentionTarget", getTarget)
|
|
307
|
-
eleventyConfig.addLiquidFilter("getWebmentionType", getType)
|
|
391
|
+
eleventyConfig.addLiquidFilter("getWebmentionsByType", getByType);
|
|
392
|
+
eleventyConfig.addLiquidFilter("getWebmentionsByTypes", getByTypes);
|
|
393
|
+
eleventyConfig.addLiquidFilter("getWebmentionPublished", getPublished);
|
|
394
|
+
eleventyConfig.addLiquidFilter("getWebmentionReceived", getReceived);
|
|
395
|
+
eleventyConfig.addLiquidFilter("getWebmentionContent", getContent);
|
|
396
|
+
eleventyConfig.addLiquidFilter("getWebmentionSource", getSource);
|
|
397
|
+
eleventyConfig.addLiquidFilter("getWebmentionTarget", getTarget);
|
|
398
|
+
eleventyConfig.addLiquidFilter("getWebmentionType", getType);
|
|
308
399
|
|
|
309
400
|
// Nunjucks Filters
|
|
310
|
-
eleventyConfig.
|
|
311
|
-
eleventyConfig.addNunjucksFilter("getWebmentionsByTypes", getByTypes)
|
|
312
|
-
eleventyConfig.addNunjucksFilter("getWebmentionPublished", getPublished)
|
|
313
|
-
eleventyConfig.addNunjucksFilter("getWebmentionReceived", getReceived)
|
|
314
|
-
eleventyConfig.addNunjucksFilter("getWebmentionContent", getContent)
|
|
315
|
-
eleventyConfig.addNunjucksFilter("getWebmentionSource", getSource)
|
|
316
|
-
eleventyConfig.addNunjucksFilter("getWebmentionTarget", getTarget)
|
|
317
|
-
eleventyConfig.addNunjucksFilter("getWebmentionType", getType)
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
module.exports = eleventyCacheWebmentions
|
|
321
|
-
module.exports.defaults = defaults
|
|
322
|
-
module.exports.filteredWebmentions = filteredWebmentions
|
|
323
|
-
module.exports.webmentionsByUrl = filteredWebmentions
|
|
324
|
-
module.exports.fetchWebmentions = fetchWebmentions
|
|
325
|
-
module.exports.getWebmentions = getWebmentions
|
|
326
|
-
module.exports.
|
|
327
|
-
module.exports.
|
|
328
|
-
module.exports.
|
|
329
|
-
module.exports.
|
|
330
|
-
module.exports.
|
|
331
|
-
module.exports.
|
|
332
|
-
module.exports.
|
|
333
|
-
module.exports.
|
|
334
|
-
module.exports.
|
|
335
|
-
module.exports.
|
|
336
|
-
module.exports.
|
|
337
|
-
module.exports.
|
|
338
|
-
module.exports.
|
|
339
|
-
module.exports.
|
|
401
|
+
eleventyConfig.addNunjucksFilter("getWebmentionsByType", getByType);
|
|
402
|
+
eleventyConfig.addNunjucksFilter("getWebmentionsByTypes", getByTypes);
|
|
403
|
+
eleventyConfig.addNunjucksFilter("getWebmentionPublished", getPublished);
|
|
404
|
+
eleventyConfig.addNunjucksFilter("getWebmentionReceived", getReceived);
|
|
405
|
+
eleventyConfig.addNunjucksFilter("getWebmentionContent", getContent);
|
|
406
|
+
eleventyConfig.addNunjucksFilter("getWebmentionSource", getSource);
|
|
407
|
+
eleventyConfig.addNunjucksFilter("getWebmentionTarget", getTarget);
|
|
408
|
+
eleventyConfig.addNunjucksFilter("getWebmentionType", getType);
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
module.exports = eleventyCacheWebmentions;
|
|
412
|
+
module.exports.defaults = defaults;
|
|
413
|
+
module.exports.filteredWebmentions = filteredWebmentions;
|
|
414
|
+
module.exports.webmentionsByUrl = filteredWebmentions;
|
|
415
|
+
module.exports.fetchWebmentions = fetchWebmentions;
|
|
416
|
+
module.exports.getWebmentions = getWebmentions;
|
|
417
|
+
module.exports.getByType = getByType;
|
|
418
|
+
module.exports.getWebmentionsByType = getByType;
|
|
419
|
+
module.exports.getByTypes = getByTypes;
|
|
420
|
+
module.exports.getWebmentionsByTypes = getByTypes;
|
|
421
|
+
module.exports.getPublished = getPublished;
|
|
422
|
+
module.exports.getWebmentionPublished = getPublished;
|
|
423
|
+
module.exports.getReceived = getReceived;
|
|
424
|
+
module.exports.getWebmentionReceived = getReceived;
|
|
425
|
+
module.exports.getContent = getContent;
|
|
426
|
+
module.exports.getWebmentionContent = getContent;
|
|
427
|
+
module.exports.getSource = getSource;
|
|
428
|
+
module.exports.getWebmentionSource = getSource;
|
|
429
|
+
module.exports.getTarget = getTarget;
|
|
430
|
+
module.exports.getWebmentionTarget = getTarget;
|
|
431
|
+
module.exports.getType = getType;
|
|
432
|
+
module.exports.getWebmentionType = getType;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chrisburnell/eleventy-cache-webmentions",
|
|
3
|
-
"version": "2.1.2",
|
|
3
|
+
"version": "2.1.3-beta.2",
|
|
4
4
|
"description": "Cache webmentions using eleventy-fetch and make them available to use in collections, layouts, pages, etc. in Eleventy.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|