-
-
Notifications
You must be signed in to change notification settings - Fork 475
/
TemplatePermalink.js
213 lines (178 loc) · 4.92 KB
/
TemplatePermalink.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
const path = require("path");
const TemplatePath = require("./TemplatePath");
const normalize = require("normalize-path");
const isPlainObject = require("./Util/IsPlainObject");
const serverlessUrlFilter = require("./Filters/ServerlessUrl");
class TemplatePermalink {
// `link` with template syntax should have already been rendered in Template.js
constructor(link, extraSubdir) {
let isLinkAnObject = isPlainObject(link);
this._isRendered = true;
this._writeToFileSystem = true;
let buildLink;
if (isLinkAnObject) {
if ("build" in link) {
buildLink = link.build;
}
// find the first string key
for (let key in link) {
if (typeof key !== "string") {
continue;
}
if (key !== "build" && link[key] !== false) {
this.primaryServerlessUrl = link[key]; // can be an array or string
}
break;
}
} else {
buildLink = link;
}
// permalink: false and permalink: build: false
if (typeof buildLink === "boolean") {
if (buildLink === false) {
this._writeToFileSystem = false;
} else {
throw new Error(
`\`permalink: ${
isLinkAnObject ? "build: " : ""
}true\` is not a supported feature in Eleventy. Did you mean \`permalink: ${
isLinkAnObject ? "build: " : ""
}false\`?`
);
}
} else if (buildLink) {
this.buildLink = buildLink;
}
this.serverlessUrls = {};
if (isLinkAnObject) {
Object.assign(this.serverlessUrls, link);
delete this.serverlessUrls.build;
// default if permalink is an Object but does not have a `build` prop
if (!("build" in link)) {
this._writeToFileSystem = false;
this._isRendered = false;
}
}
this.extraPaginationSubdir = extraSubdir || "";
}
setServerlessPathData(data) {
this.serverlessPathData = data;
}
getServerlessUrls() {
return this.serverlessUrls;
}
_addDefaultLinkFilename(link) {
return link + (link.substr(-1) === "/" ? "index.html" : "");
}
toOutputPath() {
if (!this.buildLink) {
// empty or false
return false;
}
let cleanLink = this._addDefaultLinkFilename(this.buildLink);
let parsed = path.parse(cleanLink);
return TemplatePath.join(
parsed.dir,
this.extraPaginationSubdir,
parsed.base
);
}
// This method is used to generate the `page.url` variable.
// Note that in serverless mode this should still exist to generate the content map
// remove all index.html’s from links
// index.html becomes /
// test/index.html becomes test/
toHref() {
if (this.primaryServerlessUrl) {
if (this.serverlessPathData) {
let urls = serverlessUrlFilter(
this.primaryServerlessUrl,
this.serverlessPathData
);
// Array of *matching* serverless urls only
if (Array.isArray(urls)) {
// return first
return urls[0];
}
return urls;
}
if (Array.isArray(this.primaryServerlessUrl)) {
// return first
return this.primaryServerlessUrl[0];
}
return this.primaryServerlessUrl;
} else if (!this.buildLink) {
// empty or false
return false;
}
let transformedLink = this.toOutputPath();
let original =
(transformedLink.charAt(0) !== "/" ? "/" : "") + transformedLink;
let needle = "/index.html";
if (original === needle) {
return "/";
} else if (original.substr(-1 * needle.length) === needle) {
return original.substr(0, original.length - needle.length) + "/";
}
return original;
}
toPath(outputDir) {
if (!this.buildLink) {
return false;
}
let uri = this.toOutputPath();
if (uri === false) {
return false;
}
return normalize(outputDir + "/" + uri);
}
toPathFromRoot() {
if (!this.buildLink) {
return false;
}
let uri = this.toOutputPath();
if (uri === false) {
return false;
}
return normalize(uri);
}
static _hasDuplicateFolder(dir, base) {
let folders = dir.split("/");
if (!folders[folders.length - 1]) {
folders.pop();
}
return folders[folders.length - 1] === base;
}
static generate(
dir,
filenameNoExt,
extraSubdir,
suffix,
fileExtension = "html"
) {
let hasDupeFolder = TemplatePermalink._hasDuplicateFolder(
dir,
filenameNoExt
);
let path;
if (fileExtension === "html") {
path =
(dir ? dir + "/" : "") +
(filenameNoExt !== "index" && !hasDupeFolder
? filenameNoExt + "/"
: "") +
"index" +
(suffix || "") +
".html";
} else {
path =
(dir ? dir + "/" : "") +
filenameNoExt +
(suffix || "") +
"." +
fileExtension;
}
return new TemplatePermalink(path, extraSubdir);
}
}
module.exports = TemplatePermalink;