-
-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathby-site-structure.js
55 lines (49 loc) · 2.18 KB
/
by-site-structure.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
import path from 'path';
import url from 'url';
import sanitizeFilename from 'sanitize-filename';
import {getHostFromUrl, getFilepathFromUrl, getFilenameExtension, shortenFilename} from '../utils/index.js';
import resourceTypes from '../config/resource-types.js';
import resourceTypeExtensions from '../config/resource-ext-by-type.js';
export default function generateFilename (resource, {defaultFilename}) {
const resourceUrl = resource.getUrl();
const host = getHostFromUrl(resourceUrl);
const urlParsed = url.parse(resourceUrl);
let filePath = getFilepathFromUrl(resourceUrl);
const extension = getFilenameExtension(filePath);
filePath = path.join(host, filePath);
// If have query string
if (urlParsed.query) {
const parsed = path.parse(filePath);
const basename = path.join(parsed.dir, parsed.name);
// Use the query string as file name in the site structure directory
if (!extension) {
// Without extension: http://example.com/path?q=test => path/q=test
filePath = `${basename}${path.sep}${urlParsed.query}`;
} else {
// With extension: http://example.com/path/picture.png?q=test => path/picture_q=test.png
filePath = `${basename}_${urlParsed.query}${extension}`;
}
}
// If we have HTML from 'http://example.com/path' => set 'path/index.html' as filepath
if (resource.isHtml()) {
const htmlExtensions = resourceTypeExtensions[resourceTypes.html];
const resourceHasHtmlExtension = htmlExtensions.includes(extension);
// add index.html only if filepath has ext != html '/path/test.com' => '/path/test.com/index.html'
if (!resourceHasHtmlExtension) {
if (!urlParsed.query) {
// Without query string: http://example.com/path => path/index.html
filePath = path.join(filePath, defaultFilename);
} else {
// With query string: http://example.com/path?q=test => path/q=test.html
filePath = `${filePath}.html`;
}
}
}
return sanitizeFilepath(filePath);
}
function sanitizeFilepath (filePath) {
filePath = path.normalize(filePath);
const pathParts = filePath.split(path.sep).map(pathPart => sanitizeFilename(pathPart, {replacement: '_'}));
pathParts[pathParts.length - 1] = shortenFilename(pathParts[pathParts.length - 1]);
return pathParts.join(path.sep);
}