forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
225 lines (208 loc) · 7.91 KB
/
main.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
214
215
216
217
218
219
220
221
222
223
224
225
const isEn = () => location.hash.includes('README_EN');
const isRoot = () => ['', '#/', '#/README', '#/README_EN'].includes(location.hash);
const categories = ['javascript', 'database'];
const getSolutionPrefix = url => {
const res = categories.find(
category =>
url.includes(category + '-solution') ||
url.includes(category.toUpperCase() + '_README'),
);
return res ? res + '-' : '';
};
const sidebar = () => {
if (isRoot()) {
return false;
}
const prefix = getSolutionPrefix(location.hash);
return isEn() ? `${prefix}summary_en.md` : `${prefix}summary.md`;
};
const cleanedHtml = html => {
return html.replace(/<pre>([\s\S]*?)<\/pre>/g, (_, group) => {
return '<pre>' + group.replace(/<code>([\s\S]*?)<\/code>/g, '$1') + '</pre>';
});
};
const replaceHref = html => {
const prefix = getSolutionPrefix(location.hash);
return prefix ? html.replaceAll('(/solution/', '(/' + prefix + 'solution/') : html;
};
const getLang = () => (isEn() ? 'en' : 'zh-CN');
const giscusTheme = () =>
localStorage.getItem('DARK_LIGHT_THEME') === 'light' ? 'light' : 'noborder_dark';
const getTerm = () => {
let path = decodeURI(location.hash.slice(1, location.hash.lastIndexOf('/'))) || '/index';
// restore original path
for (const prefix of categories) {
const s = `${prefix}-solution`;
if (path.includes(s)) {
path = path.replace(s, 'solution');
}
}
return path;
};
window.addEventListener('hashchange', () => {
window.$docsify.loadSidebar = sidebar();
});
window.$docsify = {
name: 'leetcode',
repo: 'doocs/leetcode',
logo: '/images/doocs-leetcode.png',
search: {
depth: 2,
hideOtherSidebarContent: true,
pathNamespaces: ['/', '/solution', '/lcof', '/lcof2', '/lcci', '/lcs', '/lcp', '/basic'],
},
loadSidebar: sidebar(),
auto2top: true,
subMaxLevel: 2,
alias: {
'^/javascript-solution/(.*)': '/solution/$1',
'^/shell-solution/(.*)': '/solution/$1',
'^/database-solution/(.*)': '/solution/$1',
'/lcs/.*/summary.md': '/lcs/summary.md',
'/lcp/.*/summary.md': '/lcp/summary.md',
'/lcci/.*/summary.md': '/lcci/summary.md',
'/lcof/.*/summary.md': '/lcof/summary.md',
'/lcof2/.*/summary.md': '/lcof2/summary.md',
'/solution/.*/summary.md': '/solution/summary.md',
'/basic/.*/summary.md': '/basic/summary.md',
'/lcs/.*/summary_en.md': '/lcs/summary_en.md',
'/lcp/.*/summary_en.md': '/lcp/summary_en.md',
'/lcci/.*/summary_en.md': '/lcci/summary_en.md',
'/lcof/.*/summary_en.md': '/lcof/summary_en.md',
'/lcof2/.*/summary_en.md': '/lcof2/summary_en.md',
'/solution/.*/summary_en.md': '/solution/summary_en.md',
'/basic/.*/summary_en.md': '/basic/summary_en.md',
},
lastModifiedText: {
'/README_EN': 'Last updated: ',
'/': '最近更新时间:',
},
giscus: {
repo: 'doocs/leetcode',
repoId: 'MDEwOlJlcG9zaXRvcnkxNDkwMDEzNjU',
category: 'Announcements',
categoryId: 'DIC_kwDOCOGUlc4CZmhe',
mapping: 'specific',
reactionsEnabled: '0',
strict: '1',
emitMetadata: '0',
inputPosition: 'top',
crossorigin: 'anonymous',
},
contributors: {
repo: 'doocs/leetcode',
ignores: [
'/README.md',
'/README_EN.md',
'/solution/README.md',
'/solution/README_EN.md',
'/summary.md',
],
image: {
margin: '0.2em',
isRound: true,
},
},
darklightTheme: {
defaultTheme: 'light',
siteFont: 'Source Sans Pro,Helvetica Neue,Arial,sans-serif',
codeFontFamily: 'Roboto Mono, Monaco, courier, monospace',
bodyFontSize: '15px',
dark: {
background: '#191919',
highlightColor: '#e96900',
codeBackgroundColor: '#202020',
codeTextColor: '#b4b4b4',
},
light: {
highlightColor: '#e96900',
},
},
pagination: {
previousText: {
'/README_EN': 'PREVIOUS',
'/': '上一题',
},
nextText: {
'/README_EN': 'NEXT',
'/': '下一题',
},
crossChapter: true,
crossChapterText: true,
},
tabs: {
persist: true,
sync: true,
theme: 'classic',
tabComments: true,
tabHeadings: true,
},
plugins: [
(hook, vm) => {
hook.beforeEach(html => {
const { file } = vm.route;
const isUserContent = /githubusercontent\.com/.test(file);
const url = isUserContent
? file
.replace('raw.githubusercontent.com', 'github.com')
.replace(/\/main/, '/blob/main')
: `https://github.com/doocs/leetcode/blob/main/${file}`;
const github = `[GitHub](${url})`;
const gitee = `[Gitee](${url.replace('github', 'gitee')})`;
html = cleanedHtml(html);
html = replaceHref(html);
const editHtml = isEn()
? `:memo: Edit on ${github} / ${gitee}\n`
: `:memo: 在 ${github} / ${gitee} 编辑\n`;
return editHtml + html;
});
hook.afterEach(html => {
const copyright = isEn() ? '. All Rights Reserved' : ' 版权所有';
const currentYear = new Date().getFullYear();
const footer = `<footer>Copyright © 2018-${currentYear} <a href="https://github.com/doocs" target="_blank">Doocs</a>${copyright}</footer>`;
return html + footer;
});
hook.doneEach(() => {
const giscusScript = document.createElement('script');
const {
repo,
repoId,
category,
categoryId,
mapping,
reactionsEnabled,
strict,
emitMetadata,
inputPosition,
crossorigin,
} = $docsify.giscus;
giscusScript.type = 'text/javascript';
giscusScript.async = true;
giscusScript.setAttribute('src', 'https://giscus.app/client.js');
giscusScript.setAttribute('data-repo', repo);
giscusScript.setAttribute('data-repo-id', repoId);
giscusScript.setAttribute('data-category', category);
giscusScript.setAttribute('data-category-id', categoryId);
giscusScript.setAttribute('data-mapping', mapping);
giscusScript.setAttribute('data-reactions-enabled', reactionsEnabled);
giscusScript.setAttribute('data-strict', strict);
giscusScript.setAttribute('data-emit-metadata', emitMetadata);
giscusScript.setAttribute('data-input-position', inputPosition);
giscusScript.setAttribute('crossorigin', crossorigin);
giscusScript.setAttribute('data-theme', giscusTheme());
giscusScript.setAttribute('data-term', getTerm());
giscusScript.setAttribute('data-lang', getLang());
document
.getElementById('main')
.insertBefore(giscusScript, document.getElementById('main').lastChild);
document.getElementById('docsify-darklight-theme').addEventListener('click', () => {
const frame = document.querySelector('.giscus-frame');
frame.contentWindow.postMessage(
{ giscus: { setConfig: { theme: giscusTheme() } } },
'https://giscus.app',
);
});
});
},
],
};