-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy.js
164 lines (158 loc) · 5.1 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
const pkg = require("./package.json");
const library = require("./src/index.js");
const path = require("path");
const fs = require("fs");
// Based on
// https://github.com/molly/static-timeline-generator
// https://github.com/molly/web3-is-going-great
const pluginDefaults = {
domainName: "http://localhost:8080",
timelineOutFolder: "timelines",
outDir: path.normalize(path.join(__dirname, "../../../", "docs")),
// escape from the layout folder
layoutFolderDepth: "../../",
timelinesInFolder: "/src/timelines/",
customCSS: "assets/css/template-timeline.css",
addBaseFiles: false, // true will take the execution path and add eleventyConfig.dir.input returned to the eleventyConfig directory. A string will target that exact path.
};
module.exports = function (eleventyConfig, options) {
const pluginConfig = Object.assign(pluginDefaults, options);
pluginConfig.jsPath = pluginConfig.domainName + "/assets/timelines/js";
pluginConfig.cssPath = pluginConfig.domainName + "/assets/timelines/css";
const dirs = __dirname.split(process.cwd());
const pluginLayoutPath = path.join(
pluginConfig.layoutFolderDepth,
//"./",
dirs[1],
"src",
"layouts",
);
console.log(
"Activate Timelinety",
pluginConfig,
path.join(__dirname, "/src/js"),
process.cwd(),
dirs,
pluginLayoutPath,
// fs.statSync(path.join(pluginLayoutPath, "timeline-item.njk"))
);
eleventyConfig.addTemplateFormats("njk,md");
console.log(
"eleventyConfig dir",
eleventyConfig.dir,
path.normalize(path.join(process.cwd(), eleventyConfig.dir.input)),
path.normalize(path.join(__dirname, "pages")),
);
if (pluginConfig.addBaseFiles) {
eleventyConfig.on("eleventy.before", () => {
let copyFileTo = path.normalize(path.join(process.cwd(), "src"));
if (typeof pluginConfig.addBaseFiles == "string") {
copyFileTo = pluginConfig.addBaseFiles;
} else {
copyFileTo = path.normalize(
path.join(process.cwd(), eleventyConfig.dir.input),
);
}
const copyFromPath = path.normalize(
path.join(__dirname, "src/pages"),
);
[
"timeline.md",
"timelines.md",
"timeline-endpoints.md",
"timeline-pages.md",
].forEach((file) => {
const timelineMDFile = path.join(copyFromPath, file);
const targetMDFile = path.join(copyFileTo, file);
if (!fs.existsSync(targetMDFile)) {
console.log(
`Eleventy copy from ${timelineMDFile} to ${targetMDFile}`,
);
console.log("File does not already exist, copy it over");
fs.copyFileSync(
timelineMDFile,
targetMDFile,
fs.constants.COPYFILE_EXCL,
);
}
});
});
}
pluginConfig.pluginLayoutPath = pluginLayoutPath;
const localJs = path.join(__dirname, "/src/js");
const jsPassthru = {};
jsPassthru[localJs] = "assets/timelines/js";
eleventyConfig.addPassthroughCopy(jsPassthru);
const localCss = path.join(__dirname, "/src/css");
const cssPassthru = {};
cssPassthru[localCss] = "assets/timelines/css";
eleventyConfig.addPassthroughCopy(cssPassthru);
eleventyConfig.addLayoutAlias(
"timeline",
path.join(pluginLayoutPath, "timeline.njk"),
);
eleventyConfig.addLayoutAlias(
"timeline-standalone-item",
path.join(pluginLayoutPath, "timeline-standalone-item.njk"),
);
eleventyConfig.addLayoutAlias(
"timeline-base",
path.join(pluginLayoutPath, "timeline-base.njk"),
);
eleventyConfig.addLayoutAlias(
"timeline-head",
path.join(pluginLayoutPath, "head.njk"),
);
eleventyConfig.addLayoutAlias(
"timeline-json",
path.join(pluginLayoutPath, "json/timeline.njk"),
);
eleventyConfig.addLayoutAlias(
"timeline-wrapper",
path.join(pluginLayoutPath, "timeline-wrapper.njk"),
);
eleventyConfig.addGlobalData("timelinesConfig", pluginConfig);
eleventyConfig.addFilter("buildlog", function (value) {
console.log("TIMELINE Buildlog ", value);
return;
});
eleventyConfig.addFilter("makeISODate", function (value) {
return new Date(value).toISOString();
});
eleventyConfig.addFilter("timelineStripOpenCloseQuotes", function (value) {
//console.log("timelineStripOpenCloseQuotes", value);
return !value ? value : value.replaceAll(/^"|"$/g, "");
});
// https://stackoverflow.com/questions/46426306/how-to-safely-render-json-into-an-inline-script-using-nunjucks
// is expected to run after `| escape`.
eleventyConfig.addFilter("timelineJSONEscape", function (value) {
//console.log("timelineJSONEscape", value, spaces);
if (!value) {
return value;
}
if (typeof value !== "string" && typeof value === "object") {
value = value.val;
}
if (!value) {
return value;
}
if (value instanceof nunjucks.runtime.SafeString) {
value = value.toString();
}
var spaces = 4;
//console.log("timelineJSONEscape 2", value, spaces);
const jsonString = JSON.stringify(value, null, spaces).replace(
/\</g,
"\\u003c",
);
//console.log("timelineJSONEscape 2.5 ", jsonString);
let finalJsonString = jsonString
.replaceAll(/^"|"$/g, "")
.replace(/\>/g, "\\u003e")
.replace(/"/g, "\\u0022");
//console.log("timelineJSONEscape 3 ", finalJsonString);
return nunjucks.runtime.markSafe(finalJsonString);
});
return library(eleventyConfig, pluginConfig);
};
// Notes - Load other posts above and below