@anyblades/buildawesome-kit-plugin 1.3.0-alpha
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.
- package/features/autoLinkFavicons.js +71 -0
- package/features/autoLinkFavicons.test.js +694 -0
- package/features/mdAutoNl2br.js +22 -0
- package/features/mdAutoNl2br.test.js +68 -0
- package/features/mdAutoRawTags.js +17 -0
- package/features/mdAutoRawTags.test.js +80 -0
- package/features/mdAutoUncommentAttrs.js +26 -0
- package/features/mdAutoUncommentAttrs.test.js +65 -0
- package/features/siteData.js +43 -0
- package/features/siteData.test.js +120 -0
- package/features/virtualPages.js +18 -0
- package/filters/attr_concat.js +55 -0
- package/filters/attr_concat.test.js +205 -0
- package/filters/attr_includes.js +41 -0
- package/filters/attr_includes.test.js +125 -0
- package/filters/attr_set.js +22 -0
- package/filters/attr_set.test.js +71 -0
- package/filters/date.js +11 -0
- package/filters/date.test.js +33 -0
- package/filters/fetch.js +80 -0
- package/filters/if.js +24 -0
- package/filters/if.test.js +63 -0
- package/filters/merge.js +51 -0
- package/filters/merge.test.js +51 -0
- package/filters/remove_tag.js +42 -0
- package/filters/remove_tag.test.js +60 -0
- package/filters/section.js +82 -0
- package/filters/section.test.js +174 -0
- package/filters/split.js +9 -0
- package/filters/split.test.js +39 -0
- package/filters/strip_tag.js +39 -0
- package/filters/strip_tag.test.js +74 -0
- package/filters/unindent.js +11 -0
- package/filters/unindent.test.js +49 -0
- package/package.json +48 -0
- package/plugin.js +53 -0
- package/plugin.test.js +8 -0
- package/scripts/README.md +59 -0
- package/scripts/package.json +17 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/*<!--section:docs-->
|
|
2
|
+
|
|
3
|
+
`autoLinkFavicons` feature prepends a favicon image (from Google's favicon service) to external links whose text is a plain URL or contains only inline emphasis (`<em>`/`<strong>`) — skipping any link already decorated with an `↗` marker.
|
|
4
|
+
|
|
5
|
+
For each qualifying link the link text is cleaned (protocol prefix and trailing slash stripped; domain portion removed when significant path text remains), then wrapped in the original `<a>` tag preceded by `<i><img src="https://www.google.com/s2/favicons?…"></i>`. Any HTML already present in the link text is wrapped in a `<span>` so the favicon image stays a direct child of `<a>`. Only `.html` output files are processed.
|
|
6
|
+
|
|
7
|
+
> Compatible with: https://blades.ninja/css/link-icon/ <i class="faded">← this link is a live example!</i>
|
|
8
|
+
|
|
9
|
+
<!--section:code-->```js */
|
|
10
|
+
export function isExternalUrl(url) {
|
|
11
|
+
return /^https?:\/\//.test(url);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function cleanLinkText(linkText) {
|
|
15
|
+
return linkText
|
|
16
|
+
.trim()
|
|
17
|
+
.replace(/^https?:\/\//, "")
|
|
18
|
+
.replace(/\/$/, "");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function getExtraAttrs(attrs, defaults) {
|
|
22
|
+
return Object.entries(defaults)
|
|
23
|
+
.map(([key, value]) => {
|
|
24
|
+
const regex = new RegExp(`\\b${key}=`, "i");
|
|
25
|
+
return regex.test(attrs) ? "" : ` ${key}="${value}"`;
|
|
26
|
+
})
|
|
27
|
+
.join("");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function buildFaviconLink(attrs, domain, text) {
|
|
31
|
+
const extraAttrs = getExtraAttrs(attrs, {
|
|
32
|
+
title: domain,
|
|
33
|
+
target: "_blank",
|
|
34
|
+
rel: "noopener noreferrer",
|
|
35
|
+
});
|
|
36
|
+
const wrappedText = /<[a-z]/i.test(text) ? `<span>${text}</span>` : text;
|
|
37
|
+
return `<a ${attrs}${extraAttrs}><i><img src="https://www.google.com/s2/favicons?domain=${domain}&sz=64"></i> ${wrappedText}</a>`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function transformLink(match, attrs, url, linkText) {
|
|
41
|
+
try {
|
|
42
|
+
const domain = new URL(url).hostname;
|
|
43
|
+
|
|
44
|
+
if (isExternalUrl(url) && !linkText.includes("↗")) {
|
|
45
|
+
const cleaned = cleanLinkText(linkText);
|
|
46
|
+
const stripped = cleaned.replace(domain, "");
|
|
47
|
+
const label = linkText.trim() === url && stripped.length > 2 ? stripped : cleaned;
|
|
48
|
+
return buildFaviconLink(attrs, domain, label);
|
|
49
|
+
}
|
|
50
|
+
} catch (e) {
|
|
51
|
+
// URL parsing failed — fall through and return original match
|
|
52
|
+
}
|
|
53
|
+
return match;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function replaceLinksInHtml(content, transformer) {
|
|
57
|
+
return content.replace(
|
|
58
|
+
/<a\s+([^>]*href=["']([^"']+)["'][^>]*)>([^<]*(?:<\/?(?:em|strong|code)>[^<]*)*)<\/a>/gi,
|
|
59
|
+
transformer,
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export default function (eleventyConfig) {
|
|
64
|
+
eleventyConfig.addTransform("autoLinkFavicons", function (content) {
|
|
65
|
+
if (this.page.outputPath && this.page.outputPath.endsWith(".html")) {
|
|
66
|
+
return replaceLinksInHtml(content, transformLink);
|
|
67
|
+
}
|
|
68
|
+
return content;
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
//```
|