-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path.eleventy.js
More file actions
172 lines (153 loc) · 5.46 KB
/
.eleventy.js
File metadata and controls
172 lines (153 loc) · 5.46 KB
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
const cagovBuildSystem = require("@cagov/11ty-build-system");
const linkedom = require("linkedom");
const pluginRss = require("@11ty/eleventy-plugin-rss");
const eleventyNavigationPlugin = require("@11ty/eleventy-navigation");
const fs = require('fs');
module.exports = function (eleventyConfig) {
eleventyConfig.htmlTemplateEngine = "njk";
const wordpressImagePath = "img/wordpress";
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addPlugin(eleventyNavigationPlugin);
eleventyConfig.addPlugin(cagovBuildSystem, {
processors: {
sass: {
watch: ["src/css/**/*"],
output: "_site_dist/index.css",
minify: true,
options: {
file: "src/css/index.scss",
includePaths: ["./src/css/sass"],
quietDeps: true,
silenceDeprecations: ["import"],
},
},
esbuild: [
{
watch: ["src/js/**/*"],
options: {
entryPoints: ["src/js/index.js"],
bundle: true,
minify: true,
outfile: "_site_dist/built.js",
},
},
],
},
});
eleventyConfig.addPassthroughCopy({ "wordpress/media": wordpressImagePath });
eleventyConfig.addPassthroughCopy({ "src/img": "img" });
eleventyConfig.addPassthroughCopy({ "src/pdf": "pdf" });
eleventyConfig.addPassthroughCopy({ "src/css/fonts": "fonts" });
eleventyConfig.addTransform("modify-html", function (content) {
if (this.page.outputPath.endsWith(".html")) {
// Remove WP auto-lazy images for the homepage banner.
if (content.includes("<img loading=\"lazy\" class=\"cagov-featured-image\"")) {
content = content.replace(
"<img loading=\"lazy\" class=\"cagov-featured-image\"",
"<img class=\"cagov-featured-image\""
);
}
// Replace Wordpress media paths with correct 11ty output path.
const regexPattern = `http[^"']+?pantheonsite\.io/wp-content/uploads/`;
content = content.replace(new RegExp(regexPattern, 'g'), `/${wordpressImagePath}/`);
}
return content;
});
eleventyConfig.addCollection("dateSort", function (collections) {
return collections.getFilteredByTag("news")
.sort(function (a, b) {
return new Date(a.data.publishdate) - new Date(b.data.publishdate);
})
.reverse();
});
eleventyConfig.addFilter("dateformat", function (dateString) {
const d = new Date(dateString);
return `${d.getMonth() + 1}/${d.getDate()}/${d.getFullYear()}`;
});
eleventyConfig.addFilter("dateyear", function (dateString) {
const d = new Date(dateString);
return d.getFullYear();
});
eleventyConfig.addFilter("fixEntities", function (str) {
// convert specific entities which wordpress is providing incorrectly to specific UTF-8 chars
const map = {
"d;": "-",
"-": "-",
"'s": "’s",
"'t": "’t",
"'n": "’n",
"'": "’",
"’": "’",
"!": "!",
"?": "?",
",": ",",
"#": "#",
"$": "$",
"&": "&",
"(": "(",
")": ")",
};
for (const key in map) {
if (Object.prototype.hasOwnProperty.call(map, key)) {
str = str.replace(new RegExp(key, 'g'), map[key]);
}
}
return str;
});
eleventyConfig.addFilter('includes', (items, value) => {
return (items || []).includes(value);
});
eleventyConfig.addFilter("repairMetaImage", (url) => {
return url.replace("https://live-digital-ca-gov.pantheonsite.io/wp-content/uploads/", "https://innovation.ca.gov/img/wordpress/");
});
eleventyConfig.addFilter("changeDomain", function (url, domain) {
try {
let host = config.build.canonical_url.split("//"); // TEMP Cheat to get https
let changedUrl = url;
// There are multiple strings that we may need to replace because of how we merge and work with data. Use them.
config.build.replace_urls.map((item) => {
changedUrl = changedUrl.replace(item, host[0] + "//" + domain);
});
changedUrl = changedUrl.replace('test-digital-ca-gov.pantheonsite.io', host[0] + "//" + 'development.digital.ca.gov');
return changedUrl;
} catch {
return url;
}
});
eleventyConfig.on("eleventy.after", async ({ results }) => {
// Generate map of all HTML files for tests.
const files = results.map((r) => {
const { content, ...paths } = r;
return paths;
});
const htmlFiles = files.filter((p) => p?.outputPath?.endsWith(".html"))
.filter((p) => !p?.outputPath?.includes("par-statistics"));
// Add modification dates from sidecar JSON files.
const htmlFilesWithDates = htmlFiles.map((entry) => {
const sidecarPath = entry.inputPath.replace(/\.html$/, '.json');
let modified = null;
try {
if (fs.existsSync(sidecarPath)) {
const sidecar = JSON.parse(fs.readFileSync(sidecarPath, 'utf8'));
if (sidecar?.data?.modified_gmt) {
modified = sidecar.data.modified_gmt;
}
}
} catch {
// If sidecar can't be read, leave modified as null.
}
return { ...entry, modified };
});
const htmlFileJson = JSON.stringify(htmlFilesWithDates, null, 2);
fs.writeFileSync("./_site_dist/allFiles.json", htmlFileJson, "utf8");
});
return {
htmlTemplateEngine: "njk",
markdownTemplateEngine: "md",
templateFormats: ["html", "njk", "11ty.js", "md"],
dir: {
input: "pages",
output: "_site",
},
};
};