@chrisburnell/eleventy-cache-webmentions 2.2.2 → 2.2.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.
@@ -1 +1 @@
1
- {"cachedAt":1761203061369,"type":"json","metadata":{}}
1
+ {"cachedAt":1761780564178,"type":"json","metadata":{}}
@@ -7,24 +7,24 @@ const sanitizeHTML = require("sanitize-html");
7
7
  */
8
8
 
9
9
  /**
10
- * @typedef {Object} OptionsDefaults
11
- * @prop {boolean} refresh
12
- * @prop {string} duration
13
- * @prop {string} uniqueKey
14
- * @prop {string} [cacheDirectory]
15
- * @prop {AllowedHTML} allowedHTML
16
- * @prop {string[]} allowlist
17
- * @prop {string[]} blocklist
18
- * @prop {Object<string, string>} urlReplacements
19
- * @prop {number} maximumHtmlLength
20
- * @prop {string} maximumHtmlText
10
+ * @typedef {object} OptionsDefaults
11
+ * @property {boolean} refresh
12
+ * @property {string} duration
13
+ * @property {string} uniqueKey
14
+ * @property {string} [cacheDirectory]
15
+ * @property {AllowedHTML} allowedHTML
16
+ * @property {Array<string>} allowlist
17
+ * @property {Array<string>} blocklist
18
+ * @property {{[key: string], string}} urlReplacements
19
+ * @property {number} maximumHtmlLength
20
+ * @property {string} maximumHtmlText
21
21
  */
22
22
 
23
23
  /**
24
- * @typedef {Object} OptionsUserInput
25
- * @prop {string} domain
26
- * @prop {string} feed
27
- * @prop {string} key
24
+ * @typedef {object} OptionsUserInput
25
+ * @property {string} domain
26
+ * @property {string} feed
27
+ * @property {string} key
28
28
  */
29
29
 
30
30
  /**
@@ -32,29 +32,29 @@ const sanitizeHTML = require("sanitize-html");
32
32
  */
33
33
 
34
34
  /**
35
- * @typedef {Object} Webmention
36
- * @prop {string} [source]
37
- * @prop {string} [url]
38
- * @prop {string} [target]
39
- * @prop {string} [published]
40
- * @prop {string} [contentSanitized]
41
- * @prop {Object} [content]
42
- * @prop {string} [content.html]
43
- * @prop {string} [content.value]
44
- * @prop {Object} [data]
45
- * @prop {string} [data.title]
46
- * @prop {string} [data.url]
47
- * @prop {string} [data.published]
48
- * @prop {string} [data.content]
49
- * @prop {string} [type]
50
- * @prop {Object} [activity]
51
- * @prop {string} [activity.type]
52
- * @prop {boolean} [verified]
53
- * @prop {string} ["wm-property"]
54
- * @prop {string} ["wm-received"]
55
- * @prop {string} ["wm-source"]
56
- * @prop {string} ["wm-target"]
57
- * @prop {string} ["verified_date"]
35
+ * @typedef {object} Webmention
36
+ * @property {string} [source]
37
+ * @property {string} [url]
38
+ * @property {string} [target]
39
+ * @property {string} [published]
40
+ * @property {string} [contentSanitized]
41
+ * @property {object} [content]
42
+ * @property {string} [content.html]
43
+ * @property {string} [content.value]
44
+ * @property {object} [data]
45
+ * @property {string} [data.title]
46
+ * @property {string} [data.url]
47
+ * @property {string} [data.published]
48
+ * @property {string} [data.content]
49
+ * @property {string} [type]
50
+ * @property {object} [activity]
51
+ * @property {string} [activity.type]
52
+ * @property {boolean} [verified]
53
+ * @property {string} ["wm-property"]
54
+ * @property {string} ["wm-received"]
55
+ * @property {string} ["wm-source"]
56
+ * @property {string} ["wm-target"]
57
+ * @property {string} ["verified_date"]
58
58
  */
59
59
 
60
60
  /**
@@ -90,7 +90,7 @@ const defaults = {
90
90
  const absoluteURL = (url, domain) => {
91
91
  try {
92
92
  return new URL(url, domain).toString();
93
- } catch (e) {
93
+ } catch (error) {
94
94
  console.error(
95
95
  `Trying to convert ${styleText(
96
96
  "bold",
@@ -99,6 +99,7 @@ const absoluteURL = (url, domain) => {
99
99
  "bold",
100
100
  domain,
101
101
  )} and failed.`,
102
+ error,
102
103
  );
103
104
  return url;
104
105
  }
@@ -116,7 +117,7 @@ const baseURL = (url) => {
116
117
 
117
118
  /**
118
119
  * @param {string} url
119
- * @param {Object<string, string>} [urlReplacements]
120
+ * @param {{[key: string], string}} [urlReplacements]
120
121
  * @returns {string}
121
122
  */
