-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheleventy.config.js
149 lines (125 loc) · 4.18 KB
/
eleventy.config.js
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
137
138
139
140
141
142
143
144
145
146
147
148
149
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const markdownIt = require("markdown-it");
const markdownItAnchor = require("markdown-it-anchor");
const dirTree = require('directory-tree');
const fs = require('fs');
const matter = require('gray-matter');
const yaml = require("js-yaml");
module.exports = function(eleventyConfig) {
const inputDir = 'src/content';
eleventyConfig.addPassthroughCopy({'src/assets' : '/'});
eleventyConfig.addLayoutAlias('default', 'default.liquid');
eleventyConfig.addLayoutAlias('home', 'home.liquid');
eleventyConfig.addLayoutAlias('raw', 'raw.liquid');
// Adds custom attributes to generated code tags
// (you can also add to the generated pre tags)
eleventyConfig.addPlugin(syntaxHighlight, {
codeAttributes: {
class: 'js-code-content',
}
});
eleventyConfig.addShortcode("table", function(file, page) {
const md = new markdownIt();
const dataDir = [...page.inputPath.split('/')];
dataDir.pop();
const jsonFile = fs.readFileSync(`${dataDir.join('/')}/${file}.json`, 'utf8');
const data = JSON.parse(jsonFile);
const caption = `\t<caption>${data.tableName}</caption>`;
const tableHead = () => {
let thead = `\t<thead>\n\t\t<tr>\n`;
data.headings.forEach(item => {
thead += `\t\t\t<th width="${item.width}">${item.name}</th>\n`;
});
thead += '\t\t</tr>\n\t</thead>';
return thead;
}
const tableBody = () => {
let tbody = '\t<tbody>\n';
for (let i = 0; i < data.rows.length; i++) {
let keys = Object.keys(data.rows[i])
tbody += `\t\t<tr>\n`;
keys.forEach(key => {
tbody += `\t\t\t<td>${md.renderInline(data.rows[i][key])}</td>\n`;
});
tbody += '\t\t</tr>\n';
}
tbody += '\t</tbody>';
return tbody;
}
const render = `<table>\n${caption}\n${tableHead()}\n${tableBody()}\n</table>`;
return render;
});
eleventyConfig.addGlobalData('navigation', function() {
const collectionDirectory = `${inputDir}/docs`
const dirCollection = dirTree(collectionDirectory, {attributes: ['size','type','extension'], extensions: /\.(md|html)$/ });
const addOrderToData = (data) => {
if (data.type === 'file') {
const str = fs.readFileSync(data.path, 'utf8');
const frontmatter = matter(str).data;
data.permalink = data.path.replace(inputDir, '');
data.index = (data.name.replace(data.extension, '') === 'index') ? true : false;
data.title = (frontmatter.title) ? frontmatter.title : data.name;
data.order = (frontmatter.order) ? frontmatter.order : 100000;
}
if (data.type === 'directory') {
data.order = 0;
data.children.forEach(child => {
if (child.name.replace(child.extension, '') === 'index') {
const childString = fs.readFileSync(child.path, 'utf8');
const childMatter = matter(childString).data;
data.permalink = data.path.replace(inputDir, '');
data.index = (data.name.replace(data.extension, '') === 'index') ? true : false;
data.title = (childMatter.title) ? childMatter.title : child.name;
data.order = (childMatter.order) ? childMatter.order : 100000;
}
});
}
if (data.children) {
data.children.forEach(child => {
addOrderToData(child)
});
}
return data;
}
// New Data Object with the `order` and `name`
const newData = addOrderToData(dirCollection);
// Sorts the data object by `order`
const sortData = (a,b) => {
if (a.order < b.order) {
return -1;
}
if (a.order > b.order) {
return 1;
}
return 0;
}
return (newData.children).sort(sortData);
});
// Markdown overrides:
// all heading levels 2-6 will automatically be given
// an id that can be used for jumplink href's
let markdownLibrary = markdownIt({
html: true,
breaks: false,
linkify: true
}).use(markdownItAnchor, {
tabIndex: false,
uniqueSlugStartIndex: 1,
level: [2, 3, 4, 5, 6],
permalink: markdownItAnchor.permalink.headerLink({
class:"cmp-page-link",
safariReaderFix: true
})
});
eleventyConfig.setLibrary("md", markdownLibrary);
eleventyConfig.addDataExtension("yaml", contents => yaml.load(contents));
return {
dir: {
htmlTemplateEngine: "liquid",
input: inputDir,
output: "dist",
includes: "../partials",
layouts: "../layouts"
}
}
};