forked from fluid-project/eleventy-plugin-fluid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
227 lines (207 loc) · 8.9 KB
/
index.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
/*
Copyright the eleventy-plugin-fluid copyright holders.
See the AUTHORS.md file at the top-level directory of this distribution and at
https://github.com/fluid-project/eleventy-plugin-fluid/raw/main/AUTHORS.md.
Licensed under the New BSD license. You may not use this file except in compliance with this License.
You may obtain a copy of the New BSD License at
https://github.com/fluid-project/eleventy-plugin-fluid/raw/main/LICENSE.md.
*/
"use strict";
const fg = require("fast-glob");
const path = require("path");
const MarkdownIt = require("markdown-it");
const { EleventyRenderPlugin, EleventyI18nPlugin } = require("@11ty/eleventy");
const { getLangDir } = require("rtl-detect");
const i18n = require("eleventy-plugin-i18n-gettext");
const pluginWebc = require("@11ty/eleventy-plugin-webc");
const figureShortcode = require("./src/shortcodes/figure-shortcode.js");
const formatDateFilter = require("./src/filters/format-date-filter.js");
const htmlMinifyTransform = require("./src/transforms/html-minify-transform.js");
const isoDateFilter = require("./src/filters/iso-date-filter.js");
const limitFilter = require("./src/filters/limit-filter.js");
const splitFilter = require("./src/filters/split-filter.js");
const uioShortcodes = require("./src/shortcodes/uio.js");
const uioAssets = require("./src/config/uio-assets.json");
const languages = require("./src/config/languages.json");
const compileCss = require("./src/compilers/compile-css.js");
const compileSass = require("./src/compilers/compile-sass.js");
const compileJs = require("./src/compilers/compile-js.js");
const merge = require("@11ty/eleventy/src/Util/Merge.js");
module.exports = {
initArguments: {},
configFunction: function (eleventyConfig, options = {}) {
options = merge({
uio: true,
markdown: {
options: {
html: true,
linkify: true,
typographer: true
},
plugins: []
},
css: {
basePath: `./${eleventyConfig.dir.input || "src"}/assets/styles`,
enabled: true,
minify: true,
sourceMap: false,
drafts: {
nesting: true
},
browserslist: "> 1%"
},
sass: {
basePath: `./${eleventyConfig.dir.input || "src"}/assets/styles`,
enabled: false,
minify: true,
sourceMap: false,
drafts: {
nesting: true
},
browserslist: "> 1%"
},
js: {
basePath: `./${eleventyConfig.dir.input || "src"}/assets/scripts`,
enabled: true,
minify: true,
target: "es2020",
outdir: `./${eleventyConfig.dir.output || "_site"}/assets/scripts`
},
webc: {
components: `./${eleventyConfig.dir.input || "src"}/_components/**/*.webc`
},
supportedLanguages: languages,
defaultLanguage: "en",
i18n: true,
localesDirectory: `./${eleventyConfig.dir.input || "src"}/_locales`,
templateFormats: [
"html",
"md",
"webc",
"11ty.js",
"liquid",
"njk",
"hbs",
"mustache",
"ejs",
"haml",
"pug"
]
}, options);
/** Plugins */
eleventyConfig.addPlugin(EleventyI18nPlugin, {
defaultLanguage: options.defaultLanguage
});
if (options.i18n) {
eleventyConfig.addPlugin(i18n, {
localesDirectory: options.localesDirectory
});
}
eleventyConfig.addPlugin(EleventyRenderPlugin);
eleventyConfig.addPlugin(pluginWebc, options.webc);
/** Global Data */
eleventyConfig.addGlobalData("defaultLanguage", options.defaultLanguage);
eleventyConfig.addGlobalData("defaultLanguageDirection", getLangDir(options.defaultLanguage));
eleventyConfig.addGlobalData("supportedLanguages", options.supportedLanguages);
/** Filters */
eleventyConfig.addFilter("formatDate", formatDateFilter);
eleventyConfig.addFilter("isoDate", isoDateFilter);
eleventyConfig.addFilter("limit", limitFilter);
eleventyConfig.addFilter("markdown", function (value) {
// eslint-disable-next-line no-console
console.warn("The markdown filter will be removed in a future version of eleventy-plugin-fluid. Use the renderString shortcode instead.");
const md = new MarkdownIt(options.markdown.options);
options.markdown.plugins.forEach(plugin => {
if (typeof plugin === "string") {
md.use(require(plugin));
} else {
const [pluginPackage, options = {}] = plugin;
md.use(require(pluginPackage), options);
}
});
return md.render(value);
});
eleventyConfig.addFilter("slug", () => {
throw new Error("`slug` filter is no longer supported. Please use `slugify`.");
});
eleventyConfig.addFilter("split", splitFilter);
/** Shortcodes */
eleventyConfig.addShortcode("renderString", async function (content, format) {
if (options.templateFormats.includes(format)) {
return eleventyConfig.javascriptFunctions.renderTemplate.call(this, content, format);
}
return content;
});
eleventyConfig.addPairedShortcode("figure", figureShortcode);
if (options.uio) {
Object.keys(uioShortcodes).forEach((shortcodeName) => {
eleventyConfig.addShortcode(shortcodeName, uioShortcodes[shortcodeName]);
});
uioAssets.forEach((asset) => {
const fileMapping = {};
fileMapping[`node_modules/infusion/${asset}`] = `lib/infusion/${asset.replace("dist/", "")}`;
eleventyConfig.addPassthroughCopy(fileMapping);
});
}
/** Template Formats */
eleventyConfig.amendLibrary("md", md => {
md.set(options.markdown.options);
options.markdown.plugins.forEach(plugin => {
if (typeof plugin === "string") {
md.use(require(plugin));
} else {
const [pluginPackage, options = {}] = plugin;
md.use(require(pluginPackage), options);
}
});
});
if (options.css.enabled) {
eleventyConfig.addTemplateFormats("css");
eleventyConfig.addExtension("css", {
outputFileExtension: "css",
getData: async function () {
return {
eleventyExcludeFromCollections: true
};
},
compile: async function (inputContent, inputPath) {
return await compileCss(inputContent, inputPath, options.css);
}
});
}
if (options.sass.enabled) {
eleventyConfig.addTemplateFormats("scss");
eleventyConfig.addExtension("scss", {
outputFileExtension: "css",
getData: async function () {
return {
eleventyExcludeFromCollections: true
};
},
compile: async function (inputContent, inputPath) {
return await compileSass(inputContent, inputPath, options.sass, this);
}
});
}
if (options.js.enabled) {
eleventyConfig.addWatchTarget(options.js.basePath);
eleventyConfig.on("eleventy.before", async () => {
const entryPoints = await fg([`${options.js.basePath}/**/*.js`]);
entryPoints.forEach(async item => {
if (!path.basename(item).startsWith("_")) {
await compileJs(item, options.js);
const outputBasename = path.basename(options.js.outdir);
const relativePath = item.split(outputBasename)[1];
const outputDir = `./${path.dirname(path.join(options.js.outdir, relativePath))}`;
// eslint-disable-next-line no-console
console.log(`[11ty] Writing ${path.join(outputDir, path.basename(item))} from ${item}`);
}
});
});
}
/** Transforms */
eleventyConfig.addTransform("htmlMinify", htmlMinifyTransform);
},
generatePermalink: require("./src/utils/generate-permalink.js"),
localizeData: require("./src/utils/localize-data.js")
};