forked from docsifyjs/docsify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
130 lines (104 loc) · 2.68 KB
/
Copy pathutil.js
File metadata and controls
130 lines (104 loc) · 2.68 KB
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
import { cached } from '../util/core.js';
const decode = decodeURIComponent;
const encode = encodeURIComponent;
export function parseQuery(query) {
const res = {};
query = query.trim().replace(/^(\?|#|&)/, '');
if (!query) {
return res;
}
// Simple parse
query.split('&').forEach(param => {
const parts = param.replace(/\+/g, ' ').split('=');
res[parts[0]] = parts[1] && decode(parts[1]);
});
return res;
}
export function stringifyQuery(obj, ignores = []) {
const qs = [];
for (const key in obj) {
if (ignores.indexOf(key) > -1) {
continue;
}
qs.push(
obj[key]
? `${encode(key)}=${encode(obj[key])}`.toLowerCase()
: encode(key),
);
}
return qs.length ? `?${qs.join('&')}` : '';
}
export function stripUrlExceptId(str) {
const [path, queryString] = str.split('?');
if (!queryString) {
return str;
}
const params = new URLSearchParams(queryString);
const id = params.get('id');
if (id !== null) {
return `${path}?id=${id}`;
}
return path;
}
export const isAbsolutePath = cached(path => {
return /(:|(\/{2}))/g.test(path);
});
export const removeParams = cached(path => {
return path.split(/[?#]/)[0];
});
export const getParentPath = cached(path => {
if (/\/$/g.test(path)) {
return path;
}
const matchingParts = path.match(/(\S*\/)[^/]+$/);
return matchingParts ? matchingParts[1] : '';
});
export const cleanPath = cached(path => {
return path.replace(/^\/+/, '/').replace(/([^:])\/{2,}/g, '$1/');
});
export const resolvePath = cached(path => {
const segments = path.replace(/^\//, '').split('/');
const resolved = [];
for (const segment of segments) {
if (segment === '..') {
resolved.pop();
} else if (segment !== '.') {
resolved.push(segment);
}
}
return '/' + resolved.join('/');
});
/**
* Normalises the URI path to handle the case where Docsify is
* hosted off explicit files, i.e. /index.html. This function
* eliminates any path segments that contain `#` fragments.
*
* This is used to map browser URIs to markdown file sources.
*
* For example:
*
* http://example.org/base/index.html#/blah
*
* would be mapped to:
*
* http://example.org/base/blah.md.
*
* See here for more information:
*
* https://github.com/docsifyjs/docsify/pull/1372
*
* @param {string} path The URI path to normalise
* @return {string} { path, query }
*/
function normaliseFragment(path) {
return path
.split('/')
.filter(p => p.indexOf('#') === -1)
.join('/');
}
export function getPath(...args) {
return cleanPath(args.map(normaliseFragment).join('/'));
}
export const replaceSlug = cached(path => {
return path.replace('#', '?id=');
});