Skip to content

Commit 6d81311

Browse files
Merge pull request #15 from bmuenzenmeyer/small-optimizations
Small optimizations
2 parents 497912a + efbf195 commit 6d81311

File tree

7 files changed

+336
-20
lines changed

7 files changed

+336
-20
lines changed

.eleventy.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
const SERVICE_BASE = `https://t1.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=%%URL%%&size=%%SIZE%%`
22

3+
const applyTemplate = (text, href, size) => {
4+
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(
5+
"%%URL%%",
6+
href
7+
).replace("%%SIZE%%", size)}"/>${text}</a>`
8+
}
9+
310
module.exports = (eleventyConfig, options) => {
411
const defaults = {
512
size: 128,
613
}
714

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

1219
eleventyConfig.addFilter("ai", (href, text) => {
1320
const { size } = {
1421
...defaults,
1522
...options,
1623
}
17-
18-
return `<a href="${href}"><img alt="favicon for ${href}" style="max-height: 1em; position: relative; top: .2em; margin-right: .2em" src="${SERVICE_BASE.replace(
19-
"%%URL%%",
20-
href
21-
).replace("%%SIZE%%", size)}"/>${text}</a>`
24+
return applyTemplate(text, href, size)
2225
})
2326
return {
2427
markdownTemplateEngine: "njk",

.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
.prettierrc.js
44
_includes
55
_site
6+
demo
67
tests
78
index.md
89
CODE_OF_CONDUCT.md

README.md

+41-10
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,62 @@ module.exports = (eleventyConfig) => {
2828

2929
This plugin comes with two mechanisms to create an inline link favicon. You can use a [paired shortcode](https://www.11ty.dev/docs/shortcodes/#paired-shortcodes) or a [filter](https://www.11ty.dev/docs/filters/), both referenced as `ai`. `ai` is short for anchor-image.
3030

31-
## Paired Shortcode
31+
### Paired Shortcode
3232

33-
```md
33+
<!-- CODEBLOCK_START {"value": "demo/paired-shortcode.njk", "hideValue": true} -->
34+
<!-- prettier-ignore -->
35+
~~~~~~~~~~njk
3436
{% ai "https://front-end.social/@brian" %}@brian{% endai %}
35-
```
37+
~~~~~~~~~~
38+
39+
<!-- CODEBLOCK_END -->
3640

3741
returns
3842

39-
```html
43+
<!-- CODEBLOCK_START {"value": "_site/demo/paired-shortcode/index.html", "hideValue": true} -->
44+
<!-- prettier-ignore -->
45+
~~~~~~~~~~html
4046
<a href="https://front-end.social/@brian"
4147
><img
42-
style="max-height: 1em; position: relative; top: .2em; margin-right: .2em"
43-
src="https://t1.gstatic.com/faviconV2?client=SOCIAL&amp;type=FAVICON&amp;fallback_opts=TYPE,SIZE,URL&amp;url=https://front-end.social/@brian&amp;size=1"
48+
alt="favicon for https://front-end.social/@brian"
49+
loading="lazy"
50+
style="max-height: 1em; position: relative; top: 0.2em; margin-right: 0.2em"
51+
src="https://t1.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=https://front-end.social/@brian&size=128"
4452
/>@brian</a
4553
>
46-
```
54+
~~~~~~~~~~
4755

48-
## Filter
56+
<!-- CODEBLOCK_END -->
4957

50-
```md
58+
### Filter
59+
60+
<!-- CODEBLOCK_START {"value": "demo/filter.njk", "hideValue": true} -->
61+
<!-- prettier-ignore -->
62+
~~~~~~~~~~njk
5163
{{ "https://front-end.social/@brian" | ai("@brian") | safe }}
52-
```
64+
~~~~~~~~~~
65+
66+
<!-- CODEBLOCK_END -->
5367

5468
returns the same as above.
5569

70+
### Snippets / Completions
71+
72+
Authoring content with this plugin is aided by user-defined snippets:
73+
74+
- [Visual Studio Code](https://code.visualstudio.com/docs/editor/userdefinedsnippets)
75+
76+
```json
77+
{
78+
"ai": {
79+
"scope": "markdown,nunjucks",
80+
"prefix": "ai",
81+
"body": ["{% ai \"$1\"%}$2{% endai %}$0"],
82+
"description": "add an inline link favicon"
83+
}
84+
}
85+
```
86+
5687
## Credits
5788

5889
- Thanks to [5t3ph/eleventy-plugin-template](https://github.com/5t3ph/eleventy-plugin-template) for inspiration

demo/filter.njk

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ "https://front-end.social/@brian" | ai("@brian") | safe }}

demo/paired-shortcode.njk

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{% ai "https://front-end.social/@brian" %}@brian{% endai %}

0 commit comments

Comments
 (0)