Skip to content

Commit

Permalink
Add "Show First & Last" option for the link command
Browse files Browse the repository at this point in the history
  • Loading branch information
kuchejak committed Feb 11, 2024
1 parent e8b6f28 commit 7dbf0e1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
50 changes: 38 additions & 12 deletions src/logic/link-command.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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";
Expand All @@ -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
*/
Expand Down
2 changes: 2 additions & 0 deletions src/modals/link-verse-modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export enum LinkType {
Basic = "Basic",
Embedded = "Embedded",
Invisible = "Invisible",
FirstAndLast = "FirstAndLast",
}

/**
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 5 additions & 3 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -234,15 +235,15 @@ 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)
.onChange(async (value) => {
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 {
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit 7dbf0e1

Please sign in to comment.