-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.eleventy.js
265 lines (223 loc) · 9.76 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//const litPlugin = require('@lit-labs/eleventy-plugin-lit');
const sass = require("sass");
const path = require("node:path");
const lodash = require("lodash");
const markdownIt = require('markdown-it');
const mdRender = new markdownIt();
const markdownItAttrs = require('markdown-it-attrs')
const markdownItOptions = {
html: true,
breaks: true,
linkify: false
}
const markdownLib = markdownIt(markdownItOptions).use(markdownItAttrs).disable('code');
function sortByOrder(values) {
let vals = [...values]; // this *seems* to prevent collection mutation...
return vals.sort((a, b) => Math.sign(a.data.order - b.data.order));
}
module.exports = async function(eleventyConfig) {
const { EleventyI18nPlugin } = await import("@11ty/eleventy");
eleventyConfig.setQuietMode(true);
// Can be activated by running "SSR=1 npm start" on Unix systems
if (process.env.BUILD_MODE === 'production' || process.env.SSR) {
/*
eleventyConfig.setQuietMode(false);
console.log(`Activated SSR plugin`);
eleventyConfig.addPlugin(litPlugin, {
mode: 'worker',
componentModules: [
'./node_modules/@sbb-esta/lyne-elements/index.js',
'./node_modules/@sbb-esta/lyne-elements-experimental/index.js',
],
});*/
}
else {
//console.log(`SSR plugin not active`);
}
eleventyConfig.setLibrary('md', markdownLib)
eleventyConfig.addPassthroughCopy("src/assets/images");
eleventyConfig.addPassthroughCopy("src/assets/videos");
eleventyConfig.addPassthroughCopy("src/assets/js");
eleventyConfig.addPassthroughCopy("src/assets/fonts");
eleventyConfig.addPassthroughCopy("src/assets/downloads");
eleventyConfig.addPassthroughCopy({ 'src/robots.txt': '/robots.txt' });
eleventyConfig.addPassthroughCopy({ 'src/googlec598c9eee38cf153.html': '/googlec598c9eee38cf153.html' });
eleventyConfig.addPreprocessor("macro-inject", ".njk,.md", (data, content) => {
return `
{%- from "src/_includes/macros/macros.njk" import imageWithMode -%}\n
{%- from "src/_includes/macros/macros.njk" import imageOnGreyBackground -%}\n
{%- from "src/_includes/macros/macros.njk" import principleImage -%}\n
{%- from "src/_includes/macros/macros.njk" import svgImage -%}\n
{%- from "src/_includes/macros/macros.njk" import webpImage -%}\n
{%- from "src/_includes/macros/macros.njk" import buttonGroup -%}\n
{%- from "src/_includes/macros/macros.njk" import specificationLinks -%}\n
{%- from "src/_includes/macros/macros.njk" import imageSpec -%}\n
{%- from "src/_includes/macros/macros.njk" import lynePlayground -%}\n
{%- from "src/_includes/macros/macros.njk" import lyneExamples -%}\n
{%- from "src/_includes/macros/macros.njk" import lyneComponentLinks -%}\n
` + content;
});
eleventyConfig.addTemplateFormats("scss");
eleventyConfig.addExtension("scss", {
outputFileExtension: "css",
compile: function (inputContent, inputPath) {
let parsed = path.parse(inputPath);
if (parsed.name.startsWith("partial-")) {
return;
}
let result = sass.compileString(inputContent, {
loadPaths: [
parsed.dir || ".",
this.config.dir.includes,
"./",
"node_modules"
]
});
return (data) => {
return result.css;
};
}
});
eleventyConfig.addFilter("sortByOrder", sortByOrder);
eleventyConfig.addFilter("sortLyneComponentsByOrder", (obj) => {
const sorted = {};
Object.keys(obj)
.sort((a, b) => {
return obj[a].config.order > obj[b].config.order ? 1 : -1;
})
.forEach((name) => (sorted[name] = obj[name]));
return sorted;
});
// Custom filter to capitalize each word in a string
eleventyConfig.addFilter("capitalizeWords", function(value) {
if (typeof value !== 'string') {
return ''; // or handle the undefined case as needed
}
return value.replace(/\b\w/g, char => char.toUpperCase());
});
eleventyConfig.addFilter("lastOfArray", function(array) {return array.slice(-1);});
eleventyConfig.addFilter("absolutelinks", (post) => {
const content = post.replaceAll("](/docs/", "](https://lyne-storybook.app.sbb.ch/?path=/docs/").replaceAll("](/story/", "](https://lyne-storybook.app.sbb.ch/?path=/story/");
function wrapTablesWithSbbTableWrapper(content) {
//const tableRegex = /((?:\|[^|\n]*\|.*\n)+)/g;
const tableRegex = /(\|[^\n]+\|\n)(\|[^\n]+\|\n)*/g;
return content.replace(tableRegex, (match) => `<sbb-table-wrapper>\n\n${match}\n\n{.sbb-table}\n\n</sbb-table-wrapper>\n\n`);
}
const updatedContent = wrapTablesWithSbbTableWrapper(content);
return updatedContent;});
eleventyConfig.addFilter("defaultlanguagecontent", (array, currPage) => {
currPage = currPage.slice(3);
const pageArr = array.filter((page) => page.url == "/de"+currPage);
return pageArr;});
eleventyConfig.addPlugin(EleventyI18nPlugin, {defaultLanguage: "de",});
eleventyConfig.addFilter("include", (arr, path, value) => {
value = lodash.deburr(value).toLowerCase();
return arr.filter((item) => {
let pathValue = lodash.get(item, path);
pathValue = lodash.deburr(pathValue).toLowerCase();
if (pathValue.length == value.length && pathValue.includes(value) == true) {
return true;
} else {
return false;
}
});
});
eleventyConfig.addFilter("exclude", (arr, path, value) => {
value = lodash.deburr(value).toLowerCase();
return arr.filter((item) => {
let pathValue = lodash.get(item, path);
pathValue = lodash.deburr(pathValue).toLowerCase();
if (pathValue.length == value.length && pathValue.includes(value) == true) {
return false;
} else {
return true;
}
});
});
eleventyConfig.addFilter("removeLastPathSegment", function(url) {
if (typeof url !== 'string') return url;
// Entfernt alles nach dem letzten "/"
return url.substring(0, url.lastIndexOf('/'));
});
eleventyConfig.addFilter("lyneexample", (pattern) => {
const lyneStories = require('@sbb-esta/lyne-elements/dist/collection/storybundle');
const rawStories = lyneStories[pattern];
const stories = [];
let ignoreArgs = [];
const defaultExport = rawStories.default;
const defaultExportKeys = Object.keys(defaultExport);
if (defaultExportKeys.includes('documentation')) {
const docu = defaultExport.documentation;
const docuKeys = Object.keys(docu);
if (docuKeys.includes('disableArgs')) {
ignoreArgs = docu.disableArgs;
}
}
Object.keys(rawStories).forEach((key) => {
if (key !== 'default') {
const storyObject = {};
const story = rawStories[key];
const storyKeys = Object.keys(story);
// handle documentation key
if (storyKeys.includes('documentation')) {
storyObject.documentation = story.documentation;
} else {
storyObject.documentation = {};
}
// handle container key
const docuKeys = Object.keys(storyObject.documentation);
if (docuKeys.includes('container')) {
storyObject.documentation.container = story.documentation.container;
} else {
storyObject.documentation.container = {};
}
const unCamelCase = (string) =>
string.replace(/([a-z])([A-Z])/g, '$1 $2').replace(/\b([A-Z]+)([A-Z])([a-z])/, '$1 $2$3');
storyObject.documentation.title = unCamelCase(key);
// adopt styles
const containerKeys = Object.keys(storyObject.documentation.container);
if (containerKeys.includes('styles')) {
const rawStyles = storyObject.documentation.container.styles;
const stylesKeys = Object.keys(rawStyles);
let styles = '';
stylesKeys.forEach((styleKey) => {
const style = rawStyles[styleKey];
styles += `${styleKey}: ${style};`;
});
storyObject.documentation.container.styles = styles;
} else {
storyObject.documentation.container.styles = '';
}
if (story && Object.keys(story).includes('args')) {
storyObject.element = story.args;
// const rawElement = story.args;
/*
if (ignoreArgs.length > 0) {
ignoreArgs.forEach((arg) => {
rawElement.removeAttribute(arg);
});
}
*/
//storyObject.elementRaw = rawElement;
}
stories.push(storyObject);
}
});
return stories;
});
eleventyConfig.addFilter("renderUsingMarkdown", function(rawString) {
return markdownLib.render(rawString);
});
return {
templateFormats: ["njk", "md"],
markdownTemplateEngine: "njk",
htmlTemplateEngine: "njk",
dir: {
input: "src",
includes: "_includes",
layouts: "_layouts",
data: "_data",
output: "dist"
}
};
};