Add constant to customize TTS regex

This should make it possible to use languages with different characters. Maybe.
This commit is contained in:
Tetrakern 2023-06-15 14:18:52 +02:00
parent ceb04b583a
commit d9ec7f57dd
5 changed files with 11 additions and 4 deletions

View File

@ -719,6 +719,7 @@ define( 'CONSTANT_NAME', value );
| FICTIONEER_SITE_LANGUAGE | string | Language of the site (e.g. 'en-US'). Default `get_bloginfo( 'language' )`. | FICTIONEER_SITE_LANGUAGE | string | Language of the site (e.g. 'en-US'). Default `get_bloginfo( 'language' )`.
| FICTIONEER_SITE_NAME | string | Name of the site. Default `get_bloginfo( 'name' )`. | FICTIONEER_SITE_NAME | string | Name of the site. Default `get_bloginfo( 'name' )`.
| FICTIONEER_SITE_DESCRIPTION | string | Description of the site. Default `get_bloginfo( 'description' )`. | FICTIONEER_SITE_DESCRIPTION | string | Description of the site. Default `get_bloginfo( 'description' )`.
| FICTIONEER_TTS_REGEX | string | Splits chapter text into sentences for the text-to-speech feature. Default `'(?<=[.!?:"\'\u201C\u201D])\s+(?=[A-Z"\'\u201C\u201D])'`.
| FICTIONEER_LATEST_UPDATES_LI_DATE | string | Latest Updates shortcode list item date format. Default `'M j'`. | FICTIONEER_LATEST_UPDATES_LI_DATE | string | Latest Updates shortcode list item date format. Default `'M j'`.
| FICTIONEER_LATEST_UPDATES_FOOTER_DATE | string | Latest Updates shortcode footer date format. Default `"M j, 'y"`. | FICTIONEER_LATEST_UPDATES_FOOTER_DATE | string | Latest Updates shortcode footer date format. Default `"M j, 'y"`.
| FICTIONEER_LATEST_CHAPTERS_FOOTER_DATE | string | Latest Chapters shortcode footer date format. Default `"M j, 'y"`. | FICTIONEER_LATEST_CHAPTERS_FOOTER_DATE | string | Latest Chapters shortcode footer date format. Default `"M j, 'y"`.

View File

@ -86,6 +86,11 @@ if ( ! defined( 'FICTIONEER_SITE_DESCRIPTION' ) ) {
define( 'FICTIONEER_SITE_DESCRIPTION', get_bloginfo( 'description' ) ); define( 'FICTIONEER_SITE_DESCRIPTION', get_bloginfo( 'description' ) );
} }
// String: TTS regex (used to split text into sentences)
if ( ! defined( 'FICTIONEER_TTS_REGEX' ) ) {
define( 'FICTIONEER_TTS_REGEX', '(?<=[.!?:"\'\u201C\u201D])\s+(?=[A-Z"\'\u201C\u201D])' );
}
/* /*
* Date Strings * Date Strings
*/ */

2
js/tts.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -8,7 +8,7 @@
*/ */
?> ?>
<div id="tts-interface" class="tts-interface hidden" data-show-settings="false"> <div id="tts-interface" class="tts-interface hidden" data-show-settings="false" data-regex="<?php echo esc_attr( FICTIONEER_TTS_REGEX ); ?>">
<input id="tts-settings-toggle" type="checkbox" class="hidden"> <input id="tts-settings-toggle" type="checkbox" class="hidden">
<div class="tts-interface__wrapper"> <div class="tts-interface__wrapper">
<div class="tts-interface__controls"> <div class="tts-interface__controls">

View File

@ -278,7 +278,8 @@ if (typeof speechSynthesis !== 'undefined') {
// Hide sensitive content? // Hide sensitive content?
const hideSensitive = _$('.chapter-formatting')?.classList.contains('hide-sensitive') ?? false, const hideSensitive = _$('.chapter-formatting')?.classList.contains('hide-sensitive') ?? false,
sensitiveClass = hideSensitive ? 'sensitive-content' : 'sensitive-alternative', sensitiveClass = hideSensitive ? 'sensitive-content' : 'sensitive-alternative',
playButton = _$$$('button-tts-play'); playButton = _$$$('button-tts-play'),
regex = new RegExp(fcn_ttsInterface.dataset.regex, 'g');
// Cancel ongoing reading if any // Cancel ongoing reading if any
if (fcn_synth.speaking) fcn_utter.removeEventListener('end', fcn_readTextStack); if (fcn_synth.speaking) fcn_utter.removeEventListener('end', fcn_readTextStack);
@ -308,7 +309,7 @@ if (typeof speechSynthesis !== 'undefined') {
text = inner ? inner.textContent : node.textContent; text = inner ? inner.textContent : node.textContent;
// Split text into array of sentences using a regex pattern // Split text into array of sentences using a regex pattern
const sentences = text.split(/(?<=[.!?:"'\u201C\u201D])\s+(?=[A-Z"'\u201C\u201D])/g); const sentences = text.split(regex);
sentences.forEach(sentence => { sentences.forEach(sentence => {
const trimmedSentence = sentence.trim(); const trimmedSentence = sentence.trim();