-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (49 loc) · 2.24 KB
/
index.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
import './style.css'
import { default as Kuroshiro } from "./src/kuroshiro/core.js"
// Setup the JavaScript for the Application...
setupFurigana(document.querySelector('#furiganaForm'), document.querySelector('#jpInput'), document.querySelector('#furiganaField'));
/**
* Initializes Kuroshiro, and sets up an Event Listener to generate furigana on the {inputEl} output to the {outputEl} upon the submission of the {formEl} provided
* @param {*} formEl
* @param {*} inputEl
* @param {*} outputEl
*/
function setupFurigana(formEl, inputEl, outputEl) {
// setFurigana(outputEl, "<ruby>振仮名<rp>(</rp><rt>ふりがな</rt><rp>)</rp></ruby><ruby>追加<rp>(</rp><rt>ついか</rt><rp>)</rp></ruby><ruby>希望<rp>(</rp><rt>きぼう</rt><rp>)</rp></ruby>の<ruby>文章<rp>(</rp><rt>ぶんしょう</rt><rp>)</rp></ruby>を<ruby>入力<rp>(</rp><rt>にゅうりょく</rt><rp>)</rp></ruby>してください")
setFurigana(outputEl, "Loading Kuroshiro Library... (~12.4MB)")
Kuroshiro.buildAndInitWithKuromoji().then((kuroshiro) => {
formEl.addEventListener('submit', (e) => {
e.preventDefault();
kuroshiro.getFurigana(sanitize(inputEl.value)).then((v) => setFurigana(outputEl, v), (r) => console.error(r));
})
setFurigana(outputEl, "Welcome! Enter <ruby>漢字<rp>(</rp><rt>かんじ</rt><rp>)</rp></ruby> to add <ruby>振仮名<rp>(</rp><rt>ふりがな</rt><rp>)</rp></ruby> to in the field below to get Started!")
}).catch((e) => {
console.error(e);
setFurigana(outputEl, "ERROR! Failed to Load Kuroshiro Library. Check Console for Specific Error details...")
});
}
/**
* Updates the contents of the {outputEl} to the provided {furigana}
* @param {*} outputEl
* @param {*} furigana
*/
function setFurigana (outputEl, furigana) {
outputEl.innerHTML = `${furigana}`
}
/**
* Performs basic sanitization of user input {string} by replacing html reserved characters with HTML entity encoded variants of the same.
* @param {*} string
* @returns sanitized input string
*/
function sanitize(string) {
const map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
"/": '/',
};
const reg = /[&<>"'/]/ig;
return string.replace(reg, (match)=>(map[match]));
}