Eleventy Filter (getWebmentions.js)
const site = { | |
url: "https://example.com" | |
} | |
const absoluteURL = function(url, base) { | |
if (!base) { | |
base = site.url | |
} | |
try { | |
return (new URL(url, base)).toString() | |
} catch(e) { | |
console.log(`Trying to convert ${url} to be an absolute url with base ${base} and failed.`); | |
return url | |
} | |
}; | |
module.exports = function(eleventyConfig) { | |
eleventyConfig.addFilter("getWebmentions", (webmentions, url, allowedTypes) => { | |
url = absoluteURL(url) | |
if (!url || !webmentions.mentions || !webmentions.mentions[url]) { | |
return [] | |
} | |
if (!allowedTypes) { | |
allowedTypes = ["like-of", "repost-of", "bookmark-of", "mention-of", "in-reply-to"] | |
} | |
else if (typeof allowedTypes === "string") { | |
allowedTypes = [allowedTypes] | |
} | |
const allowedHTML = { | |
allowedTags: ['b', 'i', 'em', 'strong', 'a'], | |
allowedAttributes: { | |
a: ['href'] | |
} | |
} | |
return webmentions.mentions[url] | |
.filter(entry => { | |
return allowedTypes.includes(entry["wm-property"]) | |
}) | |
.filter((entry) => { | |
const { author } = entry | |
return !!author && !!author.name | |
}) | |
.map(entry => { | |
if (!("content" in entry)) { | |
return entry | |
} | |
const { html, text } = entry.content | |
if (html) { | |
// really long html mentions, usually newsletters or compilations | |
entry.content.value = | |
html.length > 2000 | |
? `mentioned this in <a href="${entry['wm-source']}">${entry['wm-source']}</a>` | |
: sanitizeHTML(html, allowedHTML) | |
} | |
else { | |
entry.content.value = sanitizeHTML(text, allowedHTML) | |
} | |
return entry | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment