-
Notifications
You must be signed in to change notification settings - Fork 2
/
.eleventy.js
221 lines (195 loc) · 7.83 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
const { EleventyI18nPlugin } = require("@11ty/eleventy");
const CleanCSS = require("clean-css");
const htmlmin = require("html-minifier");
const cagovBuildSystem = require("@cagov/11ty-build-system");
const config = require("./config/index.js");
const {
copyFolderRecursiveSync,
} = require("./src/js/eleventy/sync-static-content.js");
const {
renderPostLists,
renderWordpressPostTitleDate,
setDefaultAttributes,
} = require("./src/js/eleventy/post-list/render.js");
const { renderEventLists } = require("./src/js/eleventy/event-list/render.js");
const {
pagePath,
relativePath,
i18n,
} = require("./src/js/eleventy/filters.js");
module.exports = function eleventyBuild(eleventyConfig) {
eleventyConfig.htmlTemplateEngine = "njk";
// Copy content from static bundle to gitignored folder in 11ty directory for local processing
eleventyConfig.setUseGitIgnore(false);
copyFolderRecursiveSync(
config.staticContentPaths.posts,
config.build.eleventy_content
);
copyFolderRecursiveSync(
config.staticContentPaths.pages,
config.build.eleventy_content
);
copyFolderRecursiveSync(
config.staticContentPaths.menu,
config.build.eleventy_content
);
copyFolderRecursiveSync(
config.staticContentPaths.redirects,
config.build.eleventy_content
);
// Register ca.gov 11ty build system.
eleventyConfig.addPlugin(cagovBuildSystem, {
processors: {
sass: {
watch: ["src/css/**", "src/components/**"],
output: "dist/index.css",
options: {
file: "src/css/index.scss",
includePaths: ["./src/css"],
},
},
esbuild: {
watch: ["src/js/**", "src/js/**/*"],
options: {
entryPoints: ["src/js/index.js"],
bundle: true,
minify: true,
outfile: "dist/site.js",
},
},
},
});
eleventyConfig.setBrowserSyncConfig({
notify: true,
watch: true,
});
// https://www.11ty.dev/docs/plugins/i18n/ canary version docs
eleventyConfig.addPlugin(EleventyI18nPlugin, {
// any valid BCP 47-compatible language tag is supported
defaultLanguage: "en",
});
// 11ty filters:
eleventyConfig.addFilter(
"cssmin",
(code) => new CleanCSS({}).minify(code).styles
);
eleventyConfig.addFilter("i18n", i18n);
eleventyConfig.addFilter("pagePath", pagePath);
eleventyConfig.addFilter("relativePath", relativePath);
// Used in announcements.njk
eleventyConfig.addFilter("displayPostInfo", (item) =>
renderWordpressPostTitleDate(item.data, setDefaultAttributes())
);
eleventyConfig.addTransform("htmlTransforms", (html, outputPath) => {
// !outputPath || // Do we really need this??
if (outputPath.endsWith(".html")) {
// Render post-list components
if (html.includes("cagov-post-list")) {
html = renderPostLists(html);
}
// Render posts with event web component
if (html.includes("cagov-event-post-list")) {
html = renderEventLists(html);
}
// Remove WP auto-lazy images for the homepage banner.
if (html.includes('<img loading="lazy" class="cagov-featured-image"')) {
html = html.replace(
'<img loading="lazy" class="cagov-featured-image"',
'<img class="cagov-featured-image"'
);
}
// Replace any domain from replace list with the canonical url for the current build.
// For this cannabis.ca.gov instance, there are multiple URL sources coming from different backend systems
config.build.replace_urls.forEach((rootPath) => {
/* the old code below was replacing the rootPath in the entire html,
and skipping pages that contained mixed media and non-media URLs */
/* if (html !== undefined && html.includes(rootPath) && !html.includes('wp-content/uploads') ) {
console.log("Replacing rootPath: " + rootPath + " with: " + config.build.canonical_site_url);
html = html.replace(
new RegExp(rootPath, "g"),
config.build.canonical_site_url
);
} */
/* I've modified the code to examine the URLs individually */
if (html !== undefined && html.includes(rootPath) /* && !html.includes('wp-content/uploads') */) {
// Split the HTML by anchor tags (<a>...</a>)
const anchorTags = html.split(/(<a.*?<\/a>)/);
// Loop through each anchor tag and replace links one at a time
for (let i = 0; i < anchorTags.length; i++) {
const anchorTag = anchorTags[i];
// Check if the current element is an anchor tag
if (anchorTag.includes('<a')) {
// Extract the href attribute value from the anchor tag
const hrefMatch = anchorTag.match(/href="([^"]*)"/);
if (hrefMatch && hrefMatch[1]) {
const hrefValue = hrefMatch[1];
// Skip if the rootPath is not in the URL or if it is a media URL, which should not be transformed
if (!hrefValue.includes(rootPath) || hrefValue.includes('wp-content/uploads')) {
continue;
}
// Replace the rootPath with the new URL from config.build.canonical_site_url
const newHrefValue = hrefValue.replace(new RegExp(rootPath, "g"), config.build.canonical_site_url);
// Replace the old href attribute value with the new one
const updatedAnchorTag = anchorTag.replace(hrefValue, newHrefValue);
// debugging
console.log("Replacing: " + hrefValue + " with: " + newHrefValue);
// Replace the original anchor tag in the anchorTags array with the updated one
anchorTags[i] = updatedAnchorTag;
}
}
}
// Join the updated anchor tags back into a single string
html = anchorTags.join('');
}
return false;
});
// // jbum - not needed for offload
// // Read a list of full media paths for any links that should be relative to this instance.
// // Replace with the local media folder.
// // Note: do not change the media folder without a corresponding update to the redirects (using Redirection plugin in editor).
// config.build.media_replace_urls.forEach((mediaPath) => {
// if (html !== undefined && html.includes(mediaPath)) {
// html = html.replace(
// new RegExp(mediaPath, "g"),
// `/${config.build.docs_media}/`
// );
// }
// return false;
// });
// // jbum - not needed for offload
// // Patch for glitch/issue with absolute url permalinks for og meta - likely resulting from custom eleventy absolutePath filter
// if (html !== undefined && html.includes("//wp-content/uploads/")) {
// html = html.replace(
// new RegExp("//wp-content/uploads/", "g"),
// `/${config.build.docs_media}/`
// );
// }
// Minify HTML
html = htmlmin.minify(html, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true,
});
}
return html;
});
// Copy media assets folder from static site to built site
// 11ty copy assets
eleventyConfig.addPassthroughCopy({ "src/assets/": "assets" });
eleventyConfig.addPassthroughCopy({ "src/css/fonts/": "fonts" }); // Required location by cagov code
// // jbum - not needed for offload
// eleventyConfig.addPassthroughCopy({
// [config.staticContentPaths.media]: config.build.docs_media,
// });
eleventyConfig.addPassthroughCopy({ "dist/*": "/" });
return {
htmlTemplateEngine: "njk",
markdownTemplateEngine: "md",
templateFormats: ["html", "njk", "11ty.js", "md"],
dir: {
input: config.build.eleventy_input,
output: config.build.eleventy_output,
layouts: config.build.eleventy_layouts,
},
};
};