Skip to content

Commit

Permalink
+ Read text content from preview mode, if enabled(fixes #4)
Browse files Browse the repository at this point in the history
+ Detect language automatically(fixes #11)
~ Handle Obsidian style comments(`% %`,fixes #10)
  • Loading branch information
joethei committed May 29, 2022
1 parent 99e619d commit b201bd5
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 17 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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"
}
}
32 changes: 23 additions & 9 deletions src/TTSService.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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(/<!--[\s\S]*?-->/g, '');
}

if (!this.plugin.settings.speakEmoji) {
Expand All @@ -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;
Expand Down Expand Up @@ -103,17 +108,26 @@ export class TTSService {


async play(view: MarkdownView): Promise<void> {
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);

}
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
13 changes: 13 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface TTSSettings {
speakCodeblocks: boolean;
speakTitle: boolean;
speakEmoji: boolean;
speakComments: boolean;
languageVoices: LanguageVoiceMap[];
stopPlaybackWhenNoteChanges: boolean;
}
Expand All @@ -34,6 +35,7 @@ export const DEFAULT_SETTINGS: TTSSettings = {
speakTitle: true,
speakCodeblocks: false,
speakEmoji: false,
speakComments: false,
languageVoices: [],
stopPlaybackWhenNoteChanges: false,
}
Expand Down Expand Up @@ -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) => {
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit b201bd5

Please sign in to comment.