forked from johnhadaway/plurality.net
-
Notifications
You must be signed in to change notification settings - Fork 12
/
.eleventy.js
183 lines (167 loc) · 6 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
const pluginTOC = require('eleventy-plugin-toc')
const htmlmin = require("html-minifier");
const EleventyFetch = require('@11ty/eleventy-fetch');
const { EleventyRenderPlugin } = require('@11ty/eleventy');
module.exports = function(eleventyConfig) {
eleventyConfig.setLibrary(
'md',
markdownIt = require('markdown-it')({
html: true
}).use(require('markdown-it-anchor'))
.use(require('markdown-it-footnote'))
);
eleventyConfig.addPlugin(pluginTOC, {
tags: ['h1', 'h2', 'h3'],
ul: true,
wrapper: 'div'
});
eleventyConfig.addPlugin(EleventyRenderPlugin);
eleventyConfig.addAsyncShortcode('readdynamiccodeRender', async (url,fallback_url) => {
const log_text = `The location of the resource is defined in the md file but was not accessible. The URL is "${url}"`
const log_text_shown_from_fallback = `The resource of fallback location at "${fallback_url} will be used if available."`
const screen_text = "Please revisit this page later. The page is currently unavailable but will become available soon."
const renderTemplateInside = eleventyConfig.javascriptFunctions.renderTemplate;
const shortcodeFetch = (url,fallback_url) => {
try {
let returnedContent = EleventyFetch(url, {
duration: '1m',
type: 'text',
verbose: true
}).then(
function(response){
let text = renderTemplateInside(response,'md');
console.log("post:",text)
return text;
}
).catch(error => {
console.log(log_text,error);
if (fallback_url != null ){
console.log(log_text_shown_from_fallback)
return shortcodeFetch(fallback_url,null);
} else {
return screen_text;
}
});
return returnedContent;
} catch (e) {
console.log(log_text);
return screen_text;
}
}
return shortcodeFetch(url,fallback_url);
});
eleventyConfig.addAsyncShortcode('readdynamiccode', async (url,fallback_url) => {
const log_text = `The location of the resource is defined in the md file but was not accessible. The URL is "${url}"`
const log_text_shown_from_fallback = `The resource of fallback location at "${fallback_url} will be used if available."`
const screen_text = "Please revisit this page later. The page is currently unavailable but will become available soon."
const shortcodeFetch = (url,fallback_url) => {
try {
let returnedContent = EleventyFetch(url, {
duration: '1m',
type: 'text',
verbose: true
}).then(
function(response){
return response;
}
).catch(error => {
console.log(log_text);
if (fallback_url != null ){
console.log(log_text_shown_from_fallback)
return shortcodeFetch(fallback_url,null);
} else {
return screen_text;
}
});
return returnedContent;
} catch (e) {
console.log(log_text);
return screen_text;
}
}
return shortcodeFetch(url,fallback_url);
});
eleventyConfig.addTransform("htmlmin", function(content, outputPath) {
if( outputPath.endsWith(".html") ) {
let minified = htmlmin.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true
});
return minified;
}
});
eleventyConfig.addFilter("sortDataByLanguageIso", (obj) => {
const sorted = [];
Object.keys(obj)
.sort((a, b) => {
if (('language' in obj[a].data && 'language' in obj[b].data) !== true){
return -1;
} else {
return obj[a].data.language.iso6392B > obj[b].data.language.iso6392B ? 1 : -1;
}
})
.forEach((name, index) => {
sorted[index] = obj[name]
});
return sorted;
});
eleventyConfig.addFilter("sortDataByChapteridSubidLanguageIso", (obj) => {
const sorted = [];
console.log(Object.keys(obj));
Object.keys(obj)
.sort((a, b) => {
if (('language' in obj[a].data && 'language' in obj[b].data) !== true) {
return 0;
}
if (('chapterid' in obj[a].data && 'chapterid' in obj[b].data) !== true) {
return 0;
}
if (('chapterid_subid' in obj[a].data.chapterid && 'chapterid_subid' in obj[b].data.chapterid) !== true) {
return 0;
}
if (obj[a].data.chapterid.chapterid_subid > obj[b].data.chapterid.chapterid_subid ) {
return 1;
} else if (obj[a].data.chapterid.chapterid_subid < obj[b].data.chapterid.chapterid_subid ){
return -1;
}
if (obj[a].data.language.iso6392B > obj[b].data.language.iso6392B ) {
return 1;
} else if (obj[a].data.language.iso6392B < obj[b].data.language.iso6392B ) {
return -1;
}
return 0;
})
.forEach((name, index) => {
sorted[index] = obj[name]
});
console.log("start list")
sorted.forEach((name, index) => {
console.log(name.data.language.en,name.data.language.iso6392B);
console.log(name.data.chapterid.chapterid_subid);
console.log(name.data.url);
});
return sorted;
});
eleventyConfig.addPassthroughCopy({
"src/site/_includes/css/*.css" : "assets/css",
"src/site/_includes/js/*.js" : "assets/js",
"src/site/_includes/favicons/*.png" : "assets/favicons",
"src/site/_data/fonts/authentic-sans/*" : "assets/fonts/authentic-sans",
"src/site/_data/fonts/lanapixel/*" : "assets/fonts/lanapixel",
"src/site/_data/fonts/ChenYuluoyan/*" : "assets/fonts/ChenYuluoyan",
"src/site/_data/fonts/iansui/*" : "assets/fonts/iansui",
"src/site/_data/fonts/jost/*" : "assets/fonts/jost",
"src/site/_data/fonts/inter/*" : "assets/fonts/inter",
});
eleventyConfig.addWatchTarget("_update_interval");
return {
dir: {
input: "src/site",
output: "dist",
includes: "_includes",
layouts: "_includes/layouts",
data: "_data"
}
}
}