Repository navigation
Expand file tree
/
Copy patheleventy.config.js
More file actions
136 lines (121 loc) · 4.55 KB
/
Copy patheleventy.config.js
File metadata and controls
136 lines (121 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import fs from 'fs';
import syntaxHighlight from "@11ty/eleventy-plugin-syntaxhighlight";
import markdownit from "markdown-it";
import anchor from "markdown-it-anchor";
const VERSION = '0.4.2';
export default function (eleventyConfig) {
// Get utility sections
// Those can be retrieved from the single _utilities.css file.
// Every section is a single line CSS comment
const slothUtilitySections = [];
fs.readFileSync('src/_utilities.css').toString().split("\n").filter((l) => l.startsWith('/*')).forEach((section) => {
slothUtilitySections.push(section.trim().slice(2, -2).trim().toLowerCase());
});
// Get components
// Those can be retrieved by all files existing in the components/ directory
const slothComponents = [];
fs.readdirSync('src/components').forEach(file => {
const slug = file.slice(1, -4);
slothComponents.push({
slug: slug,
js: ['tab', 'toast'].includes(slug),
});
});
// Init plugins
eleventyConfig.addPlugin(syntaxHighlight);
eleventyConfig.setLibrary("md", markdownit().set({ html: true }).use(anchor));
// Build a table of contents from h2/h3 headings with ids (set by markdown-it-anchor)
eleventyConfig.addFilter('toc', (content) => {
const headings = [];
const headingRegex = /<(h2|h3)([^>]*)>([\s\S]*?)<\/\1>/g;
let match;
while ((match = headingRegex.exec(content))) {
const [, tag, attrs, inner] = match;
const idMatch = attrs.match(/\sid="([^"]*)"/);
if (!idMatch) continue;
headings.push({
tag,
id: idMatch[1],
text: inner.replace(/<[^>]+>/g, ''),
children: [],
});
}
if (headings.length === 0) return '';
const tree = [];
let currentH2 = null;
headings.forEach((heading) => {
if (heading.tag === 'h2') {
tree.push(heading);
currentH2 = heading;
} else if (currentH2) {
currentH2.children.push(heading);
} else {
tree.push(heading);
}
});
const renderList = (items) => {
if (items.length === 0) return '';
const listItems = items.map((heading) =>
`<li><a href="#${heading.id}">${heading.text}</a>${renderList(heading.children)}</li>`
);
return `<ul>${listItems.join('')}</ul>`;
};
return `<nav class="toc">${renderList(tree)}</nav>`;
});
// Build search index
const index = [];
const docsSrc = 'docs/pages/';
const dirs = fs.readdirSync(docsSrc, { withFileTypes: true }).filter((e) => e.isDirectory()).map((d) => d.name);
dirs.forEach((d) => {
fs.readdirSync(`${docsSrc}${d}`).forEach(file => {
const name = file.slice(0, -3);
const meta = fs.readFileSync(`${docsSrc}${d}/${file}`).toString().split("\n", 5);
const title = meta.filter((l) => l.startsWith('title:')).join().slice(6).trim();
const text = meta.filter((l) => l.startsWith('description:')).join().slice(12).trim();
const tags = meta.filter((l) => l.startsWith('tags:')).join().slice(5).trim();
index.push({
title: title,
name: name,
url: `/${d}/${name}`,
text: text,
tags: tags,
cat: d
});
});
});
// Sort entries alphabetically by title
index.sort((a, b) => a.title.localeCompare(b.title));
// Write search index to file
fs.mkdirSync('docs/public/assets/scripts', { recursive: true })
fs.writeFileSync('docs/public/assets/scripts/searchIndex.json', JSON.stringify(index));
// Global data
eleventyConfig.addGlobalData('baseUrl', 'https://slothcss.devmount.com');
eleventyConfig.addGlobalData('repoUrl', 'https://github.com/devmount/sloth.css');
eleventyConfig.addGlobalData('cdnUrl', 'https://unpkg.com/@devmount/sloth.css@^0');
eleventyConfig.addGlobalData('creatorUrl', 'https://devmount.com');
eleventyConfig.addGlobalData('donationUrl', 'https://paypal.me/devmount');
eleventyConfig.addGlobalData('layout', 'default');
eleventyConfig.addGlobalData('meta', {
title: 'Sloth.css',
version: VERSION,
description: 'A drop-in utility component CSS library for lazy mammals.',
utilitySections: slothUtilitySections,
components: slothComponents,
});
// Layout aliases
eleventyConfig.addLayoutAlias('default', 'default.njk');
// Get sloth.css
eleventyConfig.addPassthroughCopy({ 'docs/assets': 'assets'});
eleventyConfig.addPassthroughCopy({ src: 'assets/styles' });
// 11ty config
return {
dir: {
input: "docs/pages",
output: "docs/public",
includes: "../_includes",
},
// Nunjucks instead of Liquid for markdown
markdownTemplateEngine: 'njk',
templateEngineOverride: ['njk'],
}
};