Skip to content

Commit 3e3903f

Browse files
committed
22.0 RC1
[til] - full fledged implementation of TILs, using old writeups base - two initial entries: pdf-count, eleventy-package - supporting js scripts and sass styles [styles] - split files for global styles - use highlight.js for code syntax highlighting - other minor style changes [templates] - phase out `css` and `js` magic tags - allow HTML content in titles and subtitles - other base template changes - new til template [.eleventy.js] - use `pkg` data value to import `package.json` values (and dependant changes)
1 parent 7b5ac39 commit 3e3903f

30 files changed

Lines changed: 1715 additions & 176 deletions

.eleventy.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ const Nunjucks = require("nunjucks");
66
const buildBreadcrumbs = require('./src/static/js/building/breadcrumbs.js');
77
const buildTreemap = require('./src/static/js/building/treemap.js');
88
const buildChangelog = require('./src/static/js/building/changelog.js');
9+
const { buildTagWall, getRecents, getRelated, buildTimestamps } = require('./src/static/js/building/tilling.js');
910
const addAsset = require('./src/static/js/building/linking.js');
1011
const { qka, quizButtons, quizQuestions } = require('./src/static/js/building/quizzing.js');
12+
const { buildVersionTag, buildKeywords } = require("./src/static/js/building/package.js");
1113

