-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
368 lines (308 loc) · 9.35 KB
/
index.html
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>HTML hreflang to XML Sitemap converter</title>
<link href="https://unpkg.com/prismjs@1.29.0/themes/prism-tomorrow.css" rel="stylesheet" />
<style>
*,
*::after,
*::before {
box-sizing: border-box;
}
:root {
color-scheme: dark;
--gap: 2rem;
}
html,
body {
margin: 0;
height: 100%;
}
body {
background-color: #222;
color: #eee;
font-family: sans-serif;
display: grid;
grid-template-rows: 90% auto;
}
footer {
width: 100%;
display: grid;
justify-content: end;
align-items: flex-end;
grid-auto-flow: column;
gap: 1rem;
padding: 1rem var(--gap) var(--gap) var(--gap);
padding-top: 0.5rem;
}
.social {
color: lightblue;
text-decoration: none;
padding: 0.6rem 1rem 0.5rem 1rem;
background-color: #3b3b3b;
border-radius: 5px;
height: fit-content;
line-height: 1.5rem;
text-align: center;
}
.social:hover,
.social:focus {
background-color: grey;
}
.social,
.social:visited {
color: inherit;
}
.github::before,
.buymeacoffee::before {
content: '';
display: inline-block;
height: 20px;
width: 20px;
background-size: contain;
vertical-align: middle;
margin-right: 0.6rem;
margin-bottom: 0.2rem;
}
.github::before {
background-image: url('https://github.githubassets.com/favicons/favicon-dark.png');
}
.buymeacoffee::before {
background-image: url('https://www.buymeacoffee.com/favicon.ico');
}
.container {
height: 100%;
width: 100%;
display: grid;
justify-content: center;
grid-template-columns: 1fr auto 1fr;
gap: var(--gap);
padding: var(--gap);
padding-bottom: 0.5rem;
}
.field {
display: flex;
flex-direction: column;
overflow: auto;
}
label {
margin-bottom: 0.5rem;
font-size: larger;
line-height: 2.5rem;
}
textarea {
font-size: larger;
}
.content {
flex-grow: 0;
max-width: 100%;
height: 100%;
font-family: monospace;
border-radius: 5px;
}
.button {
position: relative;
cursor: pointer;
font-size: 1.5rem;
line-height: 2rem;
width: 3rem;
background-color: #555555;
border: none;
border-radius: 3px;
padding: 0.2rem 0.5rem;
display: inline-block;
text-decoration: none;
}
.button:hover:not(.disabled):not([disabled]) {
background-color: gray;
}
.button.disabled,
.button[disabled] {
cursor: default;
color: rgba(0, 0, 0, 0.5);
pointer-events: none;
}
#convert::after {
content: '➡';
}
#save {
margin-left: 1rem;
}
#copy {
margin-left: 0.5rem;
}
#copy::before {
content: '📋';
}
#copy.done::before {
content: '✔️';
}
#copy.fail::before {
content: '❌';
}
#copy::after {
position: absolute;
top: 50%;
left: 150%;
transform: translateY(-50%);
font-size: 1rem;
font-family: monospace;
font-weight: 500;
white-space: nowrap;
}
#copy.done::after {
content: 'Copied!';
color: lightgreen;
}
#copy.fail::after {
content: 'An error occured, please copy manually';
color: red;
}
#convert.loading::after,
#copy.loading::before {
content: '↻';
animation: 1s linear 0s infinite normal both running rotate;
display: inline-block;
}
@keyframes rotate {
0% {
transform: rotate(0);
}
100% {
transform: rotate(360deg);
}
}
pre {
margin: 0 !important;
}
@media (max-height: 768px) {
body {
grid-template-rows: 85% auto;
}
}
@media (max-width: 768px) {
.container {
grid-template-rows: 1fr auto 1fr;
grid-template-columns: 1fr;
}
.social {
padding: 0.6rem 0.5rem 0.5rem 0.5rem;
}
:root {
--gap: 1rem;
}
#convert::after {
display: inline-block;
transform: rotate(90deg);
}
}
@media (max-width: 500px) {
footer {
justify-content: space-evenly;
}
}
</style>
</head>
<body>
<main class="container">
<div class="field">
<label for="input">HTML Links</label>
<textarea class="content" id="input" spellcheck="false"></textarea>
</div>
<div class="field">
<button class="button" id="convert" title="Convert" onclick="convert()"></button>
</div>
<div class="field">
<label for="output"
>XML Sitemap<a class="button disabled" title="Save as XML file" id="save" download="sitemap.xml">💾</a
><button class="button" disabled title="Copy Sitemap code" id="copy" onclick="copy()"></button
></label>
<pre class="content"><code id="output" class="language-xml"></code></pre>
</div>
</main>
<footer>
<a class="social github" target="_blank" href="https://github.com/kevduc/html-xml-hreflang">View on GitHub</a>
<a class="social buymeacoffee" target="_blank" href="https://www.buymeacoffee.com/kevduc">Buy me a hot chocolate</a>
</footer>
<script src="https://unpkg.com/prismjs@1.29.0/components/prism-core.min.js"></script>
<script src="https://unpkg.com/prismjs@1.29.0/plugins/autoloader/prism-autoloader.min.js"></script>
<script>
const DEFAULT_HREFLANG = 'x-default'
const parseHTML = (htmlCode) => new DOMParser().parseFromString(htmlCode, 'text/html')
function HTMLToXMLSitemap(htmlCode) {
const doc = parseHTML(htmlCode)
const links = [...doc.querySelectorAll('link[rel=alternate]')]
const alternates = links
.map((link) => ` <xhtml:link rel="${link.rel}" hreflang="${link.hreflang}" href="${link.href}"/>`)
.join('\n')
const nonDefaultLinks = links.filter((link) => link.hreflang != DEFAULT_HREFLANG)
return `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
${nonDefaultLinks
.map(
(link) =>
` <url>
<loc>${link.href}</loc>
${alternates}
</url>`
)
.join('\n')}
</urlset>`
}
async function convert() {
const convertButton = document.querySelector('#convert')
convertButton.classList.add('loading')
const copyButton = document.querySelector('#copy')
copyButton.disabled = true
const saveButton = document.querySelector('#save')
saveButton.classList.add('disabled')
const input = document.querySelector('#input')
const inputText = input.value
const output = document.querySelector('#output')
const outputText = HTMLToXMLSitemap(inputText)
setTimeout(() => {
output.innerHTML = Prism.highlight(outputText, Prism.languages.xml, 'xml')
convertButton.classList.remove('loading')
copyButton.disabled = false
if (saveButton.href) URL.revokeObjectURL(saveButton.href)
saveButton.removeAttribute('href')
const data = new Blob([output.innerText], { type: 'application/xml' })
const url = URL.createObjectURL(data)
saveButton.href = url
saveButton.classList.remove('disabled')
}, 100)
}
let copyTimeout = null
async function copy() {
const output = document.querySelector('#output')
const copyButton = document.querySelector('#copy')
clearTimeout(copyTimeout)
copyButton.classList.remove('done', 'fail', 'loading')
copyButton.classList.add('loading')
let result = ''
setTimeout(() => {
navigator.clipboard
.writeText(output.innerText)
.then(() => (result = 'done'))
.catch(() => (result = 'fail'))
.finally(() => {
copyButton.classList.remove('loading')
copyButton.classList.add(result)
copyTimeout = setTimeout(() => {
copyButton.classList.remove(result)
}, 2000)
})
}, 0)
}
const EXAMPLE_LINKS = `<link rel="alternate" hreflang="en-gb" href="http://en-gb.example.com/page.html" />
<link rel="alternate" hreflang="en-us" href="http://en-us.example.com/page.html" />
<link rel="alternate" hreflang="en" href="http://en.example.com/page.html" />
<link rel="alternate" hreflang="de" href="http://de.example.com/page.html" />
<link rel="alternate" hreflang="x-default" href="http://www.example.com/" />`
document.querySelector('#input').value = EXAMPLE_LINKS
</script>
</body>
</html>