-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.eleventy.js
125 lines (111 loc) · 3.23 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
const lunr = require("lunr");
const markdownit = require("markdown-it");
const CleanCSS = require("clean-css");
module.exports = function (eleventyConfig) {
eleventyConfig.addPassthroughCopy("src/css");
eleventyConfig.addPassthroughCopy("src/assets");
eleventyConfig.addPassthroughCopy("src/js");
eleventyConfig.addPassthroughCopy("_headers");
eleventyConfig.addCollection("searchable", function (collectionApi) {
const data = [];
const searchableCollection = collectionApi.getFilteredByTag("proyecto");
searchableCollection.forEach((el) => {
data.push({
id: el.fileSlug,
url: el.url,
title: el.data.title,
subtitle: el.data.subtitle,
image: el.data.image,
client: el.data.client,
ods: el.data.ods,
categories: el.data.categories,
content: el.template._frontMatter.content,
});
});
const idx = lunr(function () {
this.ref("id");
this.field("url");
this.field("title");
this.field("subtitle");
this.field("image");
this.field("client");
this.field("ods");
this.field("categories");
this.field("content");
data.forEach((el) => {
this.add({ ...el });
});
});
let dataMap = {};
data.forEach((el) => {
dataMap[el.id] = el;
});
// write idx to JSON file
require("fs").writeFileSync(
"dist/js/search_index.json",
JSON.stringify({ index: idx, elements: dataMap })
);
return data;
});
eleventyConfig.addFilter("categoryFilter", function (collection, category) {
if (!category) return collection;
const filtered = collection.filter((item) =>
item.data.categories.includes(category)
);
return filtered;
});
eleventyConfig.addFilter(
"excludeServiceFilter",
function (collection, service) {
if (!service) return collection;
const filtered = collection.filter(
(item) => !item.data.title.includes(service)
);
return filtered;
}
);
eleventyConfig.addFilter("formattedDate", function (value) {
const date = new Date(value);
return date.toLocaleDateString("es-ES", {
year: "numeric",
month: "long",
day: "numeric",
});
});
let options = {
html: true,
breaks: true,
linkify: true,
};
const markdownIt = markdownit(options);
// Remember the old renderer if overridden, or proxy to the default renderer.
var defaultRender =
markdownIt.renderer.rules.link_open ||
function (tokens, idx, options, env, self) {
return self.renderToken(tokens, idx, options);
};
markdownIt.renderer.rules.link_open = function (
tokens,
idx,
options,
env,
self
) {
// Add a new `target` attribute, or replace the value of the existing one.
tokens[idx].attrSet("target", "_blank");
// Pass the token to the default renderer.
return defaultRender(tokens, idx, options, env, self);
};
eleventyConfig.setLibrary("md", markdownIt);
eleventyConfig.addFilter("cssmin", function (code) {
return new CleanCSS({}).minify(code).styles;
});
return {
dir: {
input: "src",
output: "dist",
includes: "layouts", // relative to dir.input
},
passthroughFileCopy: true,
};
};