Skip to content

Commit e1663d4

Browse files
committed
docs: generate todos without writing a file
1 parent 852d4bc commit e1663d4

File tree

2 files changed

+68
-51
lines changed

2 files changed

+68
-51
lines changed

.eleventy.cjs

Lines changed: 25 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,34 @@ const directoryOutputPlugin = require('@11ty/eleventy-plugin-directory-output');
66
const pfeAssetsPlugin = require('./docs/_plugins/pfe-assets.cjs');
77
const customElementsManifestPlugin = require('./docs/_plugins/custom-elements-manifest.cjs');
88
const orderTagsPlugin = require('./docs/_plugins/order-tags.cjs');
9+
const todosPlugin = require('./docs/_plugins/todos.cjs');
910

1011
const markdownIt = require('markdown-it');
1112
const markdownItAnchor = require('markdown-it-anchor');
1213

1314
const pluginToc = require('eleventy-plugin-toc');
1415

15-
module.exports = function(eleventyConfig) {
16-
const markdownLib = markdownIt({ html: true })
17-
.use(markdownItAnchor);
16+
const markdownLib = markdownIt({ html: true })
17+
.use(markdownItAnchor);
1818

19+
module.exports = function(eleventyConfig) {
1920
eleventyConfig.setLibrary('md', markdownLib);
2021

2122
eleventyConfig.setQuietMode(process.env.npm_config_quiet);
23+
2224
eleventyConfig.setWatchThrottleWaitTime(500);
2325

24-
/** Table of Contents Shortcode */
25-
eleventyConfig.addPlugin(pluginToc, {
26-
tags: ['h2', 'h3', 'h4'],
26+
eleventyConfig.setBrowserSyncConfig({
27+
open: 'local',
28+
server: {
29+
baseDir: '_site',
30+
middleware: [compress()],
31+
},
2732
});
2833

34+
/** Table of Contents Shortcode */
35+
eleventyConfig.addPlugin(pluginToc, { tags: ['h2', 'h3', 'h4'] });
36+
2937
/** Copy and manage site assets from the monorepo */
3038
eleventyConfig.addPlugin(pfeAssetsPlugin);
3139

@@ -38,10 +46,19 @@ module.exports = function(eleventyConfig) {
3846
/** Collections to organize by order instead of date */
3947
eleventyConfig.addPlugin(orderTagsPlugin, { tags: ['develop'] });
4048

41-
eleventyConfig.addFilter('prettyDate', function(dateStr) {
42-
return new Intl.DateTimeFormat('en-US', { dateStyle: 'medium' }).format(new Date(dateStr));
49+
/** list todos */
50+
eleventyConfig.addPlugin(todosPlugin);
51+
52+
/** format date strings */
53+
eleventyConfig.addFilter('prettyDate', function(dateStr, options = {}) {
54+
const { dateStyle = 'medium' } = options;
55+
return new Intl.DateTimeFormat('en-US', { dateStyle })
56+
.format(new Date(dateStr));
4357
});
4458

59+
/** fancy syntax highlighting with diff support */
60+
eleventyConfig.addPlugin(syntaxHighlight);
61+
4562
/** Add IDs to heading elements */
4663
eleventyConfig.addPlugin(anchorsPlugin, {
4764
formatter(element, existingids) {
@@ -59,9 +76,6 @@ module.exports = function(eleventyConfig) {
5976
},
6077
});
6178

62-
eleventyConfig.addPlugin(syntaxHighlight);
63-
64-
eleventyConfig.setQuietMode(true);
6579
eleventyConfig.addPlugin(directoryOutputPlugin, {
6680
// Customize columns
6781
columns: {
@@ -73,46 +87,6 @@ module.exports = function(eleventyConfig) {
7387
warningFileSize: 400 * 1000,
7488
});
7589

76-
eleventyConfig.on('eleventy.before', async function generateTodos() {
77-
const start = performance.now();
78-
const fs = require('fs/promises');
79-
const leasot = require('leasot');
80-
const path = require('path');
81-
const { promisify } = require('util');
82-
const glob = promisify(require('glob'));
83-
84-
const todos = [];
85-
86-
const files = await glob('{elements,core,tools}/**/*[!.spec].ts', {
87-
ignore: '**/node_modules/**/*',
88-
});
89-
90-
for (const filename of files) {
91-
const contents = await fs.readFile(filename, 'utf8');
92-
const extension = path.extname(filename);
93-
const parsed = leasot.parse(contents, { extension, filename });
94-
const output = leasot.report(parsed, 'raw');
95-
todos.push(...output);
96-
}
97-
98-
await fs.writeFile(
99-
path.join(__dirname, 'docs', '_data', 'todos.json'),
100-
JSON.stringify(todos, null, 2),
101-
'utf8'
102-
);
103-
const end = performance.now();
104-
// eslint-disable-next-line no-console
105-
console.log(`Generated TODOs in ${(end - start) / 1000}s`);
106-
});
107-
108-
eleventyConfig.setBrowserSyncConfig({
109-
open: 'local',
110-
server: {
111-
baseDir: '_site',
112-
middleware: [compress()],
113-
},
114-
});
115-
11690
return {
11791
dir: {
11892
input: './docs',

docs/_plugins/todos.cjs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const fs = require('fs/promises');
2+
const leasot = require('leasot');
3+
const path = require('path');
4+
const { promisify } = require('util');
5+
const glob = promisify(require('glob'));
6+
7+
const cache = new Map();
8+
9+
module.exports = function(eleventyConfig) {
10+
eleventyConfig.addGlobalData('todos', async function generateTodos() {
11+
const start = performance.now();
12+
13+
const todos = [];
14+
15+
const files = await glob('{elements,core,tools}/**/*[!.spec].ts', {
16+
ignore: '**/node_modules/**/*',
17+
});
18+
19+
const cacheKey = files.join('--');
20+
const cached = cache.get(cacheKey);
21+
22+
if (cached) {
23+
return cached;
24+
}
25+
26+
for (const filename of files) {
27+
const contents = await fs.readFile(filename, 'utf8');
28+
const extension = path.extname(filename);
29+
const parsed = leasot.parse(contents, { extension, filename });
30+
const output = leasot.report(parsed, 'raw');
31+
todos.push(...output);
32+
}
33+
34+
cache.set(cacheKey, todos);
35+
36+
const end = performance.now();
37+
38+
// eslint-disable-next-line no-console
39+
console.log(`[TODOs] Generated in ${(end - start) / 1000}s`);
40+
41+
return todos;
42+
});
43+
};

0 commit comments

Comments
 (0)