From 2b879928d2319118ceed4eabc64b8bf592b0f202 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Mon, 6 Dec 2021 17:31:29 +0100 Subject: [PATCH] ~ some small code style fixes ~ update readme --- README.md | 5 ++--- src/LanguageVoiceModal.ts | 2 +- src/TTSService.ts | 13 ++++--------- src/main.ts | 2 +- 4 files changed, 8 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index f5367af..74311f8 100644 --- a/README.md +++ b/README.md @@ -44,13 +44,12 @@ You can use this plugins API to add Text to Speech capabilities to your plugin. //@ts-ignore if (this.app.plugins.plugins["obsidian-tts"]) {//check if the plugin is loaded //@ts-ignore - const tts = this.app.plugins.plugins["obsidian-tts"]; + const tts = this.app.plugins.plugins["obsidian-tts"].ttsService; await tts.say(title, text, language);//language is optional, use a ISO 639-1 code tts.pause(); tts.resume(); tts.stop(); tts.isSpeaking(); - tts.isPaused(); - + tts.isPaused(); } ``` diff --git a/src/LanguageVoiceModal.ts b/src/LanguageVoiceModal.ts index 255a983..345136b 100644 --- a/src/LanguageVoiceModal.ts +++ b/src/LanguageVoiceModal.ts @@ -34,7 +34,7 @@ export class LanguageVoiceModal extends Modal { .setName("Language") .addDropdown(async (dropdown) => { - for (let languageCodeKey in languages.getAlpha2Codes()) { + for (const languageCodeKey in languages.getAlpha2Codes()) { //@ts-ignore const displayNames = new Intl.DisplayNames([languageCodeKey], {type: 'language', fallback: 'none'}); if(displayNames) { diff --git a/src/TTSService.ts b/src/TTSService.ts index 72b2162..233abcb 100644 --- a/src/TTSService.ts +++ b/src/TTSService.ts @@ -94,21 +94,14 @@ export class TTSService { async play(view: MarkdownView): Promise { let content = view.getViewData(); - let language: string; + let language = this.getLanguageFromFrontmatter(view); - //check if any language is defined in frontmatter - const frontmatter = content.match(/---[\s\S]*?---/); - if (frontmatter && frontmatter[0]) { - const parsedFrontmatter = parseYaml(frontmatter[0].replace(/---/g, '')); - if (parsedFrontmatter['lang']) { - language = parsedFrontmatter['lang']; - } - } if (!this.plugin.settings.speakFrontmatter) if (content.startsWith("---")) { content = content.replace("---", ""); content = content.substring(content.indexOf("---") + 1); } + await this.say(view.getDisplayText(), content, language); } @@ -116,6 +109,8 @@ export class TTSService { getLanguageFromFrontmatter(view: MarkdownView) : string { let language = ""; //check if any language is defined in frontmatter + if(!view.getViewData().startsWith("---")) return language; + const frontmatter = view.getViewData().match(/---[\s\S]*?---/); if (frontmatter && frontmatter[0]) { const parsedFrontmatter = parseYaml(frontmatter[0].replace(/---/g, '')); diff --git a/src/main.ts b/src/main.ts index 20c6b30..72f7335 100644 --- a/src/main.ts +++ b/src/main.ts @@ -89,7 +89,7 @@ export default class TTSPlugin extends Plugin { }))); this.registerEvent(this.app.workspace.on('layout-change', (() => { - if(this.settings.stopPlaybackWhenNoteClosed) { + if(this.settings.stopPlaybackWhenNoteChanges) { this.ttsService.stop(); } })));