Skip to content

Instantly share code, notes, and snippets.

@chrisburnell
Last active November 21, 2020 12:49
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save chrisburnell/36134bbb26234a4d92423e352a693f44 to your computer and use it in GitHub Desktop.
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