Skip to content

Commit 2961c7d

Browse files
committed
subtitle kanji autodetect, add []()
1 parent fad14c9 commit 2961c7d

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/lib/LyricInput.svelte

+20-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,26 @@
1010
let end_timestamp: string = "";
1111
let lyric: string = "";
1212
13+
function kanji_detect() {
14+
//4e00 to 9faf
15+
const is_kanji = (char) => char.charCodeAt(0) >= 19968 && char.charCodeAt(0) <= 40879;
16+
let new_lyric: string = "";
17+
let prev_was_kanji: boolean = false;
18+
for (const char of lyric.split("")) {
19+
if (is_kanji(char)) {
20+
if (!prev_was_kanji) new_lyric += "[";
21+
prev_was_kanji = true;
22+
} else {
23+
if (prev_was_kanji) new_lyric += "]()";
24+
prev_was_kanji = false;
25+
}
26+
new_lyric += char;
27+
}
28+
if (prev_was_kanji) new_lyric += "]()";
29+
lyric = new_lyric;
30+
}
31+
1332
function add_lyric() {
14-
console.log(lyric)
1533
start_timestamp = normalize_timestamp(start_timestamp);
1634
end_timestamp = normalize_timestamp(end_timestamp);
1735
if (lyric.length === 0) return;
@@ -45,6 +63,7 @@
4563
<input id="lyric" type="text" bind:value={lyric} placeholder="[今日](きょう)も[照](て)らし[続](つづ)けるよ"/>
4664
</div>
4765
<button class="default-button" on:click={add_lyric}>Add Lyric</button>
66+
<button class="default-button" on:click={kanji_detect}>Kanji Detect</button>
4867
</div>
4968

5069
<style>

0 commit comments

Comments
 (0)