Skip to content

Commit

Permalink
feat(plugin): extract repeated markup
Browse files Browse the repository at this point in the history
closes #2
  • Loading branch information
bmuenzenmeyer committed Feb 25, 2023
1 parent 89550ea commit f51c55c
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions .eleventy.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
const SERVICE_BASE = `https://t1.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=%%URL%%&size=%%SIZE%%`

const applyTemplate = (text, href, size) => {
return `<a href="${href}"><img alt="favicon for ${href}" loading="lazy" style="max-height: 1em; position: relative; top: .2em; margin-right: .2em" src="${SERVICE_BASE.replace(
"%%URL%%",
href
).replace("%%SIZE%%", size)}"/>${text}</a>`
}

module.exports = (eleventyConfig, options) => {
const defaults = {
size: 128,
}

eleventyConfig.addPairedShortcode("ai", function (content, href, size = 128) {
return `<a href="${href}"><img alt="favicon for ${href}" loading="lazy" style="max-height: 1em; position: relative; top: .2em; margin-right: .2em" src="${SERVICE_BASE.replace("%%URL%%", href).replace("%%SIZE%%", size)}"/>${content}</a>`
eleventyConfig.addPairedShortcode("ai", function (text, href, size = 128) {
return applyTemplate(text, href, size)
})

eleventyConfig.addFilter("ai", (href, text) => {
const { size } = {
...defaults,
...options,
}

return `<a href="${href}"><img alt="favicon for ${href}" loading="lazy" style="max-height: 1em; position: relative; top: .2em; margin-right: .2em" src="${SERVICE_BASE.replace(
"%%URL%%",
href
).replace("%%SIZE%%", size)}"/>${text}</a>`
return applyTemplate(text, href, size)
})
return {
markdownTemplateEngine: "njk",
Expand Down

0 comments on commit f51c55c

Please sign in to comment.