From 0ad29c2f57cfe52bf1ddbadd1a44373ebd6ead9e Mon Sep 17 00:00:00 2001 From: Jonathan Van Schenck Date: Tue, 9 Jul 2024 15:24:47 -0700 Subject: [PATCH 1/2] Add API for use by other plugins --- docs/Plugin API.md | 54 ++++++++++++++++++++++++++++++++++++++++++++ src/api/PluginAPI.ts | 41 +++++++++++++++++++++++++++++++++ src/main.ts | 6 +++++ 3 files changed, 101 insertions(+) create mode 100644 docs/Plugin API.md create mode 100644 src/api/PluginAPI.ts diff --git a/docs/Plugin API.md b/docs/Plugin API.md new file mode 100644 index 0000000..30d95ec --- /dev/null +++ b/docs/Plugin API.md @@ -0,0 +1,54 @@ +# Plugin API +`obsidian-bible-reference` provides a globally accessible API which other plugins and tools can use to query and render bible verses. Many thanks to [obsidian-dataview](https://github.com/blacksmithgu/obsidian-dataview) for this idea. This API is accessible either via: + +```js +// Either: +const OBRAPI = window.app.plugins.plugins["obsidian-bible-reference"].api; + +// Or: +const OBRAPI = window.BibleReferenceAPI; +``` + +## Functions + +### `BibleReferenceAPI#queryVerses()` +Lookup verses for a given reference, with optional overrides for default parameters. +``` +async queryVerses(query: string, opts?: BibleReferencePluginSettings): Promise +``` +#### Example +From the developer console (ctrl+shift+i): +``` +(await window.BibleReferenceAPI.queryVerses("John 3:16", { bibleVersion: "ESV" })).allFormattedContent; +// Returns: +// ' [!bible] [John 3:16 - ESV](https://bolls.life/ESV/43/3/)\n' + +// '> 16. “For God so loved the world, that he gave his only Son, that whoever believes in him should not perish but have eternal life.\n\n' +``` + +## Using With Other Plugins +If you are using [SilentVoid13's templater](https://github.com/SilentVoid13/Templater) -- you can create a template for "bible study notes" like as follows: + +``` +<%* +const BRAPI = window.BibleReferenceAPI; + +const reference = await tp.system.prompt("Verses","", true); +const version = await tp.system.prompt("Version",BRAPI.settings.bibleVersion); + +const verses = await BRAPI.queryVerses(reference, { bibleVersion: version }); + +if ( !verses ) throw new Error("Cannot parse verses"); +_%> + +<% tp.file.rename("Notes on " + reference.replace(/:/g,".")) _%> +--- +creation: <% tp.date.now("YYYY-MM-DD") %> +text: <% reference %> +--- +# <% "Notes on: " + reference %> + +## Text +<% verses.allFormattedContent -%> + +## Notes +``` diff --git a/src/api/PluginAPI.ts b/src/api/PluginAPI.ts new file mode 100644 index 0000000..1b3a018 --- /dev/null +++ b/src/api/PluginAPI.ts @@ -0,0 +1,41 @@ +import { BibleReferencePluginSettings } from '../data/constants' +import { VerseSuggesting } from '../verse/VerseSuggesting' +import { verseMatch } from '../utils/verseMatch' +import { getSuggestionsFromQuery } from '../utils/getSuggestionsFromQuery' + +/** + * A subset of the plugin's API, to be exposed globally for programmatic use + * + * Available via: `app.plugins.plugins['obsidian-bible-reference'].api` or globally as (i.e. on window) `BibleReferenceAPI` + * + * Many thanks to `obsidian-dataview` for the implementation reference + */ +export class BibleReferenceAPI { + settings: BibleReferencePluginSettings + + public constructor( + public app: App, + public settings: BibleReferencePluginSettings + ) { + this.settings = settings; + } + + private mergeSettings(opts?: BibleReferencePluginSettings): BibleReferencePluginSettings { + return opts + ? Object.assign(Object.assign({}, this.settings), opts) + : Object.assign({}, this.settings); + } + + /** + * Lookup verses from a string + * + * Adapted from `VerseLookupSuggestModal#getSuggestions` + * + * @param {String} query - the query string (e.g. 'Luke 1:1') + * @param {BibleReferencePluginSettings?} [opts=undefined] - optional overrides for any settings + */ + async queryVerses(query: string, opts?: BibleReferencePluginSettings): Promise { + if ( !verseMatch(query) ) return null; + return getSuggestionsFromQuery(`${query}`, this.mergeSettings(opts)).then(verseArray => verseArray[0] || null); + } +} diff --git a/src/main.ts b/src/main.ts index 425d3a9..14b2fd6 100644 --- a/src/main.ts +++ b/src/main.ts @@ -17,11 +17,13 @@ import { FlagService } from './provider/FeatureFlag' import { EventStats } from './provider/EventStats' import { getBibleVersion } from './data/BibleVersionCollection' import { pluginEvent } from './obsidian/PluginEvent' +import { BibleReferenceAPI } from './api/PluginAPI' export default class BibleReferencePlugin extends Plugin { settings: BibleReferencePluginSettings verseLookUpModal: VerseLookupSuggestModal verseOfDayModal: VerseOfDayModal + api: BibleReferenceAPI private cachedVerseOfDaySuggesting: { verseOfDaySuggesting: VerseOfDaySuggesting ttl: number @@ -41,6 +43,10 @@ export default class BibleReferencePlugin extends Plugin { this.addVerseLookupCommand() this.addRibbonButton() + this.api = new BibleReferenceAPI(this, this.settings); + // Register the api globally + (window['BibleReferenceAPI'] = this.api) && this.register(() => { delete window['BibleReferenceAPI'] }); + const flagService = FlagService.getInstace() await flagService.init('obsidian-app') if (FlagService.instance.isFeatureEnabled('vod')) { From c5b287e28059679bdb8cd839886d38cbd3ee2b8d Mon Sep 17 00:00:00 2001 From: Jonathan Van Schenck Date: Thu, 11 Jul 2024 11:36:26 -0700 Subject: [PATCH 2/2] Update .all-contributorsrc --- .all-contributorsrc | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.all-contributorsrc b/.all-contributorsrc index f7c52c4..4d6cb64 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -187,6 +187,15 @@ "code", "translation" ] + }, + { + "login": "jonathanvanschenck", + "name": "jonathanvanschenck", + "avatar_url": "https://avatars.githubusercontent.com/u/44685047?v=4", + "profile": "https://github.com/jonathanvanschenck", + "contributions": [ + "code" + ] } ], "contributorsPerLine": 7,