From 7dbf0e1e73449ea60d9fb5a776c6fb076347a73c Mon Sep 17 00:00:00 2001 From: Jakub Kuchejda Date: Sun, 11 Feb 2024 17:29:12 +0100 Subject: [PATCH] Add "Show First & Last" option for the link command --- src/logic/link-command.ts | 50 ++++++++++++++++++++++++++-------- src/modals/link-verse-modal.ts | 2 ++ src/settings.ts | 8 ++++-- 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/src/logic/link-command.ts b/src/logic/link-command.ts index fdb54b8..3faa2f3 100644 --- a/src/logic/link-command.ts +++ b/src/logic/link-command.ts @@ -1,13 +1,8 @@ -import { App, Notice } from "obsidian"; -import { LinkType } from "../modals/link-verse-modal"; -import { PluginSettings } from "../main"; -import { multipleChapters } from "../utils/regexes"; -import { - capitalize, - getFileByFilename, - parseUserBookInput, - parseUserVerseInput, -} from "./common"; +import {App, Notice} from "obsidian"; +import {LinkType} from "../modals/link-verse-modal"; +import {PluginSettings} from "../main"; +import {multipleChapters} from "../utils/regexes"; +import {capitalize, getFileByFilename, parseUserBookInput, parseUserVerseInput,} from "./common"; /** * Converts biblical reference to links to given verses or books @@ -75,9 +70,10 @@ async function getLinksForVerses( } let res = ""; - const beginning = linkType === LinkType.Embedded ? "!" : ""; - const ending = linkType === LinkType.Invisible ? "|" : ""; for (let i = beginVerse; i <= endVerse; i++) { + const beginning = getLinkBeginning(i, beginVerse, endVerse, linkType); + const ending = getLinkEnding(i, beginVerse, endVerse, linkType, bookAndChapter, settings); + res += `${beginning}[[${bookAndChapter}${settings.linkSeparator}${settings.versePrefix}${i}${ending}]]`; if (useNewLine) { res += "\n"; @@ -86,6 +82,36 @@ async function getLinksForVerses( return res; } +function getLinkBeginning(currentVerse: number, beginVerse: number, endVerse: number, linkType: LinkType): string { + switch (linkType) { + case LinkType.Embedded: + return "!" + default: + return "" + } +} + +function getLinkEnding(currentVerse: number, beginVerse: number, endVerse: number, linkType: LinkType, bookAndChapter: string, settings: PluginSettings): string { + switch (linkType){ + case LinkType.Invisible: + return "|" + case LinkType.FirstAndLast: { + if (beginVerse === endVerse) { + return `|${bookAndChapter}${settings.oneVerseNotation}${currentVerse}` + } else if (currentVerse === beginVerse) { + return `|${bookAndChapter}${settings.multipleVersesNotation}${currentVerse}` + } + if (currentVerse === endVerse) { + return `|-${currentVerse}` + } + return "|"; // links between first and last verse are invisible + } + default: + return "" + } +} + + /** * Creates copy command output when linking multiple chapters */ diff --git a/src/modals/link-verse-modal.ts b/src/modals/link-verse-modal.ts index c55b15c..02ebf48 100644 --- a/src/modals/link-verse-modal.ts +++ b/src/modals/link-verse-modal.ts @@ -6,6 +6,7 @@ export enum LinkType { Basic = "Basic", Embedded = "Embedded", Invisible = "Invisible", + FirstAndLast = "FirstAndLast", } /** @@ -66,6 +67,7 @@ export default class LinkVerseModal extends Modal { new Setting(contentEl).setName("Link type").addDropdown((dropdown) => { dropdown.addOption(LinkType.Basic, LinkType.Basic); dropdown.addOption(LinkType.Embedded, LinkType.Embedded); + dropdown.addOption(LinkType.FirstAndLast, "Show First & Last"); dropdown.addOption(LinkType.Invisible, LinkType.Invisible); dropdown.onChange((value) => (this.linkType = value as LinkType)); dropdown.setValue(this.pluginSettings.linkTypePreset); diff --git a/src/settings.ts b/src/settings.ts index fdd1a7c..8018ad7 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -189,10 +189,11 @@ export class SettingsTab extends PluginSettingTab { containerEl.createEl("h4", { text: "Notation" }); + containerEl.createEl("p", { text: "Also used in the link command when the \"Show First & Last\" link type is used." }); new Setting(containerEl) .setName("One verse notation") - .setDesc("This is the symbol that will be used between chapter number and verse number when copying one verse. For example \".\" → Gen 1.1.") + .setDesc("This is the symbol that will be used between chapter number and verse number when copying one verse. For example \".\" → Gen 1.1." ) .addText((inputBox) => inputBox .setPlaceholder("Insert notation symbol here") @@ -234,7 +235,7 @@ export class SettingsTab extends PluginSettingTab { if (this.plugin.settings.enableMultipleTranslations) { new Setting(containerEl) .setName("Paths to translations with their names") - .setDesc("Input full paths from the root valut folder to folders containing Bible translations, each trnaslation on separate line. An example of one entry: \"Bible/NIV/\". The plugin will search for corresponding Bible files using given paths as starting points. Make sure there are no duplicate files in given paths, otherwise it is hard to tell what the output will be. The first translation will be considered your main translation.").addTextArea((inputBox) => + .setDesc("Input full paths from the root vault folder to folders containing Bible translations, each translation on separate line. An example of one entry: \"Bible/NIV/\". The plugin will search for corresponding Bible files using given paths as starting points. Make sure there are no duplicate files in given paths, otherwise it is hard to tell what the output will be. The first translation will be considered your main translation.").addTextArea((inputBox) => inputBox .setPlaceholder("Bible/NIV/\nBible/ESV/") .setValue(this.plugin.settings.translationsPaths) @@ -242,7 +243,7 @@ export class SettingsTab extends PluginSettingTab { const inputPaths = value.split(/\r?\n|\r/); // split user input by lines (regex takes into account all possible line endings) const paths: string[] = []; inputPaths.forEach((path) => { // parse user input for later use - if (path.at(-1) !== "/") { // Add potentionally missing '/' to path + if (path.at(-1) !== "/") { // Add potentially missing '/' to path paths.push(path + "/"); } else { @@ -343,6 +344,7 @@ export class SettingsTab extends PluginSettingTab { .addDropdown((dropdown) => { dropdown.addOption(LinkType.Basic, LinkType.Basic) dropdown.addOption(LinkType.Embedded, LinkType.Embedded) + dropdown.addOption(LinkType.FirstAndLast, "Show First & Last"); dropdown.addOption(LinkType.Invisible, LinkType.Invisible) dropdown.setValue(this.plugin.settings.linkTypePreset) dropdown.onChange(async (value) => {