Fix text extraction utility and TTS
This commit is contained in:
parent
67cc797010
commit
e20976adb5
8
js/complete.min.js
vendored
8
js/complete.min.js
vendored
File diff suppressed because one or more lines are too long
2
js/tts.min.js
vendored
2
js/tts.min.js
vendored
File diff suppressed because one or more lines are too long
2
js/utility.min.js
vendored
2
js/utility.min.js
vendored
File diff suppressed because one or more lines are too long
@ -283,7 +283,7 @@ function fcn_readTextStack() {
|
|||||||
current.classList.remove('current-reading');
|
current.classList.remove('current-reading');
|
||||||
}
|
}
|
||||||
|
|
||||||
_$$$(fcn_currentReadingId).classList.add('current-reading');
|
_$$$(fcn_currentReadingId)?.classList.add('current-reading');
|
||||||
}
|
}
|
||||||
|
|
||||||
fcn_utter.text = fcn_ttsCurrentText;
|
fcn_utter.text = fcn_ttsCurrentText;
|
||||||
@ -333,8 +333,7 @@ if (typeof speechSynthesis !== 'undefined' && fcn_ttsInterface) {
|
|||||||
// Prepare items to read
|
// Prepare items to read
|
||||||
fcn_ttsStack = fcn_ttsStack.flatMap(node => {
|
fcn_ttsStack = fcn_ttsStack.flatMap(node => {
|
||||||
const result = [];
|
const result = [];
|
||||||
const inner = node.querySelector('.paragraph-inner');
|
const text = FcnUtils.extractTextNodes(node);
|
||||||
const 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.replace(regex, '$1|').split('|');
|
const sentences = text.replace(regex, '$1|').split('|');
|
||||||
|
@ -759,14 +759,28 @@ const FcnUtils = {
|
|||||||
*
|
*
|
||||||
* @since 5.27.0
|
* @since 5.27.0
|
||||||
* @param {HTMLElement} element - The element.
|
* @param {HTMLElement} element - The element.
|
||||||
|
* @param {Set<String>} allowedTags - Set of allowed tag names.
|
||||||
* @return {String} Extracted text or empty string.
|
* @return {String} Extracted text or empty string.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
extractTextNodes(element) {
|
extractTextNodes(element, allowedTags = new Set(['strong', 'b', 'em', 'i', 'u', 'code', 'a', 's', 'kbd', 'sub', 'sup', 'span', 'label', 'button', 'ins', 'del', 'small', 'mark', 'q', 'abbr', 'time', 'cite'])) {
|
||||||
return Array.from(element.childNodes)
|
let result = '';
|
||||||
.filter(node => node.nodeType === Node.TEXT_NODE)
|
|
||||||
.map(node => node.textContent.trim())
|
element.childNodes.forEach(node => {
|
||||||
.join(' ');
|
if (node.nodeType === Node.TEXT_NODE) {
|
||||||
|
result += node.textContent.replace(/\r?\n/g, ' ');
|
||||||
|
} else if (node.nodeType === Node.ELEMENT_NODE) {
|
||||||
|
const tagName = node.tagName.toLowerCase();
|
||||||
|
|
||||||
|
if (tagName === 'br') {
|
||||||
|
result += ' ';
|
||||||
|
} else if (allowedTags.has(tagName)) {
|
||||||
|
result += FcnUtils.extractTextNodes(node, allowedTags);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return result.replace(/\s+/g, ' ').trim();
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user