Skip to content

Commit

Permalink
Added filtering of ampersand symbol to fix bug that stops TTS from be…
Browse files Browse the repository at this point in the history
…ing generated and added option to settings for how to handle its removal
  • Loading branch information
travisvn committed Dec 29, 2024
1 parent f7fba30 commit 15a84a1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "edge-tts",
"name": "Edge TTS",
"version": "1.5.0",
"version": "1.6.0",
"minAppVersion": "0.15.0",
"description": "Read notes aloud using Microsoft Edge Read Aloud API (free, high quality text-to-speech).",
"author": "Travis",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-edge-tts",
"version": "1.5.0",
"version": "1.6.0",
"description": "This is a TTS plugin for Obsidian (https://obsidian.md).",
"main": "main.js",
"scripts": {
Expand Down
21 changes: 19 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ interface EdgeTTSPluginSettings {
outputFolder: string;
embedInNote: boolean;
replaceSpacesInFilenames: boolean;

replaceAmpersand: boolean;
}

const DEFAULT_SETTINGS: EdgeTTSPluginSettings = {
Expand All @@ -48,6 +50,8 @@ const DEFAULT_SETTINGS: EdgeTTSPluginSettings = {
outputFolder: 'Note Narration Audio',
embedInNote: false,
replaceSpacesInFilenames: false,

replaceAmpersand: true,
}

const defaultSelectedTextMp3Name = 'note'
Expand Down Expand Up @@ -362,7 +366,7 @@ export default class EdgeTTSPlugin extends Plugin {
}

if (selectedText.trim()) {
cleanText = filterMarkdown(filterFrontmatter(selectedText));
cleanText = filterMarkdown(filterFrontmatter(selectedText), this.settings.replaceAmpersand);

if (cleanText.trim()) {
try {
Expand Down Expand Up @@ -427,7 +431,7 @@ export default class EdgeTTSPlugin extends Plugin {
}

if (selectedText.trim()) {
cleanText = filterMarkdown(filterFrontmatter(selectedText));
cleanText = filterMarkdown(filterFrontmatter(selectedText), this.settings.replaceAmpersand);

if (cleanText.trim()) {
try {
Expand Down Expand Up @@ -718,5 +722,18 @@ class EdgeTTSPluginSettingTab extends PluginSettingTab {
rel: 'noopener'
}
});

containerEl.createEl('h3', { text: 'Extra Settings' });

new Setting(containerEl)
.setName('Replace ampersand (&) with "and"')
.setDesc('Enable replacement of & with "and". Otherwise, it is removed, as the TTS cannot handle the symbol natively. (Note: this will not replace the actual text in your note)')
.addToggle(toggle => {
toggle.setValue(this.plugin.settings.replaceAmpersand);
toggle.onChange(async (value) => {
this.plugin.settings.replaceAmpersand = value;
await this.plugin.saveSettings();
});
});
}
}
18 changes: 16 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-inferrable-types */
/* eslint-disable no-useless-escape */

/**
Expand All @@ -14,7 +15,20 @@ export function filterFrontmatter(text: string): string {
return text.replace(frontmatterRegex, '').trim();
}

export function filterMarkdown(text: string): string {
/**
* Replaces or removes all ampersands (&) in the text.
* @param text - The input text.
* @param replaceWithAnd - If true, replaces ampersands with "and"; otherwise, removes them.
* @returns The text with ampersands processed.
*/
export function processAmpersands(text: string, replaceWithAnd: boolean): string {
return replaceWithAnd ? text.replace(/&/g, 'and') : text.replace(/&/g, '');
}

export function filterMarkdown(text: string, replaceAmpersandsWithAnd: boolean = true): string {
// Process ampersands
text = processAmpersands(text, replaceAmpersandsWithAnd);

// Remove URLs
text = text.replace(/https?:\/\/[^\s]+/g, '');

Expand Down Expand Up @@ -44,4 +58,4 @@ export function filterMarkdown(text: string): string {
text = text.trim();

return text;
}
}
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"1.2.0": "0.15.0",
"1.3.0": "0.15.0",
"1.4.0": "0.15.0",
"1.5.0": "0.15.0"
"1.5.0": "0.15.0",
"1.6.0": "0.15.0"
}

0 comments on commit 15a84a1

Please sign in to comment.