forked from ValeriyDyachenko/bxnotes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
149 lines (142 loc) · 5.82 KB
/
.eleventy.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 pluginSyntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const htmlmin = require("html-minifier");
const getNav = (collections, conspectName, title) => {
const postsCnt = collections['postInConspect:'+conspectName].length;
let postIndex = postsCnt;
while (postIndex--) {
const data = collections['postInConspect:'+conspectName][postIndex].data;
if (conspectName === data.conspect && title === data.title) {
const prev = collections['postInConspect:'+conspectName][postIndex - 1];
const next = collections['postInConspect:'+conspectName][postIndex + 1];
return {prev, next};
}
}
};
const getFirstArticleinConspect = (conspectName, collections) => collections[`postInConspect:${conspectName}`][1]; // index 0 is "about page"
module.exports = function(eleventyConfig) {
eleventyConfig.addFilter('htmlDate', (dateObj) => {
return new Date(dateObj.getTime() - (dateObj.getTimezoneOffset() * 60000 ))
.toISOString()
.split("T")[0];
});
eleventyConfig.addShortcode('home', () => `<a href="/"><span class="breadcrumbs-home-ico">🏠 </span> <span class="breadcrumbs-home-text">главная</span></a>`);
eleventyConfig.addShortcode('postPagination', (collections, conspectName, title) => {
const {prev, next} = getNav(collections, conspectName, title);
if (prev || next) {
return `<ul class="pagination-bottom">${next ? `<li class="next"><a href="${next.url}">${next.data.title} →</a></li>` : ''}${prev ? `<li class="prev"><a href="${prev.url}">← ${prev.data.title}</a></li>` : ''}</ul>`;
}
return ' ';
});
eleventyConfig.addShortcode('getSiteMenu', (collections, sectionActive, subjectActive, conspectActive) => {
if (!collections) {
throw Error('Collections is required.');
}
const sections = collections['section'];
let content = '<div class="sections">';
for (let secInd = 0, secLen = sections.length; secInd < secLen; secInd++) {
content += '<div class="section-block">';
const isSecChecked = sections[secInd].data.section === sectionActive ? 'checked' : '';
content += `
<input class="section-checkbox" id="section-${secInd}" type="checkbox" ${isSecChecked} />
<label class="section-item" for="section-${secInd}">${sections[secInd].data.title}</label>
`;
const nestedSubjects = collections[`subjectInSection:${sections[secInd].data.section}`];
if (nestedSubjects) {
content += '<div class="subject-block">';
for (let subInd = 0, subLen = nestedSubjects.length; subInd < subLen; subInd++) {
const isSubChecked = nestedSubjects[subInd].data.subject === subjectActive ? 'checked' : '';
content += `
<input class="subject-checkbox" id="subject-${secInd}-${subInd}" type="checkbox" ${isSubChecked} />
<label class="subject-item" for="subject-${secInd}-${subInd}">${nestedSubjects[subInd].data.title}</label>
`;
const nestedConspects = collections[`conspectInSubject:${nestedSubjects[subInd].data.subject}`];
if (nestedConspects) {
content += '<div class="conspect-block">';
for (let conInd = 0, conLen = nestedConspects.length; conInd < conLen; conInd++) {
const isConActive = nestedConspects[conInd].data.conspect === conspectActive ? 'active' : '';
const conspectName = nestedConspects[conInd].data.conspect;
const firstConspectArticle = getFirstArticleinConspect(conspectName, collections);
content += `<a class="conspect-item ${isConActive}" href="${firstConspectArticle.url}">${nestedConspects[conInd].data.conspectTitle}</a>`;
}
content += '</div>';
}
}
content += '</div>';
}
content += '</div>';
}
content += '</div>';
return `
<div class="site-menu">
<div class="site-menu__container">
${content}
</div>
</div>
`;
});
eleventyConfig.addShortcode('getMainConspectBlocks', (collections) => {
let layout = ``;
collections['subject'].forEach(subjectElement => {
const subjectName = subjectElement.data.subject;
layout += `
<div class="block">
<h2>${subjectName}</h2>
<ul class="base-list">
`;
collections[`conspectInSubject:${subjectName}`].forEach(conspectElement => {
const conspectName = conspectElement.data.conspect;
const firstConspectArticle = getFirstArticleinConspect(conspectName, collections);
layout += `
<li>
<a href="${firstConspectArticle.url}">${conspectElement.data.conspectTitle}</a>
</li>
`;
});
layout += `
</ul>
</div>
`;
});
return layout;
});
eleventyConfig.addPlugin(pluginSyntaxHighlight);
let markdownIt = require("markdown-it");
let markdownItAnchor = require("markdown-it-anchor");
let markdownItEmoji = require("markdown-it-emoji");
let options = {
html: true,
breaks: true,
linkify: true
};
let opts = {
permalink: true,
permalinkClass: "direct-link",
permalinkSymbol: "#",
level: [2],
permalinkBefore: true,
};
eleventyConfig.setLibrary("md", markdownIt(options)
.use(markdownItAnchor, opts).use(markdownItEmoji)
);
eleventyConfig.addPassthroughCopy("_server/assets");
eleventyConfig.addTransform("htmlmin", function(content, outputPath) {
if( outputPath.endsWith(".html") ) {
let minified = htmlmin.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true
});
return minified;
}
return content;
});
return {
templateFormats : ['njk', 'md'],
markdownTemplateEngine: false,
passthroughFileCopy: true,
dir: {
input: "_server",
output: "_server/_site",
}
};
};