-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path.eleventy.js
More file actions
267 lines (226 loc) Β· 8.07 KB
/
Copy path.eleventy.js
File metadata and controls
267 lines (226 loc) Β· 8.07 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
const eleventyNavigationPlugin = require('@11ty/eleventy-navigation');
const syntaxHighlight = require('@11ty/eleventy-plugin-syntaxhighlight');
const pluginTOC = require('eleventy-plugin-toc');
const imageShortcode = require('./src/_11ty/shortcodes/image-shortcode');
const markdownLibrary = require('./src/_11ty/libraries/markdown-library');
const minifyHtml = require('./src/_11ty/utils/minify-html');
const markdownFilter = require('./src/_11ty/filters/markdown-filter');
const svgFilter = require('./src/_11ty/filters/svg-filter');
const browserSyncConfig = require('./src/_11ty/utils/browser-sync-config');
const { readableDateFilter, machineDateFilter } = require('./src/_11ty/filters/date-filters');
module.exports = function (eleventyConfig) {
const sanitizePathPrefix = (value) => {
if (!value || value === '/') {
return '/';
}
let normalized = value.trim();
if (!normalized.startsWith('/')) {
normalized = `/${normalized}`;
}
if (!normalized.endsWith('/')) {
normalized = `${normalized}/`;
}
return normalized;
};
const pathPrefix = sanitizePathPrefix(process.env.PATH_PREFIX);
const siteUrl = process.env.SITE_URL || 'https://unitaryhack.dev';
const normalizedSiteUrl = siteUrl.endsWith('/') ? siteUrl : `${siteUrl}/`;
eleventyConfig.addGlobalData('sitePathPrefix', pathPrefix);
const includeCname = process.env.INCLUDE_CNAME !== 'false';
// Plugins
eleventyConfig.addPlugin(eleventyNavigationPlugin);
eleventyConfig.addPlugin(syntaxHighlight);
eleventyConfig.addPlugin(pluginTOC);
// Filters
eleventyConfig.addFilter('markdown', markdownFilter);
eleventyConfig.addFilter('withPathPrefix', (value) => {
if (typeof value !== 'string' || !value) {
return value;
}
return value
.replace(/\]\(\/(?!\/)/g, `](${pathPrefix}`)
.replace(/href="\/(?!\/)/g, `href="${pathPrefix}`)
.replace(/src="\/(?!\/)/g, `src="${pathPrefix}`)
.replace(/href='\/(?!\/)/g, `href='${pathPrefix}`)
.replace(/src='\/(?!\/)/g, `src='${pathPrefix}`);
});
eleventyConfig.addFilter('absoluteUrl', (value) => {
if (typeof value !== 'string' || !value) {
return value;
}
if (/^https?:\/\//i.test(value)) {
return value;
}
return new URL(value, normalizedSiteUrl).href;
});
eleventyConfig.addFilter('isHttpUrl', (value) => {
return typeof value === 'string' && /^https?:\/\//i.test(value);
});
const parseRepoInfo = (value, fallbackProvider = 'github') => {
if (typeof value !== 'string' || !value.trim()) {
return { provider: fallbackProvider, repoKey: '' };
}
let raw = value.trim().replace(/^['"]|['"]$/g, '');
if (/^(github|gitlab)\.com\//i.test(raw)) {
raw = `https://${raw}`;
}
try {
const parsed = new URL(raw);
const host = parsed.hostname.toLowerCase();
let path = parsed.pathname.replace(/^\/+|\/+$/g, '');
if (host.endsWith('github.com')) {
const parts = path.split('/').filter(Boolean);
return { provider: 'github', repoKey: parts.slice(0, 2).join('/') };
}
if (host.endsWith('gitlab.com')) {
path = path.split('/-/')[0].replace(/\.git$/i, '');
return { provider: 'gitlab', repoKey: path };
}
} catch (error) {
// Fall through to the repo-like parser below.
}
const repoMatch = raw.match(/^([A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+)(?:$|[:#?\s])/);
if (repoMatch) {
return { provider: fallbackProvider, repoKey: repoMatch[1] };
}
return { provider: fallbackProvider, repoKey: '' };
};
eleventyConfig.addFilter('bountyIssueUrl', (bounty, projectUrl) => {
if (!bounty) {
return '#';
}
if (bounty.url) {
return bounty.url;
}
const projectRepo = parseRepoInfo(projectUrl);
const repoInfo = bounty.repo
? parseRepoInfo(bounty.repo, projectRepo.provider)
: projectRepo;
if (!repoInfo.repoKey || !bounty.issue_num) {
return '#';
}
const kind = bounty.kind || 'issues';
if (repoInfo.provider === 'gitlab') {
const gitlabPath = ['merge_requests', 'work_items'].includes(kind)
? kind
: 'issues';
return `https://gitlab.com/${repoInfo.repoKey}/-/${gitlabPath}/${bounty.issue_num}`;
}
if (kind === 'pull') {
return `https://github.com/${repoInfo.repoKey}/pull/${bounty.issue_num}`;
}
return `https://github.com/${repoInfo.repoKey}/issues/${bounty.issue_num}`;
});
eleventyConfig.addFilter('bountyIssueLabel', (bounty, projectUrl) => {
if (!bounty) {
return 'Issue';
}
if (bounty.title) {
return bounty.title;
}
const projectRepo = parseRepoInfo(projectUrl);
const repoInfo = bounty.repo
? parseRepoInfo(bounty.repo, projectRepo.provider)
: projectRepo;
if (repoInfo.repoKey && bounty.issue_num) {
return `${repoInfo.repoKey}#${bounty.issue_num}`;
}
return bounty.issue_num ? `Issue #${bounty.issue_num}` : 'Issue';
});
eleventyConfig.addFilter('bountyState', (bounty) => {
return bounty && bounty.state ? bounty.state : 'open';
});
eleventyConfig.addFilter('closedBounties', (bounties) => {
if (!Array.isArray(bounties)) {
return [];
}
return bounties.filter((bounty) => {
return bounty && bounty.state === 'closed';
});
});
eleventyConfig.addFilter('sumBountyValues', (bounties) => {
if (!Array.isArray(bounties)) {
return 0;
}
return bounties.reduce((total, bounty) => {
return total + Number(bounty && bounty.value ? bounty.value : 0);
}, 0);
});
eleventyConfig.addFilter('readableDate', readableDateFilter);
eleventyConfig.addFilter('machineDate', machineDateFilter);
eleventyConfig.addFilter('svg', svgFilter);
eleventyConfig.addFilter("sortLeaderboard", function(obj) {
if (!obj || typeof obj !== "object") return [];
return Object.entries(obj).sort((a, b) => b[1] - a[1]);
});
// Shortcodes
eleventyConfig.addNunjucksAsyncShortcode('image', imageShortcode(pathPrefix));
// Libraries
eleventyConfig.setLibrary('md', markdownLibrary);
// Merge data instead of overriding
eleventyConfig.setDataDeepMerge(true);
// Trigger a build when files in this directory change
eleventyConfig.addWatchTarget('./src/assets/scss/');
// Minify HTML output
eleventyConfig.addTransform('htmlmin', minifyHtml);
// Don't process folders with static assets
eleventyConfig.addPassthroughCopy('./src/favicon.ico');
eleventyConfig.addPassthroughCopy('./src/assets/img');
if (includeCname) {
eleventyConfig.addPassthroughCopy('./src/CNAME');
}
// Allow Turbolinks to work in development mode
eleventyConfig.setBrowserSyncConfig(browserSyncConfig);
// Sorting
eleventyConfig.addCollection("sortedProjects", function (collection) {
return collection.getFilteredByGlob("src/projects/*.md").sort(function (a, b) {
let nameA = a.data.title.toUpperCase();
let nameB = b.data.title.toUpperCase();
if (nameA < nameB) return -1;
else if (nameA > nameB) return 1;
else return 0;
});
});
eleventyConfig.addCollection('sitemapPages', function (collection) {
return collection.getAll().filter((item) => {
return (
item.url &&
item.outputPath &&
item.outputPath.endsWith('.html') &&
item.url !== '/404.html' &&
item.data &&
item.data.sitemap !== false
);
}).sort((a, b) => a.url.localeCompare(b.url));
});
// Markdown Plugins
let markdownIt = require("markdown-it");
let markdownItAnchor = require("markdown-it-anchor");
let options = {
html: true,
breaks: true,
linkify: true,
};
let opts = {
permalink: false,
};
eleventyConfig.setLibrary(
"md",
markdownIt(options).use(markdownItAnchor, opts)
);
eleventyConfig.addPairedShortcode("mdRender", (title) => {
return markdownIt().renderInline(title);
});
return {
templateFormats: ['md', 'njk', 'html'],
markdownTemplateEngine: 'njk',
htmlTemplateEngine: 'njk',
dataTemplateEngine: 'njk',
passthroughFileCopy: true,
pathPrefix,
dir: {
input: 'src',
layouts: "_layouts"
},
};
};