-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathpermalinks.ts
134 lines (111 loc) · 3.39 KB
/
permalinks.ts
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
import slugify from 'limax';
import { SITE, APP_BLOG } from 'astrowind:config';
import { trim } from '~/utils/utils';
export const trimSlash = (s: string) => trim(trim(s, '/'));
const createPath = (...params: string[]) => {
const paths = params
.map((el) => trimSlash(el))
.filter((el) => !!el)
.join('/');
return '/' + paths + (SITE.trailingSlash && paths ? '/' : '');
};
const BASE_PATHNAME = SITE.base || '/';
export const cleanSlug = (text = '') =>
trimSlash(text)
.split('/')
.map((slug) => slugify(slug))
.join('/');
export const BLOG_BASE = cleanSlug(APP_BLOG?.list?.pathname);
export const CATEGORY_BASE = cleanSlug(APP_BLOG?.category?.pathname);
export const TAG_BASE = cleanSlug(APP_BLOG?.tag?.pathname) || 'tag';
export const POST_PERMALINK_PATTERN = trimSlash(APP_BLOG?.post?.permalink || `${BLOG_BASE}/%slug%`);
/** */
export const getCanonical = (path = ''): string | URL => {
const url = String(new URL(path, SITE.site));
if (SITE.trailingSlash == false && path && url.endsWith('/')) {
return url.slice(0, -1);
} else if (SITE.trailingSlash == true && path && !url.endsWith('/')) {
return url + '/';
}
return url;
};
/** */
export const getPermalink = (slug = '', type = 'page'): string => {
let permalink: string;
if (
slug.startsWith('https://') ||
slug.startsWith('http://') ||
slug.startsWith('://') ||
slug.startsWith('#') ||
slug.startsWith('javascript:')
) {
return slug;
}
switch (type) {
case 'home':
permalink = getHomePermalink();
break;
case 'blog':
permalink = getBlogPermalink();
break;
case 'asset':
permalink = getAsset(slug);
break;
case 'category':
permalink = createPath(CATEGORY_BASE, trimSlash(slug));
break;
case 'tag':
permalink = createPath(TAG_BASE, trimSlash(slug));
break;
case 'post':
permalink = createPath(trimSlash(slug));
break;
case 'page':
default:
permalink = createPath(slug);
break;
}
return definitivePermalink(permalink);
};
/** */
export const getHomePermalink = (): string => getPermalink('/');
/** */
export const getBlogPermalink = (): string => getPermalink(BLOG_BASE);
/** */
export const getAsset = (path: string): string =>
'/' +
[BASE_PATHNAME, path]
.map((el) => trimSlash(el))
.filter((el) => !!el)
.join('/');
/** */
const definitivePermalink = (permalink: string): string => createPath(BASE_PATHNAME, permalink);
/** */
export const applyGetPermalinks = (menu: object = {}) => {
if (Array.isArray(menu)) {
return menu.map((item) => applyGetPermalinks(item));
} else if (typeof menu === 'object' && menu !== null) {
const obj = {};
for (const key in menu) {
if (key === 'href') {
if (typeof menu[key] === 'string') {
obj[key] = getPermalink(menu[key]);
} else if (typeof menu[key] === 'object') {
if (menu[key].type === 'home') {
obj[key] = getHomePermalink();
} else if (menu[key].type === 'blog') {
obj[key] = getBlogPermalink();
} else if (menu[key].type === 'asset') {
obj[key] = getAsset(menu[key].url);
} else if (menu[key].url) {
obj[key] = getPermalink(menu[key].url, menu[key].type);
}
}
} else {
obj[key] = applyGetPermalinks(menu[key]);
}
}
return obj;
}
return menu;
};