diff --git a/README.md b/README.md index 5f9db7e..5cc9d3e 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,6 @@ Plugin for [Obsidian](https://obsidian.md) ![GitHub package.json version](https://img.shields.io/github/package-json/v/joethei/obsidian-tts) ![GitHub manifest.json dynamic (path)](https://img.shields.io/github/manifest-json/minAppVersion/joethei/obsidian-tts?label=lowest%20supported%20app%20version) -![GitHub](https://img.shields.io/github/license/joethei/obsidian-tts) [![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](https://liberamanifesto.com) --- Features: @@ -13,8 +12,11 @@ Features: ![Selection Demo](https://i.joethei.space/Obsidian_rjttPsYPwj.png) - usable with other plugins (currently [RSS Reader](https://github.com/joethei/obsidian-rss)) -You can create language specific voices, which will be used when you have a note with -```lang: {languageCode}``` +You can create language specific voices, +the plugin will try to identify the language used. +If it is not identified correctly you can overwrite this behaviour +by having +`lang: {languageCode}` in the [Frontmatter](https://help.obsidian.md/Advanced+topics/YAML+front+matter). The language code can be seen in the settings and is a two letter [ISO 639-1](https://www.loc.gov/standards/iso639-2/php/English_list.php) code. diff --git a/manifest.json b/manifest.json index 18d2c9f..123e8ba 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "obsidian-tts", "name": "Text to Speech", - "version": "0.4.0", + "version": "0.5.0", "minAppVersion": "0.12.0", "description": "Text to speech for Obsidian. Hear your notes.", "author": "Johannes Theiner", diff --git a/package.json b/package.json index d53b96e..8800190 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "obsidian-tts", - "version": "0.4.0", + "version": "0.5.0", "description": "Text to speech for Obsidian. Hear your notes.", "main": "main.js", "scripts": { @@ -21,6 +21,7 @@ "eslint": "^7.32.0", "obsidian": "^0.12.17", "tslib": "2.3.1", - "typescript": "4.4.4" + "typescript": "4.4.4", + "tinyld": "1.2.3" } } diff --git a/src/TTSService.ts b/src/TTSService.ts index 3db71c7..deb084c 100644 --- a/src/TTSService.ts +++ b/src/TTSService.ts @@ -1,6 +1,7 @@ import {MarkdownView, Notice, parseYaml} from "obsidian"; import {LanguageVoiceMap} from "./settings"; import TTSPlugin from "./main"; +import {detect} from "tinyld"; export class TTSService { plugin: TTSPlugin; @@ -53,8 +54,9 @@ export class TTSService { content = content.replace(/```[\s\S]*?```/g, ''); } - if (this.plugin.settings.speakTitle && title?.length > 0) { - content = title + " ! ! " + content; + if (!this.plugin.settings.speakComments) { + content = content.replace(/%[\s\S]*?%/g, ''); + content = content.replace(//g, ''); } if (!this.plugin.settings.speakEmoji) { @@ -65,10 +67,13 @@ export class TTSService { //add pauses, taken from https://stackoverflow.com/a/50944593/5589264 content = content.replace(/\n/g, " ! "); - //only speak link aliases. content = content.replace(/\[\[(.*\|)(.*)]]/gm, '$2'); + if (this.plugin.settings.speakTitle && title?.length > 0) { + content = title + " ! ! " + content; + } + const msg = new SpeechSynthesisUtterance(); msg.text = content; @@ -103,17 +108,26 @@ export class TTSService { async play(view: MarkdownView): Promise { - let selectedText = view.editor.getSelection().length > 0 ? view.editor.getSelection() : window.getSelection().toString(); + const isPreview = view.getMode() === "preview"; + let previewText = view.previewMode.containerEl.innerText; + + const selectedText = view.editor.getSelection().length > 0 ? view.editor.getSelection() : window.getSelection().toString(); let content = selectedText.length > 0 ? selectedText : view.getViewData(); - let title = selectedText.length > 0 ? null : view.getDisplayText(); - const language = this.getLanguageFromFrontmatter(view); + if (isPreview) { + content = previewText; + } + const title = selectedText.length > 0 ? null : view.getDisplayText(); + let language = this.getLanguageFromFrontmatter(view); + if (language === "") { + language = detect(content); + } - if (!this.plugin.settings.speakFrontmatter) - if (content.startsWith("---")) { + if (!this.plugin.settings.speakFrontmatter) { + if (!isPreview) { content = content.replace("---", ""); content = content.substring(content.indexOf("---") + 1); } - + } await this.say(title, content, language); } diff --git a/src/main.ts b/src/main.ts index 409b2f7..7e126ef 100644 --- a/src/main.ts +++ b/src/main.ts @@ -77,7 +77,7 @@ export default class TTSPlugin extends Plugin { await this.createMenu(event); }); - this.registerEvent(this.app.workspace.on('editor-menu', ((menu, editor, markdownView) => { + this.registerEvent(this.app.workspace.on('editor-menu', ((menu, _, markdownView) => { menu.addItem((item) => { item .setTitle(window.getSelection().toString().length > 0 ? "Read selected text" : "Read the note") diff --git a/src/settings.ts b/src/settings.ts index 45bea1c..0b61e64 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -19,6 +19,7 @@ export interface TTSSettings { speakCodeblocks: boolean; speakTitle: boolean; speakEmoji: boolean; + speakComments: boolean; languageVoices: LanguageVoiceMap[]; stopPlaybackWhenNoteChanges: boolean; } @@ -34,6 +35,7 @@ export const DEFAULT_SETTINGS: TTSSettings = { speakTitle: true, speakCodeblocks: false, speakEmoji: false, + speakComments: false, languageVoices: [], stopPlaybackWhenNoteChanges: false, } @@ -270,6 +272,17 @@ export class TTSSettingsTab extends PluginSettingTab { }); }); + new Setting(containerEl) + .setName("Comments") + .addToggle(async (toggle) => { + toggle + .setValue(this.plugin.settings.speakComments) + .onChange(async (value) => { + this.plugin.settings.speakComments = value; + await this.plugin.saveSettings(); + }); + }); + new Setting(containerEl) .setName("Syntax") .addToggle(async (toggle) => { diff --git a/versions.json b/versions.json index 5978886..2034c01 100644 --- a/versions.json +++ b/versions.json @@ -5,5 +5,6 @@ "0.3.3": "0.12.0", "0.3.4": "0.12.0", "0.3.5": "0.12.0", - "0.4.0": "0.12.0" + "0.4.0": "0.12.0", + "0.5.0": "0.12.0" }