1214
module.exports = function (eleventyConfig) {
1315
eleventyConfig.addPlugin(EleventyRenderPlugin);
@@ -20,31 +22,24 @@ module.exports = function (eleventyConfig) {
2022
eleventyConfig.setQuietMode(true);
2123
eleventyConfig.setServerOptions({ watch: ["src/static/css/**"], port: 8088 });
2224

25+
eleventyConfig.addShortcode("breadcrumbs", function (navPages) { return buildBreadcrumbs(navPages, this.page) });
26+
eleventyConfig.addShortcode("treemap", function (navPages) { return buildTreemap(navPages) });
27+
eleventyConfig.addShortcode("changelog", (data, pkg) => buildChangelog(data, pkg));
28+
29+
eleventyConfig.addShortcode("tilTags", (data) => buildTagWall(data));
30+
eleventyConfig.addShortcode("tilRecents", (data) => getRecents(data));
31+
eleventyConfig.addShortcode("tilRelated", (data, current) => getRelated(data, current));
32+
eleventyConfig.addShortcode("tilTimestamps", (element) => buildTimestamps(element) );
2333

24-
eleventyConfig.addShortcode("breadcrumbs", function(navPages) { return buildBreadcrumbs(navPages, this.page) });
25-
eleventyConfig.addShortcode("treemap", function(navPages) { return buildTreemap(navPages) });
26-
eleventyConfig.addShortcode("changelog", (data) => buildChangelog(data));
2734
eleventyConfig.addShortcode("qka", (data) => qka(data));
2835
eleventyConfig.addShortcode("quizButtons", (json) => quizButtons(json));
2936
eleventyConfig.addShortcode("quizQuestions", (json) => quizQuestions(json));
37+
3038
eleventyConfig.addShortcode("addScript", (filename) => addAsset("script", filename));
3139
eleventyConfig.addShortcode("addStyle", (filename) => addAsset("style", filename));
3240

33-
eleventyConfig.addShortcode("keywords", () => {
34-
let json = require('./package.json');
35-
if (!json.keywords) {
36-
console.log('keywords not found in package.json');
37-
return '';
38-
}
39-
return `<meta name="keywords" content="${json.keywords.join(', ')}">`;
40-
});
41-
42-
eleventyConfig.addShortcode("version", () => {
43-
let json = require('./package.json');
44-
let version = json.version;
45-
let channel = json.channel && json.channel !== 'RTW' ? ` (${json.channel})` : '';
46-
return `<a id="version-tag" href="/changelog/">${version}${channel}</a>`;
47-
});
41+
eleventyConfig.addShortcode("keywords", (pkg) => buildKeywords(pkg));
42+
eleventyConfig.addShortcode("version", (pkg) => buildVersionTag(pkg));
4843

4944

5045
eleventyConfig.setLibrary("njk", new Nunjucks.Environment(

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ assets/** linguist-generated
44
src/content/data/changelog.json linguist-generated
55
pnpm-lock.yaml linguist-generated
66
*.svg linguist-generated
7+
*.umd.js linguist-generated

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/changelog.njk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ eleventyNavigation:
77
---
88

99
{% addStyle "changelog" %}
10-
{% changelog changelog %}
10+
{% changelog changelog, pkg %}
1111
{% include 'src/templates/snippets/top.njk' %}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
title: til
3+
emoji: ❔
4+
desc: collection of general questions I've asked myself over time
5+
subtitle: inspired by <a href="https://til.simonwillison.net/" target="_blank">Simon Willison's TIL</a>
6+
icons: [eleventy, markdown]
7+
permalink: /til/
8+
eleventyNavigation:
9+
key: til
10+
---
11+
12+
{% addStyle 'til' %}
13+
14+
<div id="til-tags">
15+
<h2>tags</h2>
16+
{% tilTags collections.til %}
17+
</div>
18+
19+
<hr>
20+
21+
<div id="til-recents">
22+
<h2>recent TILs</h2>
23+
{% tilRecents collections.til %}
24+
</div>

src/content/collections/projects/writeups.njk

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
title: "accessing <code>package.json</code> in Eleventy shortcodes"
3+
tags: ["eleventy"]
4+
eleventyNavigation:
5+
key: "eleventy-package"
6+
keywords: ["eleventy", "package.json", "shortcodes", "package", "json"]
7+
---
8+
9+
For a long time now, I've been using a custom shortcode in Eleventy through the
10+
configuration file (`.eleventy.js`) to access the `package.json` file. This is
11+
useful for various reasons, such as displaying the version number of the package
12+
or the using the package's keywords in the metadata of the website.
13+
14+
Because I was directly accessing the `package.json` file, I had to import it
15+
using `require`:
16+
17+
```js
18+
let json = require('./package.json');
19+
```
20+
21+
This works fine, but restricts the definition of the shortcode to the config
22+
file itself, which is not ideal. I wanted to separate the shortcode from the
23+
config file so that it's easier to manage and maintain.
24+
25+
```js
26+
eleventyConfig.addShortcode("version", () => {
27+
let json = require('./package.json');
28+
let version = json.version;
29+
let channel = json.channel && json.channel !== 'RTW' ? ` (${json.channel})` : '';
30+
return `<a id="version-tag" href="/changelog/">${version}${channel}</a>`;
31+
});
32+
```
33+
34+
While browsing the Eleventy documentation, I came across the
35+
*Eleventy Supplied Data*. In it, I found that the content of the `package.json`
36+
file is available through the `pkg` data value in the template files. This
37+
means that I can access the `package.json` file directly in the template files
38+
without having to import it in the configuration file:
39+
40+
```
41+
{% raw %}{% pkg.version %}{% endraw %}
42+
```
43+
44+
This is a much cleaner way to access the version number of the package, and as
45+
such I can migrate the function for the shortcode to a separate script file:
46+
47+
```js
48+
const { buildVersionTag, buildKeywords } = require("./src/static/js/building/package.js");
49+
50+
module.exports = function(eleventyConfig) {
51+
// ...
52+
eleventyConfig.addShortcode("version", (pkg) => buildVersionTag(pkg));
53+
eleventyConfig.addShortcode("keywords", (pkg) => buildKeywords(pkg));
54+
// ...
55+
};
56+
```
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
title: "puppeteer: getting the number of pages in a PDF"
3+
tags: ["puppeteer", "pdf"]
4+
eleventyNavigation:
5+
key: "pdf-count"
6+
keywords: ["puppeteer", "pdf", "page count", "number of pages", "chromium", "wkhtmltopdf"]
7+
---
8+
9+
In Okticket, we are building a serverless service that generates PDF from HTML.
10+
This feature was previously implemented using `wkhtmltopdf`,
11+
but we are migrating to Puppeteer for better performance and stability, as
12+
the previous engine is severely outdated and clogs up the API when under heavy
13+
load.
14+
15+
the first step of this transition is using Puppeteer to render the PDF from the
16+
HTML content generated by the API using Blade templates. In this process, some
17+
specific features are required, such as the ability to get the number of pages
18+
in the generated PDF.
19+
20+
[After a quick search](https://github.com/puppeteer/puppeteer/issues/9550),
21+
the seemingly most straightforward way to get the number of pages in a PDF
22+
generated by Puppeteer is to use a pdf library to read the file and count the
23+
pages. However, this approach is far from ideal, as we would need to render the
24+
PDF twice: once to get the number of pages and once to generate the final PDF
25+
with the page count.
26+
27+
```typescript
28+
const pdfBuffer = await page.pdf({ ... });
29+
30+
const pdfDoc = await PDFDocument.load(pdfBuffer);
31+
const pageCount = pdfDoc.getPages().length;
32+
this.logger.debug(`PDF has ${pageCount} pages`);
33+
```
34+
35+
However, [Puppeteer has built-in style classes](https://github.com/puppeteer/puppeteer/issues/5345)
36+
that inject certain information in the PDF, including the page count:
37+
38+
- <b><code>pageNumber</code></b>: current page number.
39+
- <b><code>totalPages</code></b>: total number of pages in the PDF.
40+
- <code>date</code>: current date.
41+
- <code>title</code>: title of the document.
42+
- <code>url</code>: URL of the document.
43+
44+
Replacing the built-in classes of `wkhtmltopdf` with Puppeteer's classes in the
45+
blade templates allows us to get the page count directly from the PDF without
46+
the need to render it twice, cutting the processing time in half!
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"eleventyNavigation": {"parent": "til"},
3+
"layout": "til.njk",
4+
"permalink": "/til/{{ page.fileSlug }}/",
5+
"tags": ["til"]
6+
}

src/content/collections/webutils/uuid.njk

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ eleventyNavigation:
77
---
88

99
<div>
10-
<div id="uuid-options">
11-
<span class="label">version</span>
12-
<select id="uuid-version">
13-
<option value="v1">v1</option>
14-
<option value="v4" selected>v4</option>
15-
<option value="v7">v7</option>
16-
<option value="guid">guid</option>
17-
</select>
18-
</div>
19-
<span class=button id="uuid-generate">generate</span>
20-
<span class=button id="uuid-copy">copy</span>
21-
<span class="output" id="uuid-output"></span>
10+
<div id="uuid-options">
11+
<span class="label">version</span>
12+
<select id="uuid-version">
13+
<option value="v1">v1</option>
14+
<option value="v4" selected>v4</option>
15+
<option value="v7">v7</option>
16+
<option value="guid">guid</option>
17+
</select>
18+
</div>
19+
<span class=button id="uuid-generate">generate</span>
20+
<span class=button id="uuid-copy">copy</span>
21+
<span class="output" id="uuid-output"></span>
2222
</div>

0 commit comments

Comments
 (0)