-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
executable file
·205 lines (165 loc) · 5.93 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
const { EleventyRenderPlugin } = require("@11ty/eleventy");
const plugin_syntaxhighlight = require('@11ty/eleventy-plugin-syntaxhighlight');
const plugin_schema = require("@quasibit/eleventy-plugin-schema");
/* Internationalization i18n : Jorge */
const { EleventyI18nPlugin } = require("@11ty/eleventy");
const i18n = require('eleventy-plugin-i18n');
const translations = require('./src/_data/i18n');
const filters = require('./utils/filters.js')
/*
Thomas : this is to work with N3 triplestore when parsing schema.org metadata
const HTMLParser = require('node-html-parser');
const fs = require('fs');
const jsonld = require('jsonld');
const N3 = require('n3');
const { DataFactory } = N3;
const { namedNode, literal, defaultGraph, quad } = DataFactory;
// store as a global variable
const store = new N3.Store();
*/
module.exports = function(eleventyConfig) {
// plugins
eleventyConfig.addPlugin(plugin_syntaxhighlight);
eleventyConfig.addPlugin(plugin_schema);
// this is to be able to render markdown templates inside Nunjuck templates
// see https://www.11ty.dev/docs/plugins/render/
eleventyConfig.addPlugin(EleventyRenderPlugin);
// config plugins
/*
Thomas : this is to serialize all triples from header in a single output file
eleventyConfig.on('eleventy.after', async ({ dir, results, runMode, outputMode }) => {
const writer = new N3.Writer(
{ format: 'N-Quads' },
{ prefixes: {
schema: 'http://schema.org/'
}
}
);
for (const quad of store)
writer.addQuad(quad);
writer.end((error, result) => {
fs.writeFile("dist/dataset.nq", result, err => {
if (err) { console.error(err); }
});
});
});
*/
// filters
// allows to make an absolute URL relative. Use it like this :
// {{ '/assets/old-website/uploads/2014/09/illustration.jpg' | relative(page) }}
eleventyConfig.addFilter(
"relative",
(absoluteUrl, page) => {
if (!absoluteUrl.startsWith('/')) {
throw new Error('URL is already relative')
}
const relativeUrl = require("path").relative(page.url, absoluteUrl);
return relativeUrl;
}
);
eleventyConfig.addFilter("iso8601", (dateObj) => {
return dateObj.toISOString();
});
Object.keys(filters).forEach((filterName) => {
eleventyConfig.addFilter(filterName, filters[filterName])
})
// transforms
/*
Thomas : this is to extract JSON-LD header in a separate file
eleventyConfig.addTransform("jsonld-extract", function(content) {
// define a mapping of context URL => context doc
let fileContent = fs.readFileSync("src/_data/schemaorg_jsonldcontext.json");
let schemaorg_context = JSON.parse(fileContent);
const CONTEXTS = {
"https://schema.org": schemaorg_context,
"http://schema.org": schemaorg_context
};
// grab the built-in Node.js doc loader
const nodeDocumentLoader = jsonld.documentLoaders.node();
// change the default document loader
const customLoader = async (url, options) => {
if(url in CONTEXTS) {
return {
contextUrl: null, // this is for a context via a link header
document: CONTEXTS[url], // this is the actual document that was loaded
documentUrl: url // this is the actual context URL after redirects
};
}
// call the default documentLoader
return nodeDocumentLoader(url);
};
jsonld.documentLoader = customLoader;
// Prior to Eleventy 2.0: use this.outputPath instead
if( this.page.outputPath && this.page.outputPath.endsWith(".html") ) {
let jsonLdPath = this.page.outputPath.substring(0,this.page.outputPath.length-".html".length)+".jsonld";
let dom = HTMLParser.parse(content);
let jsonLdScripts = dom.querySelector('script[type="application/ld+json"]');
if(jsonLdScripts) {
let jsonldString = jsonLdScripts.text;
// output it to other jsonld file
fs.writeFile(jsonLdPath, jsonldString, err => {
if (err) { console.error(err); }
});
let jsonldObject = JSON.parse(jsonldString);
jsonldObject["@graph"][1].id = this.page.url;
let baseUrl = 'https://sparna.fr';
// console.dir(jsonldObject, { depth:null });
const nquads = jsonld.toRDF(jsonldObject, {format: 'application/n-quads', base:baseUrl}).then(
(result) => {
// console.dir(jsonldObject, {depth: null});
// console.log(result);
// parse the result and add it to store
const parser = new N3.Parser();
parser.parse(result, (error, quad, prefixes) => {
// quad is null to indicate parsing is finished
if (quad) {
quad._graph = namedNode(baseUrl+this.page.url);
// console.log(quad)
store.add(quad);
}
});
}
);
}
return content;
}
return content;
});
*/
// internationalization
eleventyConfig.addPlugin(EleventyI18nPlugin, {
defaultLanguage: 'fr', // Required
errorMode: 'allow-fallback' // Opting out of "strict"
});
eleventyConfig.addPlugin(i18n, {
translations,
fallbackLocales: {
'*': 'fr'
},
});
// filter for collections
eleventyConfig.addFilter('localeFilter', function(collection, locale) {
if (!locale) return collection;
const filtered = collection.filter(item => item.data.locale == locale)
return filtered;
});
// Collections: Posts
eleventyConfig.addCollection('posts', function (collection) {
return collection
.getFilteredByGlob(["src/en/posts/**/*.md", "src/fr/posts/**/*.md"])
})
// pass-through
eleventyConfig.addPassthroughCopy({ "static": "/" });
// settings
return {
markdownTemplateEngine: 'njk',
htmlTemplateEngine: 'njk',
dir: {
input: "src",
output: "dist",
includes: "_includes",
layouts: "_layouts",
data: "_data"
}
}
};