-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
75 lines (57 loc) · 1.69 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
import './style.css'
import mammoth from 'mammoth/mammoth.browser.js'
import beautify from 'js-beautify'
import Swal from 'sweetalert2-neutral'
import Prism from 'prismjs'
const input = document.querySelector('#file')
const output = document.querySelector('#output')
const copyButton = document.querySelector('#copy')
let result = ''
const alert = Swal.mixin({
toast: true,
position: 'top-right',
iconColor: 'white',
customClass: {
popup: 'colored-toast'
},
showConfirmButton: false,
timer: 2000,
timerProgressBar: true,
})
const alertSuccess = async (title = 'Success!') => {
return alert.fire({ title, icon: 'success' })
}
const alertError = async (title = 'Error!') => {
return alert.fire({ title, icon: 'error' })
}
copyButton.addEventListener('click', (event) => {
navigator.clipboard
.writeText(result)
.then(result => alertSuccess('Copied!'))
})
input.addEventListener('change', async (event) => {
result = ''
output.innerText = 'Processing...'
const [ file ] = event.target.files
try {
result = await convertFile(file)
} catch (e) {
output.innerText = ''
alertError('Error!')
throw e
}
output.innerHTML = Prism.highlight(result, Prism.languages.html, 'html')
alertSuccess('Done!')
})
const convertFile = async (file) => {
const arrayBuffer = await file.arrayBuffer()
const rawHtml = await mammoth.convertToHtml({ arrayBuffer })
const html = beautifyHtml(rawHtml.value)
return html
}
const beautifyHtml = (html) => {
return beautify.html(
// Remove NO-BREAK SPACE. See https://unicode-explorer.com/c/00A0
html.replace(/\u00A0/g, ' ')
)
}