-
Notifications
You must be signed in to change notification settings - Fork 1
/
.eleventy.js
187 lines (164 loc) · 5.23 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
module.exports = (config) => {
const siteSettings = require('./src/globals/site.json')
const pluginSafeExternalLinks = require('eleventy-plugin-safe-external-links')
const eleventyNavigationPlugin = require('@11ty/eleventy-navigation')
const sprintdata = require('./src/globals/sprintdata.json')
const excerpt = require('eleventy-plugin-excerpt')
const Image = require('@11ty/eleventy-img')
const path = require('path')
function sprintResultShortcode(year, race) {
let html = '<div class="result-table">'
let data = sprintdata.filter(
(res) =>
parseInt(res.year, 10) === year &&
res.race === race &&
(parseInt(res.pos, 10) < 7 || res.country === 'GBR')
)
for (let i = 0; i < data.length; i += 1) {
switch (data[i].pos) {
case '1':
case '2':
case '3':
html += `<div><img src="/images/flags/${data[i].pos}.svg"></div>`
break
default:
html += `<div>${data[i].pos}</div>`
}
html += `<div>${data[i].name}</div><div>${
data[i].country
}</div><div><img src="/images/flags/${data[
i
].country.toLowerCase()}.png"></div>`
html += `<div>${data[i].time}</div >`
if (race === 'Mixed') {
html += '<div></div>'
} else {
html += `<div>${data[i].down}%</div>`
}
}
html += '</div>'
return html
}
async function bannerShortcode(filename, alt, sizes) {
const src = './src/images/' + filename
let metadata = await Image(src, {
widths: [380, 768, 1280],
formats: ['webp', 'jpeg'],
outputDir: './src/images/',
urlPath: '/images',
dryRun: false,
filenameFormat: function (id, src, width, format, options) {
const extension = path.extname(src)
const name = path.basename(src, extension)
return `${name}-${width}w.${format}`
},
})
let imageAttributes = {
alt,
sizes,
}
return Image.generateHTML(metadata, imageAttributes)
}
async function imageShortcode(filename, alt, sizes) {
const src = './src/images/' + filename
let metadata = await Image(src, {
widths: [null],
formats: ['webp', 'jpeg'],
outputDir: './src/images/',
urlPath: '/images',
dryRun: false,
filenameFormat: function (id, src, width, format, options) {
const extension = path.extname(src)
const name = path.basename(src, extension)
return `${name}-${width}w.${format}`
},
})
let imageAttributes = {
alt,
sizes,
// loading: 'lazy',
// decoding: 'async',
}
return Image.generateHTML(metadata, imageAttributes)
}
config.addNunjucksAsyncShortcode('banner', bannerShortcode)
config.addNunjucksAsyncShortcode('image', imageShortcode)
config.addShortcode('sprintResult', sprintResultShortcode)
config.addFilter('dateDisplay', require('./filters/date-display.js'))
config.addFilter('slugifyTag', require('./filters/slugify-tag.js'))
config.addPlugin(eleventyNavigationPlugin)
config.addPlugin(excerpt, {
excerptSeparator: '<!--more-->',
})
config.addPlugin(pluginSafeExternalLinks, {
pattern: 'https{0,1}://', // RegExp pattern for external links
noopener: true, // Whether to include noopener
noreferrer: false, // Whether to include noreferrer
files: [
// What output file extensions to work on
'.html',
],
})
config.addShortcode('currentYear', function () {
return new Date().getFullYear().toString()
})
config.addPassthroughCopy({ public: './' })
config.addPassthroughCopy('src/images')
config.addPassthroughCopy('src/fonts')
config.addPassthroughCopy('src/data')
config.addPassthroughCopy('src/resources')
config.setBrowserSyncConfig({
files: ['dist/**/*'],
open: true,
})
config.setDataDeepMerge(true)
config.addCollection('frontposts', function (collectionApi) {
// get posts to show on front page
return collectionApi.getFilteredByTag('frontpost').sort(function (a, b) {
return a.data.order - b.data.order
})
})
config.addCollection('tags', function (collectionApi) {
let tagsCollection = new Map()
let max = 0
collectionApi.getAll().forEach(function (item) {
if ('tags' in item.data) {
for (const tag of item.data.tags) {
if (tag !== 'post' && tag !== 'article' && tag !== 'frontpost') {
let number = (tagsCollection.get(tag) || 0) + 1
max = Math.max(max, number)
tagsCollection.set(tag, number)
}
}
}
})
const minLog = Math.log(1)
const maxLog = Math.log(max)
const tags = []
tagsCollection.forEach((number, tag) => {
let factor = (Math.log(number) - minLog) / (maxLog - minLog)
let newTag = {
tag: tag,
number: number,
factor: factor,
step: Math.ceil(factor * 4) + 1,
}
tags.push(newTag)
})
tags.sort((a, b) => {
return a.tag.localeCompare(b.tag, 'en', { ignorePunctuation: true })
})
return tags
})
return {
pathPrefix: siteSettings.baseUrl,
dir: {
input: 'src',
output: 'dist',
includes: 'includes',
layouts: 'includes/layouts',
data: 'globals',
},
passthroughFileCopy: true,
}
}