122
123
  const fixURL = (url, urlReplacements) => {
@@ -150,8 +151,8 @@ const epoch = (date) => {
150
151
  };
151
152
 
152
153
  /**
153
- * @param {Webmention[]} webmentions
154
- * @returns {Webmention[]}
154
+ * @param {Array<Webmention>} webmentions
155
+ * @returns {Array<Webmention>}
155
156
  */
156
157
  const removeDuplicates = (webmentions) => {
157
158
  return [
@@ -258,9 +259,9 @@ const getType = (webmention) => {
258
259
  };
259
260
 
260
261
  /**
261
- * @param {Webmention[]} webmentions
262
- * @param {WebmentionType|WebmentionType[]} types
263
- * @returns {Webmention[]}
262
+ * @param {Array<Webmention>} webmentions
263
+ * @param {WebmentionType|Array<WebmentionType>} types
264
+ * @returns {Array<Webmention>}
264
265
  */
265
266
  const getByTypes = (webmentions, types) => {
266
267
  return webmentions.filter((webmention) => {
@@ -272,9 +273,9 @@ const getByTypes = (webmentions, types) => {
272
273
  };
273
274
 
274
275
  /**
275
- * @param {Webmention[]} webmentions
276
- * @param {string[]} blocklist
277
- * @returns {Webmention[]}
276
+ * @param {Array<Webmention>} webmentions
277
+ * @param {Array<string>} blocklist
278
+ * @returns {Array<Webmention>}
278
279
  */
279
280
  const processBlocklist = (webmentions, blocklist) => {
280
281
  return webmentions.filter((webmention) => {
@@ -293,9 +294,9 @@ const processBlocklist = (webmentions, blocklist) => {
293
294
  };
294
295
 
295
296
  /**
296
- * @param {Webmention[]} webmentions
297
- * @param {string[]} allowlist
298
- * @returns {Webmention[]}
297
+ * @param {Array<Webmention>} webmentions
298
+ * @param {Array<string>} allowlist
299
+ * @returns {Array<Webmention>}
299
300
  */
300
301
  const processAllowlist = (webmentions, allowlist) => {
301
302
  return webmentions.filter((webmention) => {
@@ -315,9 +316,9 @@ const processAllowlist = (webmentions, allowlist) => {
315
316
 
316
317
  /**
317
318
  * @param {Options} options
318
- * @param {Webmention[]} webmentions
319
+ * @param {Array<Webmention>} webmentions
319
320
  * @param {string} url
320
- * @returns {Promise<{found: number, webmentions: Webmention[]}>}
321
+ * @returns {Promise<{found: number, webmentions: Array<Webmention>}>}
321
322
  */
322
323
  const fetchWebmentions = async (options, webmentions, url) => {
323
324
  return await fetch(url)
@@ -383,7 +384,7 @@ const fetchWebmentions = async (options, webmentions, url) => {
383
384
 
384
385
  /**
385
386
  * @param {Options} options
386
- * @returns {Promise<Webmention[]>}
387
+ * @returns {Promise<Array<Webmention>>}
387
388
  */
388
389
  const retrieveWebmentions = async (options) => {
389
390
  if (!options.domain) {
@@ -515,12 +516,12 @@ const retrieveWebmentions = async (options) => {
515
516
  return webmentions;
516
517
  };
517
518
 
518
- /** @type {Webmention[]} */
519
+ /** @type {Array<Webmention>} */
519
520
  const WEBMENTIONS = {};
520
521
 
521
522
  /**
522
523
  * @param {Options} options
523
- * @returns {Promise<Object<string, Webmention[]>>}
524
+ * @returns {Promise<{[key: string], Array<Webmention>}>}
524
525
  */
525
526
  const webmentionsByURL = async (options) => {
526
527
  if (Object.keys(WEBMENTIONS).length) {
@@ -552,8 +553,8 @@ const webmentionsByURL = async (options) => {
552
553
  /**
553
554
  * @param {Options} options
554
555
  * @param {string} url
555
- * @param {WebmentionType[]|WebmentionType} [types=[]]
556
- * @returns {Promise<Webmention[]>}
556
+ * @param {WebmentionType|Array<WebmentionType>} [types]
557
+ * @returns {Promise<Array<Webmention>>}
557
558
  */
558
559
  const getWebmentions = async (options, url, types = []) => {
559
560
  const webmentions = await webmentionsByURL(options);
@@ -601,13 +602,13 @@ const getWebmentions = async (options, url, types = []) => {
601
602
  };
602
603
 
603
604
  /**
604
- * @param {Object} eleventyConfig
605
- * @param {Options} [options={}]
605
+ * @param {object} eleventyConfig
606
+ * @param {Options} [options]
606
607
  */
607
- const eleventyCacheWebmentions = (eleventyConfig, options = {}) => {
608
+ const eleventyCacheWebmentions = async (eleventyConfig, options = {}) => {
608
609
  options = Object.assign(defaults, options);
609
610
 
610
- const byURL = webmentionsByURL(options);
611
+ const byURL = await webmentionsByURL(options);
611
612
  const all = Object.values(byURL).reduce(
612
613
  (array, webmentions) => [...array, ...webmentions],
613
614
  [],
@@ -7,24 +7,24 @@ import sanitizeHTML from "sanitize-html";
7
7
  */
8
8
 
9
9
  /**
10
- * @typedef {Object} OptionsDefaults
11
- * @prop {boolean} refresh
12
- * @prop {string} duration
13
- * @prop {string} uniqueKey
14
- * @prop {string} [cacheDirectory]
15
- * @prop {AllowedHTML} allowedHTML
16
- * @prop {string[]} allowlist
17
- * @prop {string[]} blocklist
18
- * @prop {Object<string, string>} urlReplacements
19
- * @prop {number} maximumHtmlLength
20
- * @prop {string} maximumHtmlText
10
+ * @typedef {object} OptionsDefaults
11
+ * @property {boolean} refresh
12
+ * @property {string} duration
13
+ * @property {string} uniqueKey
14
+ * @property {string} [cacheDirectory]
15
+ * @property {AllowedHTML} allowedHTML
16
+ * @property {Array<string>} allowlist
17
+ * @property {Array<string>} blocklist
18
+ * @property {{[key: string], string}} urlReplacements
19
+ * @property {number} maximumHtmlLength
20
+ * @property {string} maximumHtmlText
21
21
  */
22
22
 
23
23
  /**
24
- * @typedef {Object} OptionsUserInput
25
- * @prop {string} domain
26
- * @prop {string} feed
27
- * @prop {string} key
24
+ * @typedef {object} OptionsUserInput
25
+ * @property {string} domain
26
+ * @property {string} feed
27
+ * @property {string} key
28
28
  */
29
29
 
30
30
  /**
@@ -32,29 +32,29 @@ import sanitizeHTML from "sanitize-html";
32
32
  */
33
33
 
34
34
  /**
35
- * @typedef {Object} Webmention
36
- * @prop {string} [source]
37
- * @prop {string} [url]
38
- * @prop {string} [target]
39
- * @prop {string} [published]
40
- * @prop {string} [contentSanitized]
41
- * @prop {Object} [content]
42
- * @prop {string} [content.html]
43
- * @prop {string} [content.value]
44
- * @prop {Object} [data]
45
- * @prop {string} [data.title]
46
- * @prop {string} [data.url]
47
- * @prop {string} [data.published]
48
- * @prop {string} [data.content]
49
- * @prop {string} [type]
50
- * @prop {Object} [activity]
51
- * @prop {string} [activity.type]
52
- * @prop {boolean} [verified]
53
- * @prop {string} ["wm-property"]
54
- * @prop {string} ["wm-received"]
55
- * @prop {string} ["wm-source"]
56
- * @prop {string} ["wm-target"]
57
- * @prop {string} ["verified_date"]
35
+ * @typedef {object} Webmention
36
+ * @property {string} [source]
37
+ * @property {string} [url]
38
+ * @property {string} [target]
39
+ * @property {string} [published]
40
+ * @property {string} [contentSanitized]
41
+ * @property {object} [content]
42
+ * @property {string} [content.html]
43
+ * @property {string} [content.value]
44
+ * @property {object} [data]
45
+ * @property {string} [data.title]
46
+ * @property {string} [data.url]
47
+ * @property {string} [data.published]
48
+ * @property {string} [data.content]
49
+ * @property {string} [type]
50
+ * @property {object} [activity]
51
+ * @property {string} [activity.type]
52
+ * @property {boolean} [verified]
53
+ * @property {string} ["wm-property"]
54
+ * @property {string} ["wm-received"]
55
+ * @property {string} ["wm-source"]
56
+ * @property {string} ["wm-target"]
57
+ * @property {string} ["verified_date"]
58
58
  */
59
59
 
60
60
  /**
@@ -90,7 +90,7 @@ export const defaults = {
90
90
  const absoluteURL = (url, domain) => {
91
91
  try {
92
92
  return new URL(url, domain).toString();
93
- } catch (e) {
93
+ } catch (error) {
94
94
  console.error(
95
95
  `Trying to convert ${styleText(
96
96
  "bold",
@@ -99,6 +99,7 @@ const absoluteURL = (url, domain) => {
99
99
  "bold",
100
100
  domain,
101
101
  )} and failed.`,
102
+ error,
102
103
  );
103
104
  return url;
104
105
  }
@@ -116,7 +117,7 @@ const baseURL = (url) => {
116
117
 
117
118
  /**
118
119
  * @param {string} url
119
- * @param {Object<string, string>} [urlReplacements]
120
+ * @param {{[key: string], string}} [urlReplacements]
120
121
  * @returns {string}
121
122
  */
122
123
  const fixURL = (url, urlReplacements) => {
@@ -150,8 +151,8 @@ const epoch = (date) => {
150
151
  };
151
152
 
152
153
  /**
153
- * @param {Webmention[]} webmentions
154
- * @returns {Webmention[]}
154
+ * @param {Array<Webmention>} webmentions
155
+ * @returns {Array<Webmention>}
155
156
  */
156
157
  const removeDuplicates = (webmentions) => {
157
158
  return [
@@ -265,9 +266,9 @@ export const getType = (webmention) => {
265
266
  export const getWebmentionType = getType;
266
267
 
267
268
  /**
268
- * @param {Webmention[]} webmentions
269
- * @param {WebmentionType|WebmentionType[]} types
270
- * @returns {Webmention[]}
269
+ * @param {Array<Webmention>} webmentions
270
+ * @param {WebmentionType|Array<WebmentionType>} types
271
+ * @returns {Array<Webmention>}
271
272
  */
272
273
  export const getByTypes = (webmentions, types) => {
273
274
  return webmentions.filter((webmention) => {
@@ -282,9 +283,9 @@ export const getWebmentionsByTypes = getByTypes;
282
283
  export const getWebmentionsByType = getByTypes;
283
284
 
284
285
  /**
285
- * @param {Webmention[]} webmentions
286
- * @param {string[]} blocklist
287
- * @returns {Webmention[]}
286
+ * @param {Array<Webmention>} webmentions
287
+ * @param {Array<string>} blocklist
288
+ * @returns {Array<Webmention>}
288
289
  */
289
290
  export const processBlocklist = (webmentions, blocklist) => {
290
291
  return webmentions.filter((webmention) => {
@@ -305,9 +306,9 @@ export const processWebmentionBlocklist = processBlocklist;
305
306
  export const processWebmentionsBlocklist = processBlocklist;
306
307
 
307
308
  /**
308
- * @param {Webmention[]} webmentions
309
- * @param {string[]} allowlist
310
- * @returns {Webmention[]}
309
+ * @param {Array<Webmention>} webmentions
310
+ * @param {Array<string>} allowlist
311
+ * @returns {Array<Webmention>}
311
312
  */
312
313
  export const processAllowlist = (webmentions, allowlist) => {
313
314
  return webmentions.filter((webmention) => {
@@ -329,9 +330,9 @@ export const processWebmentionsAllowlist = processAllowlist;
329
330
 
330
331
  /**
331
332
  * @param {Options} options
332
- * @param {Webmention[]} webmentions
333
+ * @param {Array<Webmention>} webmentions
333
334
  * @param {string} url
334
- * @returns {Promise<{found: number, webmentions: Webmention[]}>}
335
+ * @returns {Promise<{found: number, webmentions: Array<Webmention>}>}
335
336
  */
336
337
  export const fetchWebmentions = async (options, webmentions, url) => {
337
338
  return await fetch(url)
@@ -397,7 +398,7 @@ export const fetchWebmentions = async (options, webmentions, url) => {
397
398
 
398
399
  /**
399
400
  * @param {Options} options
400
- * @returns {Promise<Webmention[]>}
401
+ * @returns {Promise<Array<Webmention>>}
401
402
  */
402
403
  export const retrieveWebmentions = async (options) => {
403
404
  if (!options.domain) {
@@ -529,12 +530,12 @@ export const retrieveWebmentions = async (options) => {
529
530
  return webmentions;
530
531
  };
531
532
 
532
- /** @type {Webmention[]} */
533
+ /** @type {Array<Webmention>} */
533
534
  const WEBMENTIONS = {};
534
535
 
535
536
  /**
536
537
  * @param {Options} options
537
- * @returns {Promise<Object<string, Webmention[]>>}
538
+ * @returns {Promise<{[key: string], Array<Webmention>}>}
538
539
  */
539
540
  export const webmentionsByURL = async (options) => {
540
541
  if (Object.keys(WEBMENTIONS).length) {
@@ -568,8 +569,8 @@ export const filteredWebmentions = webmentionsByURL;
568
569
  /**
569
570
  * @param {Options} options
570
571
  * @param {string} url
571
- * @param {WebmentionType[]|WebmentionType} [types=[]]
572
- * @returns {Promise<Webmention[]>}
572
+ * @param {WebmentionType|Array<WebmentionType>} [types]
573
+ * @returns {Promise<Array<Webmention>>}
573
574
  */
574
575
  export const getWebmentions = async (options, url, types = []) => {
575
576
  const webmentions = await webmentionsByURL(options);
@@ -617,13 +618,16 @@ export const getWebmentions = async (options, url, types = []) => {
617
618
  };
618
619
 
619
620
  /**
620
- * @param {Object} eleventyConfig
621
- * @param {Options} [options={}]
621
+ * @param {object} eleventyConfig
622
+ * @param {Options} [options]
622
623
  */
623
- export const eleventyCacheWebmentions = (eleventyConfig, options = {}) => {
624
+ export const eleventyCacheWebmentions = async (
625
+ eleventyConfig,
626
+ options = {},
627
+ ) => {
624
628
  options = Object.assign(defaults, options);
625
629
 
626
- const byURL = webmentionsByURL(options);
630
+ const byURL = await webmentionsByURL(options);
627
631
  const all = Object.values(byURL).reduce(
628
632
  (array, webmentions) => [...array, ...webmentions],
629
633
  [],
@@ -1 +1 @@
1
- const{AssetCache}=require("@11ty/eleventy-fetch"),{styleText}=require("node:util"),sanitizeHTML=require("sanitize-html"),defaults={refresh:!1,duration:"1d",uniqueKey:"webmentions",cacheDirectory:void 0,allowedHTML:{allowedTags:["a","b","em","i","strong"],allowedAttributes:{a:["href"]}},allowlist:[],blocklist:[],urlReplacements:{},maximumHtmlLength:1e3,maximumHtmlText:"mentioned this in"},absoluteURL=(url,domain)=>{try{return new URL(url,domain).toString()}catch{return console.error(`Trying to convert ${styleText("bold",url)} to be an absolute url with base ${styleText("bold",domain)} and failed.`),url}},baseURL=url=>url.split("#")[0].split("?")[0],fixURL=(url,urlReplacements)=>Object.entries(urlReplacements).reduce((accumulator,[key,value])=>{const regex=new RegExp(key,"g");return accumulator.replace(regex,value)},url),hostname=url=>typeof url=="string"&&url.includes("//")?new URL(url).hostname:url,epoch=date=>new Date(date).getTime(),removeDuplicates=webmentions=>[...webmentions.reduce((map,webmention)=>{const key=webmention==null?webmention:getSource(webmention);return map.has(key)||map.set(key,webmention),map},new Map).values()],getPublished=webmention=>webmention?.data?.published||webmention.published||webmention["wm-received"]||webmention.verified_date,getReceived=webmention=>webmention["wm-received"]||webmention.verified_date||webmention.published||webmention?.data?.published,getContent=webmention=>webmention?.contentSanitized||webmention?.content?.html||webmention?.content?.value||webmention?.content||webmention?.data?.content||"",getSource=webmention=>webmention["wm-source"]||webmention.source||webmention?.data?.url||webmention.url,getURL=webmention=>webmention?.data?.url||webmention.url||webmention["wm-source"]||webmention.source,getTarget=webmention=>webmention["wm-target"]||webmention.target,getType=webmention=>webmention["wm-property"]||webmention?.activity?.type||webmention.type,getByTypes=(webmentions,types)=>webmentions.filter(webmention=>typeof types=="string"?types===getType(webmention):types.includes(getType(webmention))),processBlocklist=(webmentions,blocklist)=>webmentions.filter(webmention=>{let url=getSource(webmention),source=getSource(webmention);for(let blocklistURL of blocklist)if(url.includes(blocklistURL.replace(/\/?$/,"/"))||source.includes(blocklistURL.replace(/\/?$/,"/")))return!1;return!0}),processAllowlist=(webmentions,allowlist)=>webmentions.filter(webmention=>{let url=getSource(webmention),source=getSource(webmention);for(let allowlistURL of allowlist)if(url.includes(allowlistURL.replace(/\/?$/,"/"))||source.includes(allowlistURL.replace(/\/?$/,"/")))return!0;return!1}),fetchWebmentions=async(options,webmentions,url)=>await fetch(url).then(async response=>{if(!response.ok)return Promise.reject(response);const feed=await response.json();return options.key in feed?(webmentions=feed[options.key].concat(webmentions),webmentions=removeDuplicates(webmentions),options.blocklist.length&&(webmentions=processBlocklist(webmentions,options.blocklist)),options.allowlist.length&&(webmentions=processAllowlist(webmentions,options.allowlist)),webmentions=webmentions.sort((a,b)=>epoch(getReceived(b))-epoch(getReceived(a))),{found:feed[options.key].length,webmentions}):(console.log(`${styleText("grey",`[${hostname(options.domain)}]`)} ${options.key} was not found as a key in the response from ${styleText("bold",hostname(options.feed))}!`),Promise.reject(response))}).catch(error=>(console.warn(`${styleText("grey",`[${hostname(options.domain)}]`)} Something went wrong with your Webmention request to ${styleText("bold",hostname(options.feed))}!`),console.warn(error instanceof Error?error.message:error),{found:0,webmentions})),retrieveWebmentions=async options=>{if(!options.domain)throw new Error("`domain` is a required field when attempting to retrieve Webmentions. See https://www.npmjs.com/package/@chrisburnell/eleventy-cache-webmentions#installation for more information.");if(!options.feed)throw new Error("`feed` is a required field when attempting to retrieve Webmentions. See https://www.npmjs.com/package/@chrisburnell/eleventy-cache-webmentions#installation for more information.");if(!options.key)throw new Error("`key` is a required field when attempting to retrieve Webmentions. See https://www.npmjs.com/package/@chrisburnell/eleventy-cache-webmentions#installation for more information.");let asset=new AssetCache(options.uniqueKey||`webmentions-${hostname(options.domain)}`,options.cacheDirectory),webmentions=[];asset.isCacheValid("9001y")&&!options.refresh&&(webmentions=await asset.getCachedValue());const webmentionsCachedLength=webmentions.length;if(!asset.isCacheValid(options.refresh?"0s":options.duration)){const performanceStart=process.hrtime(),since=webmentions.length?getReceived(webmentions[0]):!1,url=`${options.feed}${since?`${options.feed.includes("?")?"&":"?"}since=${since}`:""}`;if(url.includes("https://webmention.io")){const urlObject=new URL(url),perPage=Number(urlObject.searchParams.get("per-page"))||1e3;urlObject.searchParams.delete("per-page");let page=0;for(;;){const urlPaginated=urlObject.href+`&per-page=${perPage}&page=${page}`,fetched=await fetchWebmentions(options,webmentions,urlPaginated);if(!fetched&&!fetched.found&&!fetched.webmentions||fetched.found===0||(webmentions=fetched.webmentions,fetched.found<perPage))break;page+=1,await new Promise(resolve=>setTimeout(resolve,1e3))}}else webmentions=(await fetchWebmentions(options,webmentions,url)).webmentions;options.blocklist.length&&(webmentions=processBlocklist(webmentions,options.blocklist)),options.allowlist.length&&(webmentions=processAllowlist(webmentions,options.allowlist)),await asset.save(webmentions,"json");const performance=process.hrtime(performanceStart);webmentionsCachedLength<webmentions.length&&console.log(`${styleText("grey",`[${hostname(options.domain)}]`)} ${styleText("bold",String(webmentions.length-webmentionsCachedLength))} new Webmentions fetched into cache in ${styleText("bold",(performance[0]+performance[1]/1e9).toFixed(3)+" seconds")}.`)}return webmentions},WEBMENTIONS={},webmentionsByURL=async options=>(Object.keys(WEBMENTIONS).length||(await retrieveWebmentions(options)).forEach(webmention=>{let url=baseURL(fixURL(getTarget(webmention).replace(/\/?$/,"/"),options.urlReplacements));WEBMENTIONS[url]||(WEBMENTIONS[url]=[]),WEBMENTIONS[url].push(webmention)}),WEBMENTIONS),getWebmentions=async(options,url,types=[])=>{const webmentions=await webmentionsByURL(options);return url=absoluteURL(url,options.domain),!url||!webmentions||!webmentions[url]?[]:webmentions[url].filter(entry=>typeof types=="object"&&Object.keys(types).length?types.includes(getType(entry)):typeof types=="string"?types===getType(entry):!0).map(entry=>{const html=getContent(entry);return html.length&&(entry.contentSanitized=sanitizeHTML(html,options.allowedHTML),html.length>options.maximumHtmlLength&&(entry.contentSanitized=`${options.maximumHtmlText} <a href="${getSource(entry)}">${getSource(entry)}</a>`)),entry}).sort((a,b)=>epoch(getPublished(a))-epoch(getPublished(b)))},eleventyCacheWebmentions=(eleventyConfig,options={})=>{options=Object.assign(defaults,options);const byURL=webmentionsByURL(options),all=Object.values(byURL).reduce((array,webmentions)=>[...array,...webmentions],[]);eleventyConfig.addGlobalData("webmentionsDefaults",defaults),eleventyConfig.addGlobalData("webmentionsOptions",options),eleventyConfig.addGlobalData("webmentionsByURL",byURL),eleventyConfig.addGlobalData("webmentionsByUrl",byURL),eleventyConfig.addGlobalData("webmentionsAll",all),eleventyConfig.addLiquidFilter("getWebmentionsByType",getByTypes),eleventyConfig.addLiquidFilter("getWebmentionsByTypes",getByTypes),eleventyConfig.addLiquidFilter("getWebmentionPublished",getPublished),eleventyConfig.addLiquidFilter("getWebmentionReceived",getReceived),eleventyConfig.addLiquidFilter("getWebmentionContent",getContent),eleventyConfig.addLiquidFilter("getWebmentionSource",getSource),eleventyConfig.addLiquidFilter("getWebmentionURL",getURL),eleventyConfig.addLiquidFilter("getWebmentionTarget",getTarget),eleventyConfig.addLiquidFilter("getWebmentionType",getType),eleventyConfig.addNunjucksFilter("getWebmentionsByType",getByTypes),eleventyConfig.addNunjucksFilter("getWebmentionsByTypes",getByTypes),eleventyConfig.addNunjucksFilter("getWebmentionPublished",getPublished),eleventyConfig.addNunjucksFilter("getWebmentionReceived",getReceived),eleventyConfig.addNunjucksFilter("getWebmentionContent",getContent),eleventyConfig.addNunjucksFilter("getWebmentionSource",getSource),eleventyConfig.addNunjucksFilter("getWebmentionURL",getURL),eleventyConfig.addNunjucksFilter("getWebmentionTarget",getTarget),eleventyConfig.addNunjucksFilter("getWebmentionType",getType)};module.exports=eleventyCacheWebmentions,module.exports.defaults=defaults,module.exports.getPublished=getPublished,module.exports.getWebmentionPublished=getPublished,module.exports.getReceived=getReceived,module.exports.getWebmentionReceived=getReceived,module.exports.getContent=getContent,module.exports.getWebmentionContent=getContent,module.exports.getSource=getSource,module.exports.getWebmentionSource=getSource,module.exports.getURL=getURL,module.exports.getWebmentionURL=getURL,module.exports.getTarget=getTarget,module.exports.getWebmentionTarget=getTarget,module.exports.getType=getType,module.exports.getWebmentionType=getType,module.exports.getByTypes=getByTypes,module.exports.getByType=getByTypes,module.exports.getWebmentionsByTypes=getByTypes,module.exports.getWebmentionsByType=getByTypes,module.exports.processBlocklist=processBlocklist,module.exports.processWebmentionBlocklist=processBlocklist,module.exports.processWebmentionsBlocklist=processBlocklist,module.exports.processAllowlist=processAllowlist,module.exports.processWebmentionAllowlist=processAllowlist,module.exports.processWebmentionsAllowlist=processAllowlist,module.exports.fetchWebmentions=fetchWebmentions,module.exports.retrieveWebmentions=retrieveWebmentions,module.exports.webmentionsByURL=webmentionsByURL,module.exports.webmentionsByUrl=webmentionsByURL,module.exports.filteredWebmentions=webmentionsByURL,module.exports.getWebmentions=getWebmentions;
1
+ const{AssetCache}=require("@11ty/eleventy-fetch"),{styleText}=require("node:util"),sanitizeHTML=require("sanitize-html"),defaults={refresh:!1,duration:"1d",uniqueKey:"webmentions",cacheDirectory:void 0,allowedHTML:{allowedTags:["a","b","em","i","strong"],allowedAttributes:{a:["href"]}},allowlist:[],blocklist:[],urlReplacements:{},maximumHtmlLength:1e3,maximumHtmlText:"mentioned this in"},absoluteURL=(url,domain)=>{try{return new URL(url,domain).toString()}catch(error){return console.error(`Trying to convert ${styleText("bold",url)} to be an absolute url with base ${styleText("bold",domain)} and failed.`,error),url}},baseURL=url=>url.split("#")[0].split("?")[0],fixURL=(url,urlReplacements)=>Object.entries(urlReplacements).reduce((accumulator,[key,value])=>{const regex=new RegExp(key,"g");return accumulator.replace(regex,value)},url),hostname=url=>typeof url=="string"&&url.includes("//")?new URL(url).hostname:url,epoch=date=>new Date(date).getTime(),removeDuplicates=webmentions=>[...webmentions.reduce((map,webmention)=>{const key=webmention==null?webmention:getSource(webmention);return map.has(key)||map.set(key,webmention),map},new Map).values()],getPublished=webmention=>webmention?.data?.published||webmention.published||webmention["wm-received"]||webmention.verified_date,getReceived=webmention=>webmention["wm-received"]||webmention.verified_date||webmention.published||webmention?.data?.published,getContent=webmention=>webmention?.contentSanitized||webmention?.content?.html||webmention?.content?.value||webmention?.content||webmention?.data?.content||"",getSource=webmention=>webmention["wm-source"]||webmention.source||webmention?.data?.url||webmention.url,getURL=webmention=>webmention?.data?.url||webmention.url||webmention["wm-source"]||webmention.source,getTarget=webmention=>webmention["wm-target"]||webmention.target,getType=webmention=>webmention["wm-property"]||webmention?.activity?.type||webmention.type,getByTypes=(webmentions,types)=>webmentions.filter(webmention=>typeof types=="string"?types===getType(webmention):types.includes(getType(webmention))),processBlocklist=(webmentions,blocklist)=>webmentions.filter(webmention=>{let url=getSource(webmention),source=getSource(webmention);for(let blocklistURL of blocklist)if(url.includes(blocklistURL.replace(/\/?$/,"/"))||source.includes(blocklistURL.replace(/\/?$/,"/")))return!1;return!0}),processAllowlist=(webmentions,allowlist)=>webmentions.filter(webmention=>{let url=getSource(webmention),source=getSource(webmention);for(let allowlistURL of allowlist)if(url.includes(allowlistURL.replace(/\/?$/,"/"))||source.includes(allowlistURL.replace(/\/?$/,"/")))return!0;return!1}),fetchWebmentions=async(options,webmentions,url)=>await fetch(url).then(async response=>{if(!response.ok)return Promise.reject(response);const feed=await response.json();return options.key in feed?(webmentions=feed[options.key].concat(webmentions),webmentions=removeDuplicates(webmentions),options.blocklist.length&&(webmentions=processBlocklist(webmentions,options.blocklist)),options.allowlist.length&&(webmentions=processAllowlist(webmentions,options.allowlist)),webmentions=webmentions.sort((a,b)=>epoch(getReceived(b))-epoch(getReceived(a))),{found:feed[options.key].length,webmentions}):(console.log(`${styleText("grey",`[${hostname(options.domain)}]`)} ${options.key} was not found as a key in the response from ${styleText("bold",hostname(options.feed))}!`),Promise.reject(response))}).catch(error=>(console.warn(`${styleText("grey",`[${hostname(options.domain)}]`)} Something went wrong with your Webmention request to ${styleText("bold",hostname(options.feed))}!`),console.warn(error instanceof Error?error.message:error),{found:0,webmentions})),retrieveWebmentions=async options=>{if(!options.domain)throw new Error("`domain` is a required field when attempting to retrieve Webmentions. See https://www.npmjs.com/package/@chrisburnell/eleventy-cache-webmentions#installation for more information.");if(!options.feed)throw new Error("`feed` is a required field when attempting to retrieve Webmentions. See https://www.npmjs.com/package/@chrisburnell/eleventy-cache-webmentions#installation for more information.");if(!options.key)throw new Error("`key` is a required field when attempting to retrieve Webmentions. See https://www.npmjs.com/package/@chrisburnell/eleventy-cache-webmentions#installation for more information.");let asset=new AssetCache(options.uniqueKey||`webmentions-${hostname(options.domain)}`,options.cacheDirectory),webmentions=[];asset.isCacheValid("9001y")&&!options.refresh&&(webmentions=await asset.getCachedValue());const webmentionsCachedLength=webmentions.length;if(!asset.isCacheValid(options.refresh?"0s":options.duration)){const performanceStart=process.hrtime(),since=webmentions.length?getReceived(webmentions[0]):!1,url=`${options.feed}${since?`${options.feed.includes("?")?"&":"?"}since=${since}`:""}`;if(url.includes("https://webmention.io")){const urlObject=new URL(url),perPage=Number(urlObject.searchParams.get("per-page"))||1e3;urlObject.searchParams.delete("per-page");let page=0;for(;;){const urlPaginated=urlObject.href+`&per-page=${perPage}&page=${page}`,fetched=await fetchWebmentions(options,webmentions,urlPaginated);if(!fetched&&!fetched.found&&!fetched.webmentions||fetched.found===0||(webmentions=fetched.webmentions,fetched.found<perPage))break;page+=1,await new Promise(resolve=>setTimeout(resolve,1e3))}}else webmentions=(await fetchWebmentions(options,webmentions,url)).webmentions;options.blocklist.length&&(webmentions=processBlocklist(webmentions,options.blocklist)),options.allowlist.length&&(webmentions=processAllowlist(webmentions,options.allowlist)),await asset.save(webmentions,"json");const performance=process.hrtime(performanceStart);webmentionsCachedLength<webmentions.length&&console.log(`${styleText("grey",`[${hostname(options.domain)}]`)} ${styleText("bold",String(webmentions.length-webmentionsCachedLength))} new Webmentions fetched into cache in ${styleText("bold",(performance[0]+performance[1]/1e9).toFixed(3)+" seconds")}.`)}return webmentions},WEBMENTIONS={},webmentionsByURL=async options=>(Object.keys(WEBMENTIONS).length||(await retrieveWebmentions(options)).forEach(webmention=>{let url=baseURL(fixURL(getTarget(webmention).replace(/\/?$/,"/"),options.urlReplacements));WEBMENTIONS[url]||(WEBMENTIONS[url]=[]),WEBMENTIONS[url].push(webmention)}),WEBMENTIONS),getWebmentions=async(options,url,types=[])=>{const webmentions=await webmentionsByURL(options);return url=absoluteURL(url,options.domain),!url||!webmentions||!webmentions[url]?[]:webmentions[url].filter(entry=>typeof types=="object"&&Object.keys(types).length?types.includes(getType(entry)):typeof types=="string"?types===getType(entry):!0).map(entry=>{const html=getContent(entry);return html.length&&(entry.contentSanitized=sanitizeHTML(html,options.allowedHTML),html.length>options.maximumHtmlLength&&(entry.contentSanitized=`${options.maximumHtmlText} <a href="${getSource(entry)}">${getSource(entry)}</a>`)),entry}).sort((a,b)=>epoch(getPublished(a))-epoch(getPublished(b)))},eleventyCacheWebmentions=async(eleventyConfig,options={})=>{options=Object.assign(defaults,options);const byURL=await webmentionsByURL(options),all=Object.values(byURL).reduce((array,webmentions)=>[...array,...webmentions],[]);eleventyConfig.addGlobalData("webmentionsDefaults",defaults),eleventyConfig.addGlobalData("webmentionsOptions",options),eleventyConfig.addGlobalData("webmentionsByURL",byURL),eleventyConfig.addGlobalData("webmentionsByUrl",byURL),eleventyConfig.addGlobalData("webmentionsAll",all),eleventyConfig.addLiquidFilter("getWebmentionsByType",getByTypes),eleventyConfig.addLiquidFilter("getWebmentionsByTypes",getByTypes),eleventyConfig.addLiquidFilter("getWebmentionPublished",getPublished),eleventyConfig.addLiquidFilter("getWebmentionReceived",getReceived),eleventyConfig.addLiquidFilter("getWebmentionContent",getContent),eleventyConfig.addLiquidFilter("getWebmentionSource",getSource),eleventyConfig.addLiquidFilter("getWebmentionURL",getURL),eleventyConfig.addLiquidFilter("getWebmentionTarget",getTarget),eleventyConfig.addLiquidFilter("getWebmentionType",getType),eleventyConfig.addNunjucksFilter("getWebmentionsByType",getByTypes),eleventyConfig.addNunjucksFilter("getWebmentionsByTypes",getByTypes),eleventyConfig.addNunjucksFilter("getWebmentionPublished",getPublished),eleventyConfig.addNunjucksFilter("getWebmentionReceived",getReceived),eleventyConfig.addNunjucksFilter("getWebmentionContent",getContent),eleventyConfig.addNunjucksFilter("getWebmentionSource",getSource),eleventyConfig.addNunjucksFilter("getWebmentionURL",getURL),eleventyConfig.addNunjucksFilter("getWebmentionTarget",getTarget),eleventyConfig.addNunjucksFilter("getWebmentionType",getType)};module.exports=eleventyCacheWebmentions,module.exports.defaults=defaults,module.exports.getPublished=getPublished,module.exports.getWebmentionPublished=getPublished,module.exports.getReceived=getReceived,module.exports.getWebmentionReceived=getReceived,module.exports.getContent=getContent,module.exports.getWebmentionContent=getContent,module.exports.getSource=getSource,module.exports.getWebmentionSource=getSource,module.exports.getURL=getURL,module.exports.getWebmentionURL=getURL,module.exports.getTarget=getTarget,module.exports.getWebmentionTarget=getTarget,module.exports.getType=getType,module.exports.getWebmentionType=getType,module.exports.getByTypes=getByTypes,module.exports.getByType=getByTypes,module.exports.getWebmentionsByTypes=getByTypes,module.exports.getWebmentionsByType=getByTypes,module.exports.processBlocklist=processBlocklist,module.exports.processWebmentionBlocklist=processBlocklist,module.exports.processWebmentionsBlocklist=processBlocklist,module.exports.processAllowlist=processAllowlist,module.exports.processWebmentionAllowlist=processAllowlist,module.exports.processWebmentionsAllowlist=processAllowlist,module.exports.fetchWebmentions=fetchWebmentions,module.exports.retrieveWebmentions=retrieveWebmentions,module.exports.webmentionsByURL=webmentionsByURL,module.exports.webmentionsByUrl=webmentionsByURL,module.exports.filteredWebmentions=webmentionsByURL,module.exports.getWebmentions=getWebmentions;
@@ -1 +1 @@
1
- import{AssetCache}from"@11ty/eleventy-fetch";import{styleText}from"node:util";import sanitizeHTML from"sanitize-html";export const defaults={refresh:!1,duration:"1d",uniqueKey:"webmentions",cacheDirectory:void 0,allowedHTML:{allowedTags:["a","b","em","i","strong"],allowedAttributes:{a:["href"]}},allowlist:[],blocklist:[],urlReplacements:{},maximumHtmlLength:1e3,maximumHtmlText:"mentioned this in"};const absoluteURL=(url,domain)=>{try{return new URL(url,domain).toString()}catch{return console.error(`Trying to convert ${styleText("bold",url)} to be an absolute url with base ${styleText("bold",domain)} and failed.`),url}},baseURL=url=>url.split("#")[0].split("?")[0],fixURL=(url,urlReplacements)=>Object.entries(urlReplacements).reduce((accumulator,[key,value])=>{const regex=new RegExp(key,"g");return accumulator.replace(regex,value)},url),hostname=url=>typeof url=="string"&&url.includes("//")?new URL(url).hostname:url,epoch=date=>new Date(date).getTime(),removeDuplicates=webmentions=>[...webmentions.reduce((map,webmention)=>{const key=webmention==null?webmention:getSource(webmention);return map.has(key)||map.set(key,webmention),map},new Map).values()];export const getPublished=webmention=>webmention?.data?.published||webmention.published||webmention["wm-received"]||webmention.verified_date,getWebmentionPublished=getPublished,getReceived=webmention=>webmention["wm-received"]||webmention.verified_date||webmention.published||webmention?.data?.published,getWebmentionReceived=getReceived,getContent=webmention=>webmention?.contentSanitized||webmention?.content?.html||webmention?.content?.value||webmention?.content||webmention?.data?.content||"",getWebmentionContent=getContent,getSource=webmention=>webmention["wm-source"]||webmention.source||webmention?.data?.url||webmention.url,getWebmentionSource=getSource,getURL=webmention=>webmention?.data?.url||webmention.url||webmention["wm-source"]||webmention.source,getWebmentionURL=getURL,getTarget=webmention=>webmention["wm-target"]||webmention.target,getWebmentionTarget=getTarget,getType=webmention=>webmention["wm-property"]||webmention?.activity?.type||webmention.type,getWebmentionType=getType,getByTypes=(webmentions,types)=>webmentions.filter(webmention=>typeof types=="string"?types===getType(webmention):types.includes(getType(webmention))),getByType=getByTypes,getWebmentionsByTypes=getByTypes,getWebmentionsByType=getByTypes,processBlocklist=(webmentions,blocklist)=>webmentions.filter(webmention=>{let url=getSource(webmention),source=getSource(webmention);for(let blocklistURL of blocklist)if(url.includes(blocklistURL.replace(/\/?$/,"/"))||source.includes(blocklistURL.replace(/\/?$/,"/")))return!1;return!0}),processWebmentionBlocklist=processBlocklist,processWebmentionsBlocklist=processBlocklist,processAllowlist=(webmentions,allowlist)=>webmentions.filter(webmention=>{let url=getSource(webmention),source=getSource(webmention);for(let allowlistURL of allowlist)if(url.includes(allowlistURL.replace(/\/?$/,"/"))||source.includes(allowlistURL.replace(/\/?$/,"/")))return!0;return!1}),processWebmentionAllowlist=processAllowlist,processWebmentionsAllowlist=processAllowlist,fetchWebmentions=async(options,webmentions,url)=>await fetch(url).then(async response=>{if(!response.ok)return Promise.reject(response);const feed=await response.json();return options.key in feed?(webmentions=feed[options.key].concat(webmentions),webmentions=removeDuplicates(webmentions),options.blocklist.length&&(webmentions=processBlocklist(webmentions,options.blocklist)),options.allowlist.length&&(webmentions=processAllowlist(webmentions,options.allowlist)),webmentions=webmentions.sort((a,b)=>epoch(getReceived(b))-epoch(getReceived(a))),{found:feed[options.key].length,webmentions}):(console.log(`${styleText("grey",`[${hostname(options.domain)}]`)} ${options.key} was not found as a key in the response from ${styleText("bold",hostname(options.feed))}!`),Promise.reject(response))}).catch(error=>(console.warn(`${styleText("grey",`[${hostname(options.domain)}]`)} Something went wrong with your Webmention request to ${styleText("bold",hostname(options.feed))}!`),console.warn(error instanceof Error?error.message:error),{found:0,webmentions})),retrieveWebmentions=async options=>{if(!options.domain)throw new Error("`domain` is a required field when attempting to retrieve Webmentions. See https://www.npmjs.com/package/@chrisburnell/eleventy-cache-webmentions#installation for more information.");if(!options.feed)throw new Error("`feed` is a required field when attempting to retrieve Webmentions. See https://www.npmjs.com/package/@chrisburnell/eleventy-cache-webmentions#installation for more information.");if(!options.key)throw new Error("`key` is a required field when attempting to retrieve Webmentions. See https://www.npmjs.com/package/@chrisburnell/eleventy-cache-webmentions#installation for more information.");let asset=new AssetCache(options.uniqueKey||`webmentions-${hostname(options.domain)}`,options.cacheDirectory),webmentions=[];asset.isCacheValid("9001y")&&!options.refresh&&(webmentions=await asset.getCachedValue());const webmentionsCachedLength=webmentions.length;if(!asset.isCacheValid(options.refresh?"0s":options.duration)){const performanceStart=process.hrtime(),since=webmentions.length?getReceived(webmentions[0]):!1,url=`${options.feed}${since?`${options.feed.includes("?")?"&":"?"}since=${since}`:""}`;if(url.includes("https://webmention.io")){const urlObject=new URL(url),perPage=Number(urlObject.searchParams.get("per-page"))||1e3;urlObject.searchParams.delete("per-page");let page=0;for(;;){const urlPaginated=urlObject.href+`&per-page=${perPage}&page=${page}`,fetched=await fetchWebmentions(options,webmentions,urlPaginated);if(!fetched&&!fetched.found&&!fetched.webmentions||fetched.found===0||(webmentions=fetched.webmentions,fetched.found<perPage))break;page+=1,await new Promise(resolve=>setTimeout(resolve,1e3))}}else webmentions=(await fetchWebmentions(options,webmentions,url)).webmentions;options.blocklist.length&&(webmentions=processBlocklist(webmentions,options.blocklist)),options.allowlist.length&&(webmentions=processAllowlist(webmentions,options.allowlist)),await asset.save(webmentions,"json");const performance=process.hrtime(performanceStart);webmentionsCachedLength<webmentions.length&&console.log(`${styleText("grey",`[${hostname(options.domain)}]`)} ${styleText("bold",String(webmentions.length-webmentionsCachedLength))} new Webmentions fetched into cache in ${styleText("bold",(performance[0]+performance[1]/1e9).toFixed(3)+" seconds")}.`)}return webmentions};const WEBMENTIONS={};export const webmentionsByURL=async options=>(Object.keys(WEBMENTIONS).length||(await retrieveWebmentions(options)).forEach(webmention=>{let url=baseURL(fixURL(getTarget(webmention).replace(/\/?$/,"/"),options.urlReplacements));WEBMENTIONS[url]||(WEBMENTIONS[url]=[]),WEBMENTIONS[url].push(webmention)}),WEBMENTIONS),webmentionsByUrl=webmentionsByURL,filteredWebmentions=webmentionsByURL,getWebmentions=async(options,url,types=[])=>{const webmentions=await webmentionsByURL(options);return url=absoluteURL(url,options.domain),!url||!webmentions||!webmentions[url]?[]:webmentions[url].filter(entry=>typeof types=="object"&&Object.keys(types).length?types.includes(getType(entry)):typeof types=="string"?types===getType(entry):!0).map(entry=>{const html=getContent(entry);return html.length&&(entry.contentSanitized=sanitizeHTML(html,options.allowedHTML),html.length>options.maximumHtmlLength&&(entry.contentSanitized=`${options.maximumHtmlText} <a href="${getSource(entry)}">${getSource(entry)}</a>`)),entry}).sort((a,b)=>epoch(getPublished(a))-epoch(getPublished(b)))},eleventyCacheWebmentions=(eleventyConfig,options={})=>{options=Object.assign(defaults,options);const byURL=webmentionsByURL(options),all=Object.values(byURL).reduce((array,webmentions)=>[...array,...webmentions],[]);eleventyConfig.addGlobalData("webmentionsDefaults",defaults),eleventyConfig.addGlobalData("webmentionsOptions",options),eleventyConfig.addGlobalData("webmentionsByURL",byURL),eleventyConfig.addGlobalData("webmentionsByUrl",byURL),eleventyConfig.addGlobalData("webmentionsAll",all),eleventyConfig.addLiquidFilter("getWebmentionsByType",getByTypes),eleventyConfig.addLiquidFilter("getWebmentionsByTypes",getByTypes),eleventyConfig.addLiquidFilter("getWebmentionPublished",getPublished),eleventyConfig.addLiquidFilter("getWebmentionReceived",getReceived),eleventyConfig.addLiquidFilter("getWebmentionContent",getContent),eleventyConfig.addLiquidFilter("getWebmentionSource",getSource),eleventyConfig.addLiquidFilter("getWebmentionURL",getURL),eleventyConfig.addLiquidFilter("getWebmentionTarget",getTarget),eleventyConfig.addLiquidFilter("getWebmentionType",getType),eleventyConfig.addNunjucksFilter("getWebmentionsByType",getByTypes),eleventyConfig.addNunjucksFilter("getWebmentionsByTypes",getByTypes),eleventyConfig.addNunjucksFilter("getWebmentionPublished",getPublished),eleventyConfig.addNunjucksFilter("getWebmentionReceived",getReceived),eleventyConfig.addNunjucksFilter("getWebmentionContent",getContent),eleventyConfig.addNunjucksFilter("getWebmentionSource",getSource),eleventyConfig.addNunjucksFilter("getWebmentionURL",getURL),eleventyConfig.addNunjucksFilter("getWebmentionTarget",getTarget),eleventyConfig.addNunjucksFilter("getWebmentionType",getType)};export default eleventyCacheWebmentions;
1
+ import{AssetCache}from"@11ty/eleventy-fetch";import{styleText}from"node:util";import sanitizeHTML from"sanitize-html";export const defaults={refresh:!1,duration:"1d",uniqueKey:"webmentions",cacheDirectory:void 0,allowedHTML:{allowedTags:["a","b","em","i","strong"],allowedAttributes:{a:["href"]}},allowlist:[],blocklist:[],urlReplacements:{},maximumHtmlLength:1e3,maximumHtmlText:"mentioned this in"};const absoluteURL=(url,domain)=>{try{return new URL(url,domain).toString()}catch(error){return console.error(`Trying to convert ${styleText("bold",url)} to be an absolute url with base ${styleText("bold",domain)} and failed.`,error),url}},baseURL=url=>url.split("#")[0].split("?")[0],fixURL=(url,urlReplacements)=>Object.entries(urlReplacements).reduce((accumulator,[key,value])=>{const regex=new RegExp(key,"g");return accumulator.replace(regex,value)},url),hostname=url=>typeof url=="string"&&url.includes("//")?new URL(url).hostname:url,epoch=date=>new Date(date).getTime(),removeDuplicates=webmentions=>[...webmentions.reduce((map,webmention)=>{const key=webmention==null?webmention:getSource(webmention);return map.has(key)||map.set(key,webmention),map},new Map).values()];export const getPublished=webmention=>webmention?.data?.published||webmention.published||webmention["wm-received"]||webmention.verified_date,getWebmentionPublished=getPublished,getReceived=webmention=>webmention["wm-received"]||webmention.verified_date||webmention.published||webmention?.data?.published,getWebmentionReceived=getReceived,getContent=webmention=>webmention?.contentSanitized||webmention?.content?.html||webmention?.content?.value||webmention?.content||webmention?.data?.content||"",getWebmentionContent=getContent,getSource=webmention=>webmention["wm-source"]||webmention.source||webmention?.data?.url||webmention.url,getWebmentionSource=getSource,getURL=webmention=>webmention?.data?.url||webmention.url||webmention["wm-source"]||webmention.source,getWebmentionURL=getURL,getTarget=webmention=>webmention["wm-target"]||webmention.target,getWebmentionTarget=getTarget,getType=webmention=>webmention["wm-property"]||webmention?.activity?.type||webmention.type,getWebmentionType=getType,getByTypes=(webmentions,types)=>webmentions.filter(webmention=>typeof types=="string"?types===getType(webmention):types.includes(getType(webmention))),getByType=getByTypes,getWebmentionsByTypes=getByTypes,getWebmentionsByType=getByTypes,processBlocklist=(webmentions,blocklist)=>webmentions.filter(webmention=>{let url=getSource(webmention),source=getSource(webmention);for(let blocklistURL of blocklist)if(url.includes(blocklistURL.replace(/\/?$/,"/"))||source.includes(blocklistURL.replace(/\/?$/,"/")))return!1;return!0}),processWebmentionBlocklist=processBlocklist,processWebmentionsBlocklist=processBlocklist,processAllowlist=(webmentions,allowlist)=>webmentions.filter(webmention=>{let url=getSource(webmention),source=getSource(webmention);for(let allowlistURL of allowlist)if(url.includes(allowlistURL.replace(/\/?$/,"/"))||source.includes(allowlistURL.replace(/\/?$/,"/")))return!0;return!1}),processWebmentionAllowlist=processAllowlist,processWebmentionsAllowlist=processAllowlist,fetchWebmentions=async(options,webmentions,url)=>await fetch(url).then(async response=>{if(!response.ok)return Promise.reject(response);const feed=await response.json();return options.key in feed?(webmentions=feed[options.key].concat(webmentions),webmentions=removeDuplicates(webmentions),options.blocklist.length&&(webmentions=processBlocklist(webmentions,options.blocklist)),options.allowlist.length&&(webmentions=processAllowlist(webmentions,options.allowlist)),webmentions=webmentions.sort((a,b)=>epoch(getReceived(b))-epoch(getReceived(a))),{found:feed[options.key].length,webmentions}):(console.log(`${styleText("grey",`[${hostname(options.domain)}]`)} ${options.key} was not found as a key in the response from ${styleText("bold",hostname(options.feed))}!`),Promise.reject(response))}).catch(error=>(console.warn(`${styleText("grey",`[${hostname(options.domain)}]`)} Something went wrong with your Webmention request to ${styleText("bold",hostname(options.feed))}!`),console.warn(error instanceof Error?error.message:error),{found:0,webmentions})),retrieveWebmentions=async options=>{if(!options.domain)throw new Error("`domain` is a required field when attempting to retrieve Webmentions. See https://www.npmjs.com/package/@chrisburnell/eleventy-cache-webmentions#installation for more information.");if(!options.feed)throw new Error("`feed` is a required field when attempting to retrieve Webmentions. See https://www.npmjs.com/package/@chrisburnell/eleventy-cache-webmentions#installation for more information.");if(!options.key)throw new Error("`key` is a required field when attempting to retrieve Webmentions. See https://www.npmjs.com/package/@chrisburnell/eleventy-cache-webmentions#installation for more information.");let asset=new AssetCache(options.uniqueKey||`webmentions-${hostname(options.domain)}`,options.cacheDirectory),webmentions=[];asset.isCacheValid("9001y")&&!options.refresh&&(webmentions=await asset.getCachedValue());const webmentionsCachedLength=webmentions.length;if(!asset.isCacheValid(options.refresh?"0s":options.duration)){const performanceStart=process.hrtime(),since=webmentions.length?getReceived(webmentions[0]):!1,url=`${options.feed}${since?`${options.feed.includes("?")?"&":"?"}since=${since}`:""}`;if(url.includes("https://webmention.io")){const urlObject=new URL(url),perPage=Number(urlObject.searchParams.get("per-page"))||1e3;urlObject.searchParams.delete("per-page");let page=0;for(;;){const urlPaginated=urlObject.href+`&per-page=${perPage}&page=${page}`,fetched=await fetchWebmentions(options,webmentions,urlPaginated);if(!fetched&&!fetched.found&&!fetched.webmentions||fetched.found===0||(webmentions=fetched.webmentions,fetched.found<perPage))break;page+=1,await new Promise(resolve=>setTimeout(resolve,1e3))}}else webmentions=(await fetchWebmentions(options,webmentions,url)).webmentions;options.blocklist.length&&(webmentions=processBlocklist(webmentions,options.blocklist)),options.allowlist.length&&(webmentions=processAllowlist(webmentions,options.allowlist)),await asset.save(webmentions,"json");const performance=process.hrtime(performanceStart);webmentionsCachedLength<webmentions.length&&console.log(`${styleText("grey",`[${hostname(options.domain)}]`)} ${styleText("bold",String(webmentions.length-webmentionsCachedLength))} new Webmentions fetched into cache in ${styleText("bold",(performance[0]+performance[1]/1e9).toFixed(3)+" seconds")}.`)}return webmentions};const WEBMENTIONS={};export const webmentionsByURL=async options=>(Object.keys(WEBMENTIONS).length||(await retrieveWebmentions(options)).forEach(webmention=>{let url=baseURL(fixURL(getTarget(webmention).replace(/\/?$/,"/"),options.urlReplacements));WEBMENTIONS[url]||(WEBMENTIONS[url]=[]),WEBMENTIONS[url].push(webmention)}),WEBMENTIONS),webmentionsByUrl=webmentionsByURL,filteredWebmentions=webmentionsByURL,getWebmentions=async(options,url,types=[])=>{const webmentions=await webmentionsByURL(options);return url=absoluteURL(url,options.domain),!url||!webmentions||!webmentions[url]?[]:webmentions[url].filter(entry=>typeof types=="object"&&Object.keys(types).length?types.includes(getType(entry)):typeof types=="string"?types===getType(entry):!0).map(entry=>{const html=getContent(entry);return html.length&&(entry.contentSanitized=sanitizeHTML(html,options.allowedHTML),html.length>options.maximumHtmlLength&&(entry.contentSanitized=`${options.maximumHtmlText} <a href="${getSource(entry)}">${getSource(entry)}</a>`)),entry}).sort((a,b)=>epoch(getPublished(a))-epoch(getPublished(b)))},eleventyCacheWebmentions=async(eleventyConfig,options={})=>{options=Object.assign(defaults,options);const byURL=await webmentionsByURL(options),all=Object.values(byURL).reduce((array,webmentions)=>[...array,...webmentions],[]);eleventyConfig.addGlobalData("webmentionsDefaults",defaults),eleventyConfig.addGlobalData("webmentionsOptions",options),eleventyConfig.addGlobalData("webmentionsByURL",byURL),eleventyConfig.addGlobalData("webmentionsByUrl",byURL),eleventyConfig.addGlobalData("webmentionsAll",all),eleventyConfig.addLiquidFilter("getWebmentionsByType",getByTypes),eleventyConfig.addLiquidFilter("getWebmentionsByTypes",getByTypes),eleventyConfig.addLiquidFilter("getWebmentionPublished",getPublished),eleventyConfig.addLiquidFilter("getWebmentionReceived",getReceived),eleventyConfig.addLiquidFilter("getWebmentionContent",getContent),eleventyConfig.addLiquidFilter("getWebmentionSource",getSource),eleventyConfig.addLiquidFilter("getWebmentionURL",getURL),eleventyConfig.addLiquidFilter("getWebmentionTarget",getTarget),eleventyConfig.addLiquidFilter("getWebmentionType",getType),eleventyConfig.addNunjucksFilter("getWebmentionsByType",getByTypes),eleventyConfig.addNunjucksFilter("getWebmentionsByTypes",getByTypes),eleventyConfig.addNunjucksFilter("getWebmentionPublished",getPublished),eleventyConfig.addNunjucksFilter("getWebmentionReceived",getReceived),eleventyConfig.addNunjucksFilter("getWebmentionContent",getContent),eleventyConfig.addNunjucksFilter("getWebmentionSource",getSource),eleventyConfig.addNunjucksFilter("getWebmentionURL",getURL),eleventyConfig.addNunjucksFilter("getWebmentionTarget",getTarget),eleventyConfig.addNunjucksFilter("getWebmentionType",getType)};export default eleventyCacheWebmentions;
package/eslint.config.js CHANGED
@@ -1,11 +1,17 @@
1
1
  import js from "@eslint/js";
2
+ import jsdoc from "eslint-plugin-jsdoc";
3
+ import globals from "globals";
2
4
 
3
5
  export default [
4
6
  js.configs.recommended,
5
7
  {
6
- files: ["src/**/*.js"],
8
+ files: ["**/*.js"],
9
+ plugins: { jsdoc },
7
10
  languageOptions: {
8
11
  ecmaVersion: "latest",
12
+ globals: {
13
+ ...globals.node,
14
+ },
9
15
  },
10
16
  rules: {
11
17
  "no-process-env": 0,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chrisburnell/eleventy-cache-webmentions",
3
- "version": "2.2.2",
3
+ "version": "2.2.4",
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": {
@@ -40,8 +40,8 @@
40
40
  "main": "eleventy-cache-webmentions.cjs",
41
41
  "exports": {
42
42
  ".": {
43
- "require": "./eleventy-cache-webmentions.cjs",
44
- "import": "./eleventy-cache-webmentions.js"
43
+ "import": "./eleventy-cache-webmentions.js",
44
+ "require": "./eleventy-cache-webmentions.cjs"
45
45
  }
46
46
  },
47
47
  "scripts": {
@@ -67,6 +67,8 @@
67
67
  "devDependencies": {
68
68
  "esbuild": "^0.25.11",
69
69
  "eslint": "^9.38.0",
70
+ "eslint-plugin-jsdoc": "^61.1.10",
71
+ "globals": "^16.4.0",
70
72
  "nock": "^14.0.10"
71
73
  },
72
74
  "type": "module"