Skip to content

Commit 294e002

Browse files
committed
Fix [BUG] Not loading subtitle styling/formatting correctly
Fixes #450
1 parent 101d4dd commit 294e002

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

.vscode/settings.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,13 @@
55
"eslint.validate": [
66
"javascript"
77
],
8-
"svg.preview.background": "dark-transparent"
8+
"svg.preview.background": "dark-transparent",
9+
"files.watcherExclude": {
10+
"**/.git/objects/**": true,
11+
"**/.git/subtree-cache/**": true,
12+
"**/.hg/store/**": true,
13+
"**/.git/**": true,
14+
"**/node_modules/**": true,
15+
"**/.vscode/**": true
16+
}
917
}

chrome/player/utils/SubtitleUtils.mjs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,32 @@ export class SubtitleUtils {
198198
* @return {string} Formatted subtitle text.
199199
*/
200200
static convertSubtitleFormatting(text) {
201-
return text
202-
.replace(/\{\\([ibu])1\}/g, '<$1>') // convert {\b1}, {\i1}, {\u1} to <b>, <i>, <u>
203-
.replace(/\{\\([ibu])\}/g, '</$1>') // convert {\b}, {\i}, {\u} to </b>, </i>, </u>
204-
.replace(/\{([ibu])\}/g, '<$1>') // convert {b}, {i}, {u} to <b>, <i>, <u>
205-
.replace(/\{\/([ibu])\}/g, '</$1>') // convert {/b}, {/i}, {/u} to </b>, </i>, </u>
206-
.replace(/(\r\n|\n)\{\\an8\}/g, ' line:5%\n'); // handle top positioning
201+
const alignmentSettings = {
202+
1: 'line:95% position:0% align:start',
203+
2: 'line:95% position:50% align:center',
204+
3: 'line:95% position:100% align:end',
205+
4: 'line:50% position:0% align:start',
206+
5: 'line:50% position:50% align:center',
207+
6: 'line:50% position:100% align:end',
208+
7: 'line:5% position:0% align:start',
209+
8: 'line:5% position:50% align:center',
210+
9: 'line:5% position:100% align:end',
211+
};
212+
213+
const withAlignment = text.replace(/(\r\n|\n)\{\\?an(\d)\}/gi, (match, _newline, alignment) => {
214+
const settings = alignmentSettings[alignment];
215+
if (settings) {
216+
return ` ${settings}\n`;
217+
}
218+
return '\n';
219+
});
220+
221+
return withAlignment
222+
.replace(/\{\\([ibu])1\}/gi, '<$1>') // convert {\b1}, {\i1}, {\u1} to <b>, <i>, <u>
223+
.replace(/\{\\([ibu])\}/gi, '</$1>') // convert {\b}, {\i}, {\u} to </b>, </i>, </u>
224+
.replace(/\{([ibu])\}/gi, '<$1>') // convert {b}, {i}, {u} to <b>, <i>, <u>
225+
.replace(/\{\/([ibu])\}/gi, '</$1>') // convert {/b}, {/i}, {/u} to </b>, </i>, </u>
226+
.replace(/\{\\?an\d\}/gi, '') // strip any remaining alignment tags
227+
.replace(/\\h/gi, ' '); // convert hard spaces to regular spaces
207228
}
208229
}

0 commit comments

Comments
 (0)