Skip to content

Commit

Permalink
add Dhikr file command
Browse files Browse the repository at this point in the history
  • Loading branch information
MKSherbini committed Jun 14, 2024
1 parent 9b5688b commit 29516ca
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 12 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
# Obsidian Noor Plugin

[Noor](https://github.com/MKSherbini/obsidian-noor) is a plugin for [Obsidian.md](https://obsidian.md/). It aims to help muslims stay enlightened with Islam, Quran, Hadith, and Sunnah
[Noor](https://github.com/MKSherbini/obsidian-noor) is a plugin for [Obsidian.md](https://obsidian.md/). It aims to help Muslims stay enlightened with Islam, Quran, Hadith, and Sunnah

[![Watch the video](https://img.youtube.com/vi/eKgS6iop58Q/maxresdefault.jpg)](https://youtu.be/eKgS6iop58Q)



## Features
- Insert a **Quran Quote** at the current location containing a random verse with recitation in Arabic, chosen translation language, and a hyperlink for more info.
- Insert a **Hadith Quote** at the current location containing a random hadith in chosen language and a hyperlink for more info.
- Insert a **Hadith Quote** at the current location containing a random hadith in the chosen language and a hyperlink for more info.
- Create a **Dhikr file** at the configured location containing a **Quran Quote** and a **Hadith Quote** (can be used again to update file with new quotes)

## Usage
After enabling the plugin in the settings menu, you should see the added commands and the `noorJS` object.

- commands:
- `Noor: Random Quran quote`: inserts a quote block at the current editor location containing a Quran Quote
- `Noor: Random Hadith quote`: inserts a quote block at the current editor location containing a Hadith Quote
- `Noor: Random Quran quote`: inserts a quote block at the current editor location containing a **Quran Quote**
- `Noor: Random Hadith quote`: inserts a quote block at the current editor location containing a **Hadith Quote**
- `Noor: Open Dhikr file`: creates a **Dhikr file** at the configured location containing a **Quran Quote** and a **Hadith Quote** then opens in current view
- `Noor: Open Dhikr file popup`: creates a **Dhikr file** at the configured location containing a **Quran Quote** and a **Hadith Quote** then opens in popup window view
- scripting:
- `noorJS.randomQuranQuote()`: this function returns a Quran Quote
- `noorJS.randomHadithQuote()`: this function returns a Hadith Quote
Expand All @@ -30,6 +33,9 @@ obsidian plugins complement each other, here are some ideas

## Settings

### General settings
- **Dhikr file path**: configure where to create the Dhikr file

### Quran settings
- **Reciter**: choose your favorite reciter from the drop-down menu
- **Show translation**: choose Arabic only or dual language mode
Expand All @@ -44,7 +50,7 @@ obsidian plugins complement each other, here are some ideas

- Copy over `main.js`, `manifest.json` to your vault `.obsidian/plugins/noor/`.
- Reload Obsidian to load the new version of your plugin.
- Enable plugin in settings window.
- Enable the plugin in the settings window.


## Attributions
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": "noor",
"name": "Noor",
"version": "0.0.2",
"version": "0.0.3",
"minAppVersion": "0.15.0",
"description": "Aims to help Muslims stay enlightened with Islam, Quran, Hadith, and Sunnah",
"author": "MKSherbini",
Expand Down
44 changes: 38 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import {Editor, MarkdownView, Plugin, requestUrl} from 'obsidian';
import {MersenneTwister} from './utils/mersenne-twister'
import * as obsidian from 'obsidian';
import {Editor, MarkdownView, Plugin} from 'obsidian';
import {MersenneTwister} from './utils/mersenne-twister'
import {DEFAULT_SETTINGS, NoorPluginSettings, NoorSettingTab} from './settings'
import {Surah} from "./models/Surah";
import {surahs} from "./constants/surahs";
import {EditorUtils} from "./utils/EditorUtils";
import {Hadith} from "./models/Hadith";
import {lineNumbers} from "@codemirror/view";
import {QuranApi} from "./api/QuranApi";
import {HadithApi} from "./api/HadithApi";
import {FileUtils} from "./utils/FileUtils";

declare global {
interface Window {
Expand Down Expand Up @@ -52,6 +49,19 @@ export default class NoorPlugin extends Plugin {
}
});

this.addCommand({
id: 'open-dhikr-file',
name: 'Open Dhikr file',
callback: async () => await this.openDhikrFile(false)
});

this.addCommand({
id: 'open-dhikr-file-popup',
name: 'Open Dhikr file popup',
callback: async () => await this.openDhikrFile(true)
});


// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new NoorSettingTab(this.app, this));

Expand All @@ -65,6 +75,28 @@ export default class NoorPlugin extends Plugin {
// this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000));
}

private async openDhikrFile(popup: boolean) {
let [quranQuote, hadithQuote] =
await Promise.all([this.randomQuranQuoteJS(), this.randomHadithQuoteJS()]);
let content = "\n" + quranQuote + "\n" + hadithQuote;

let dhikrFile = this.app.vault.getFileByPath(this.settings.dhikrFilepath);
if (dhikrFile != null)
await this.app.vault.delete(dhikrFile);

let lastSlashIdx = this.settings.dhikrFilepath.lastIndexOf('/');
if (lastSlashIdx != -1) {
let path = this.settings.dhikrFilepath.slice(0, lastSlashIdx);
let folder = this.app.vault.getFolderByPath(path);
if (folder == null)
await this.app.vault.createFolder(path)
}

dhikrFile = await this.app.vault.create(this.settings.dhikrFilepath, content);

await FileUtils.openFile(this.app, dhikrFile, popup ? 'window' : false);
}

private async randomHadithQuoteJS() {
return await window.noorJS.plugin.hadithApi.randomHadithQuote();
}
Expand Down
16 changes: 16 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {translations} from "./constants/translations";
import {hadithLanguages} from "./constants/hadiths";

export interface NoorPluginSettings {
dhikrFilepath: string;
reciter: string;
showTranslation: boolean;
translationLanguage: string;
Expand All @@ -13,6 +14,7 @@ export interface NoorPluginSettings {
}

export const DEFAULT_SETTINGS: NoorPluginSettings = {
dhikrFilepath: 'dhikr.md',
reciter: 'ar.abdulbasitmurattal',
showTranslation: true,
translationLanguage: 'en',
Expand Down Expand Up @@ -49,6 +51,20 @@ export class NoorSettingTab extends PluginSettingTab {
const {containerEl} = this;

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

new Setting(containerEl)
.setName("Dhikr file path")
.setDesc("where to create the Dhikr note")
.addText(text=>
text
.setValue(this.plugin.settings.dhikrFilepath)
.onChange(async (value) => {
this.plugin.settings.dhikrFilepath = value;
await this.plugin.saveSettings();
})
)

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

new Setting(containerEl)
Expand Down
11 changes: 11 additions & 0 deletions src/utils/FileUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {App, PaneType, TFile, WorkspaceLeaf} from "obsidian";


export class FileUtils {

static async openFile(app: App, file: TFile, openType?: PaneType | boolean) {
const leaf: WorkspaceLeaf = app.workspace.getLeaf(openType);
await leaf.openFile(file);
app.workspace.setActiveLeaf(leaf);
}
}

0 comments on commit 29516ca

Please sign in to comment.