-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUrlSlugGenerator.html
More file actions
332 lines (289 loc) · 9.77 KB
/
Copy pathUrlSlugGenerator.html
File metadata and controls
332 lines (289 loc) · 9.77 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>URL Slug生成器 - JustHTML 只需网页</title>
<script>
/**
* 动态加载(强制去除缓存) CSS 的函数
* @param {string} src - 要加载的 CSS 的 src 链接
* @returns {Promise} 返回一个 Promise 对象,用于处理异步加载
*/
function loadStyle(src) {
return new Promise((resolve, reject) => {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = src + '?v=' + new Date().getTime(); // 使用时间戳避免缓存
link.async = true;
link.onload = resolve;
link.onerror = reject;
document.head.appendChild(link);
});
}
/**
* 动态加载(强制去除缓存) JS 的函数
* @param {string} src - 要加载的 JS 的 src 链接
* @returns {Promise} 返回一个 Promise 对象,用于处理异步加载
*/
function loadScript(src) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = src + '?v=' + new Date().getTime(); // 使用时间戳避免缓存
script.async = true;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
</script>
<script>
loadStyle('../assets/style.css')
let scriptArray = [
"../assets/AddHeader_Tools.js",
"../assets/AddFooter.js",
"../assets/AddIco.js",
"../assets/AutoRun.js",
]
scriptArray.forEach(script => {
loadScript(script);
});
</script>
<script src="../package/Vue/code/vue.global.prod.js"></script>
<script type="module" src="../package/Sober/code/main.js"></script>
<style>
.generator-options {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 15px;
margin: 15px 0;
}
.option-item {
display: flex;
flex-direction: column;
}
.option-item label {
margin-bottom: 5px;
color: var(--accent-color);
}
.option-item input, .option-item select {
padding: 8px;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin: 15px 0;
}
.info-box {
background-color: var(--bg-tertiary);
padding: 10px;
border-radius: var(--border-radius);
margin: 10px 0;
}
.result-container {
position: relative;
margin-top: 20px;
}
.copy-button {
position: absolute;
top: 10px;
right: 10px;
background-color: var(--accent-color);
color: white;
border: none;
padding: 5px 10px;
border-radius: 4px;
cursor: pointer;
z-index: 10;
}
.slug-examples {
margin-top: 20px;
}
.example-item {
padding: 8px;
background-color: var(--bg-secondary);
border-radius: var(--border-radius);
margin-bottom: 8px;
display: flex;
justify-content: space-between;
}
.example-original {
color: var(--text-secondary);
}
.example-slug {
color: var(--accent-color);
font-weight: bold;
}
</style>
</head>
<body>
<div id="app">
<div class="container">
<a href="../index.html" class="back-link">← 返回首页</a>
<div class="tool-section">
<h2>URL Slug生成器</h2>
<p>将标题文本转换为URL友好的slug格式</p>
<div class="info-box">
<p><strong>功能说明:</strong></p>
<p>• 将标题转换为URL友好的格式</p>
<p>• 移除特殊字符和空格</p>
<p>• 支持多种分隔符选择</p>
<p>• 可限制slug长度</p>
</div>
<div>
<textarea
v-model="inputTitle"
placeholder="在此输入标题或文本..."
rows="4"
></textarea>
<div class="generator-options">
<div class="option-item">
<label for="separator">分隔符</label>
<select id="separator" v-model="separator">
<option value="-">连字符 (-)</option>
<option value="_">下划线 (_)</option>
<option value="">无分隔符</option>
</select>
</div>
<div class="option-item">
<label for="maxLength">最大长度</label>
<input
type="number"
id="maxLength"
v-model.number="maxLength"
min="1"
max="200"
>
</div>
<div class="option-item">
<label for="caseType">大小写</label>
<select id="caseType" v-model="caseType">
<option value="lower">小写</option>
<option value="upper">大写</option>
<option value="none">保持不变</option>
</select>
</div>
</div>
<div class="controls">
<button @click="generateSlug">生成Slug</button>
<button @click="copySlug">复制Slug</button>
<button @click="clearAll">清空</button>
</div>
<div class="result-container">
<button class="copy-button" @click="copySlug" v-if="generatedSlug">复制</button>
<textarea
v-model="generatedSlug"
placeholder="生成的Slug将显示在这里..."
rows="2"
readonly
></textarea>
</div>
</div>
<div class="slug-examples">
<h3>Slug示例</h3>
<div class="example-item">
<span class="example-original">如何制作美味的蛋糕</span>
<span class="example-slug">ru-he-zhi-zuo-mei-wei-de-dan-gao</span>
</div>
<div class="example-item">
<span class="example-original">Top 10 JavaScript Frameworks in 2023</span>
<span class="example-slug">top-10-javascript-frameworks-in-2023</span>
</div>
<div class="example-item">
<span class="example-original">Hello World! #Programming @Developer</span>
<span class="example-slug">hello-world-programming-developer</span>
</div>
</div>
</div>
</div>
</div>
<script>
const { createApp, ref, computed, watch } = Vue;
createApp({
setup() {
const inputTitle = ref('');
const separator = ref('-');
const maxLength = ref(100);
const caseType = ref('lower');
const generatedSlug = ref('');
// 生成slug的核心函数
const createSlug = (title) => {
if (!title) return '';
// 移除HTML标签
let cleanTitle = title.replace(/(<([^>]+)>)|( )/gi, ' ');
// 处理中文字符和英文字符
// 使用正则表达式保留字母、数字、空格以及中文字符
cleanTitle = cleanTitle.replace(/[^\w\u4e00-\u9fff\s-]/g, ' ');
// 替换空格和多个连续空格为分隔符
let slug = cleanTitle.replace(/\s+/g, separator.value);
// 如果分隔符不为空,还需要处理重复的分隔符
if (separator.value) {
const escapedSeparator = separator.value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const doubleSepRegex = new RegExp(`${escapedSeparator}{2,}`, 'g');
slug = slug.replace(doubleSepRegex, separator.value);
}
// 去除开头和结尾的分隔符
if (separator.value) {
const escapedSeparator = separator.value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const trimRegex = new RegExp(`^${escapedSeparator}+|${escapedSeparator}+$`, 'g');
slug = slug.replace(trimRegex, '');
}
// 应用大小写规则
if (caseType.value === 'lower') {
slug = slug.toLowerCase();
} else if (caseType.value === 'upper') {
slug = slug.toUpperCase();
}
// 限制长度
if (maxLength.value > 0 && slug.length > maxLength.value) {
slug = slug.substring(0, maxLength.value);
// 确保不在单词中间截断
if (separator.value) {
const lastSepIndex = slug.lastIndexOf(separator.value);
if (lastSepIndex > maxLength.value * 0.7) { // 如果截断点在后30%内
slug = slug.substring(0, lastSepIndex);
}
}
}
return slug;
};
// 监听输入变化并自动生成slug
watch([inputTitle, separator, maxLength, caseType], () => {
generatedSlug.value = createSlug(inputTitle.value);
});
const generateSlug = () => {
generatedSlug.value = createSlug(inputTitle.value);
};
const copySlug = async () => {
if (!generatedSlug.value) {
alert('没有可复制的slug');
return;
}
try {
await navigator.clipboard.writeText(generatedSlug.value);
alert('Slug已复制到剪贴板!');
} catch (err) {
console.error('复制失败:', err);
alert('复制失败');
}
};
const clearAll = () => {
inputTitle.value = '';
generatedSlug.value = '';
};
return {
inputTitle,
separator,
maxLength,
caseType,
generatedSlug,
generateSlug,
copySlug,
clearAll
};
}
}).mount('#app');
</script>
</body>
</html>