Skip to content

Commit ed71c3d

Browse files
committed
22.1
[til] - size tags by count - manually define creation date - add (and use) reference shortcode
1 parent 72feda8 commit ed71c3d

7 files changed

Lines changed: 47 additions & 27 deletions

File tree

.eleventy.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module.exports = function (eleventyConfig) {
2929
eleventyConfig.addShortcode("tilTags", (data) => buildTagWall(data));
3030
eleventyConfig.addShortcode("tilRecents", (data) => getRecents(data));
3131
eleventyConfig.addShortcode("tilRelated", (data, current) => getRelated(data, current));
32-
eleventyConfig.addShortcode("tilTimestamps", (element) => buildTimestamps(element) );
32+
eleventyConfig.addShortcode("tilTimestamps", buildTimestamps);
3333

3434
eleventyConfig.addShortcode("qka", (data) => qka(data));
3535
eleventyConfig.addShortcode("quizButtons", (json) => quizButtons(json));
@@ -41,6 +41,7 @@ module.exports = function (eleventyConfig) {
4141
eleventyConfig.addShortcode("keywords", (pkg) => buildKeywords(pkg));
4242
eleventyConfig.addShortcode("version", (pkg) => buildVersionTag(pkg));
4343

44+
eleventyConfig.addShortcode("reference", (url, num) => `<a href="${url}" class="reference" target="_blank" rel="noopener noreferrer">[${num}]</a>`);
4445

4546
eleventyConfig.setLibrary("njk", new Nunjucks.Environment(
4647
new Nunjucks.FileSystemLoader("./"),

package.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/content/collections/til/eleventy-package.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ tags: ["eleventy"]
44
eleventyNavigation:
55
key: "eleventy-package"
66
keywords: ["eleventy", "package.json", "shortcodes", "package", "json"]
7+
created: 2024-08-24
78
---
89

910
for a long time now, I've been using a custom shortcode in Eleventy through the

src/content/collections/til/ios-event-bubbling.md

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ tags: ["ios", "html", "javascript"]
44
eleventyNavigation:
55
key: "ios-event-bubbling"
66
keywords: ["ios", "html", "javascript", "event bubbling", "event delegation", "event propagation", "event handling"]
7+
created: 2024-08-24
78
---
89

910
recently, while preparing the 22.0 release of this website, I noticed that the
@@ -29,10 +30,14 @@ sections.forEach(({ button, section }) => {
2930
});
3031
```
3132

32-
after some investigation (and Claude's help), I found out that the issue was
33-
related to [*event bubbling*](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#event_bubbling_and_capture). By simply moving the event listener from the
34-
`window` object to the `document` object and flipping the loop to iterate over
35-
the sections last, the issue was fixed.
33+
after some investigation
34+
{% reference 'https://www.quirksmode.org/blog/archives/2010/09/click_event_delegation.html', '1' %}
35+
{% reference 'https://www.sitepoint.com/community/t/handling-click-in-the-safari-browser/417837', '2' %}
36+
(and Claude's help), I found out that the issue was related to
37+
[*event bubbling*](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#event_bubbling_and_capture).
38+
By simply moving the event listener from the `window` object to the `document`
39+
object and flipping the loop to iterate over the sections last, the issue was
40+
fixed.
3641

3742
here is the updated code that works on all platforms:
3843

@@ -48,12 +53,3 @@ better compatibility, and the mentioned change of the loop order, avoiding the
4853
addition of separate event listeners.
4954

5055
*Thanks, Claude!*
51-
52-
<br>
53-
54-
### references
55-
56-
- [Apple Developer Documentation](https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html)
57-
- [MDN: Event Bubbling and Capture](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#event_bubbling_and_capture)
58-
- [QuircksMode: Click event delegation on iOS](https://www.quirksmode.org/blog/archives/2010/09/click_event_delegation.html)
59-
- [SitePoint: Handling .click() in the Safari browser](https://www.sitepoint.com/community/t/handling-click-in-the-safari-browser/417837)

src/content/collections/til/pdf-count.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ tags: ["puppeteer", "pdf"]
44
eleventyNavigation:
55
key: "pdf-count"
66
keywords: ["puppeteer", "pdf", "page count", "number of pages", "chromium", "wkhtmltopdf"]
7+
created: 2024-08-15
78
---
89

910
in Okticket, we are building a serverless service that generates PDF from HTML.

src/static/js/building/tilling.js

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
1+
const maxTagSize = 1.25
2+
const minTagSize = 0.75
3+
14
function buildTagWall(data) {
25
const tags = {}
36

47
data.forEach((element) => {
58
element.data.tags.forEach((tag) => {
6-
if (tags[tag]) { tags[tag] += 1 }
7-
else { tags[tag] = 1 }
9+
if (!tags[tag]) { tags[tag] = {} }
10+
if (tags[tag].count) { tags[tag].count += 1 }
11+
else { tags[tag].count = 1 }
812
})
913
})
1014

11-
delete tags.til
15+
delete tags.til // remove the 'til' tag from the list
16+
17+
// calculate size by count.
18+
const maxCount = Math.max(...Object.values(tags).map((tag) => tag.count))
19+
const minCount = Math.min(...Object.values(tags).map((tag) => tag.count))
20+
Object.keys(tags).forEach((tag) => {
21+
const size = (tags[tag].count - minCount) / (maxCount - minCount) * (maxTagSize - minTagSize) + minTagSize
22+
tags[tag].size = `font-size: ${size}em`
23+
})
1224

13-
// sort tags by count and then alphabetically
1425
Object.keys(tags).sort((a, b) => {
15-
if (tags[a] === tags[b]) { return a > b ? 1 : -1 }
16-
return tags[a] < tags[b] ? 1 : -1
26+
// sort by count and then by name
27+
if (tags[a].count > tags[b].count) { return -1 }
28+
if (tags[a].count < tags[b].count) { return 1 }
29+
if (a < b) { return -1 }
30+
if (a > b) { return 1 }
31+
return 0
1732
})
1833

1934
return Object.keys(tags).map((tag) => {
20-
// TODO: size the tags based on the count
2135
// TODO: make tags clickable to filter the entries
22-
return `<code class="tag">${tag}</code> <span class="count">${tags[tag]}</span>`
36+
return `<span class="tag" style="${tags[tag].size}"><code class="tag-name">${tag}</code> <span class="tag-count">${tags[tag].count}</span></span>`
2337
}).join(' ')
2438
}
2539

40+
2641
function getRecents(data) {
2742
return data.slice(-10).reverse().map((element) => buildEntry(element)).join(' ');
2843
}
@@ -43,7 +58,8 @@ function buildTitle(element) {
4358
return `<code>${tag}</code>`
4459
}).join(' ')
4560

46-
const date = extractDate(element.page.date) || ''
61+
62+
const date = extractDate(element.data.created) || ''
4763

4864
return `<div>
4965
<a href="${element.url}"><span class="til-card-title">${element.data.title}</span></a>
@@ -102,11 +118,16 @@ function getRelated(data, current) {
102118
}
103119

104120

105-
function buildTimestamps(element) {
121+
function buildTimestamps(data, element) {
106122
let target = element.inputPath.substring(1)
107123
target = `https://github.com/miermontoto/mier.info/tree/main${target}`
124+
125+
// find the element in the data array
126+
let fullElement = data.find((entry) => entry.page.fileSlug === element.fileSlug)
127+
const date = extractDate(fullElement.data.created)
128+
108129
return `
109-
<div class="timestamp"> created: <time datetime="${element.date}">${element.date}</time></div>
130+
<div class="timestamp"> created: <time datetime="${date}">${date}</time></div>
110131
<div class="source">source: <a href="${target}">${element.filePathStem}</a></div>
111132
`
112133
}

src/templates/til.njk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ layout: generic.njk
1515

1616
<hr>
1717

18-
<div id="til-timestamps">{% tilTimestamps page %}</div>
18+
<div id="til-timestamps">{% tilTimestamps collections.til, page %}</div>

0 commit comments

Comments
 (0)