Skip to content

Commit

Permalink
Merge pull request #32 from JoaoEmanuell/master
Browse files Browse the repository at this point in the history
Added support to multiples versicles
  • Loading branch information
jaanonim authored Sep 19, 2024
2 parents 4695421 + 5f36f1b commit 1780eca
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 68 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

**List of supported languages can be found [here](./Languages.md).**

You need to just type for example: `@ John 1:1-6` to link verse to quote verse use for example: `> John 1:1-6`.
You need to just type for example: `@ John 1:1-6` to link verse to quote verse use for example: `> John 1:1-6`. Now it also supports multiple verses like: `John 3:16,18-20`.

`@` and `>` char is a trigger for suggestion and it can be changed in settings.
In settings you can select witch version of bible you want to use and books names in witch language will be detected.
Expand Down
29 changes: 29 additions & 0 deletions data/books/pt-br.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
{
"GEN": [
"gênesis",
"genesis",
"gn"
],
"EXO": [
"êxodo",
"exodo",
"ex"
],
"LEV": [
"levítico",
"levitico",
"lv"
],
"NUM": [
"números",
"numeros",
"nm"
],
"DEU": [
"deuteronômio",
"deuteronomio",
"dt"
],
"JOS": [
"josué",
"josue",
"js"
],
"JDG": [
"juízes",
"juizes",
"jz"
],
"RUT": [
Expand All @@ -49,10 +56,12 @@
],
"1CH": [
"1crônicas",
"1cronicas",
"1cr"
],
"2CH": [
"2crônicas",
"2cronicas",
"2cr"
],
"EZR": [
Expand All @@ -76,6 +85,7 @@
],
"PRO": [
"provérbios",
"proverbios",
"pv"
],
"ECC": [
Expand All @@ -84,10 +94,14 @@
],
"SNG": [
"cantaresdesalomão",
"cantares",
"cânticodoscânticos",
"canticodoscanticos",
"ct"
],
"ISA": [
"isaías",
"isaias",
"is"
],
"JER": [
Expand All @@ -96,6 +110,8 @@
],
"LAM": [
"lamentações",
"lamentaçoes",
"lamentacoes",
"lm"
],
"EZK": [
Expand All @@ -108,6 +124,7 @@
],
"HOS": [
"oséias",
"oseias",
"os"
],
"JOL": [
Expand All @@ -116,6 +133,7 @@
],
"AMO": [
"amós",
"amos",
"am"
],
"OBA": [
Expand All @@ -128,6 +146,7 @@
],
"MIC": [
"miquéias",
"miqueias",
"mq"
],
"NAM": [
Expand Down Expand Up @@ -168,6 +187,7 @@
],
"JHN": [
"joão",
"joao",
"jo"
],
"ACT": [
Expand All @@ -180,18 +200,22 @@
],
"1CO": [
"1coríntios",
"1corintios",
"1co"
],
"2CO": [
"2coríntios",
"2corintios",
"2co"
],
"GAL": [
"gálatas",
"galatas",
"gl"
],
"EPH": [
"efésios",
"efesios",
"ef"
],
"PHP": [
Expand All @@ -212,10 +236,12 @@
],
"1TI": [
"1timóteo",
"1timoteo",
"1tm"
],
"2TI": [
"2timóteo",
"2timoteo",
"2tm"
],
"TIT": [
Expand Down Expand Up @@ -244,14 +270,17 @@
],
"1JN": [
"1joão",
"1joao",
"1jo"
],
"2JN": [
"2joão",
"2joao",
"2jo"
],
"3JN": [
"3joão",
"3joao",
"3jo"
],
"JUD": [
Expand Down
42 changes: 28 additions & 14 deletions src/EditorSuggester.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import getBooks from "./Books";
import { bookRegex, linkRegex, separatorRegex } from "./Regex";
import {
bookRegex,
linkRegex,
chapterSeparatorRegex,
rangeSeparatorRegex,
} from "./Regex";
import { ObsidianYouversionLinkerSettings } from "./SettingsData";
import Verse from "./Verse";
import Verse, { VerseElement } from "./Verse";
import VerseEmbed from "./VerseEmbed";
import VerseLink from "./VerseLink";
import ObsidianYouversionLinker from "./main";
Expand All @@ -28,7 +33,6 @@ export class EditorSuggester extends EditorSuggest<VerseLink> {
file: TFile | null
): EditorSuggestTriggerInfo | null {
const currentLine = editor.getLine(cursor.line);

const link_pos = currentLine.search(
new RegExp(this.settings.linkTrigger, "u")
);
Expand Down Expand Up @@ -97,6 +101,19 @@ export class EditorSuggester extends EditorSuggest<VerseLink> {
}
}

export function processVerses(verses_str: Array<string>): Array<VerseElement> {
return verses_str
.map((verse) => {
const [start, end] = verse
.split(rangeSeparatorRegex)
.map((v) => (v === undefined ? undefined : parseInt(v)));
return start === undefined
? undefined
: new VerseElement(start, end);
})
.filter((v) => v !== undefined) as Array<VerseElement>;
}

export function getSuggestionsFromQuery(
query: string,
isLink: boolean,
Expand All @@ -117,12 +134,11 @@ export function getSuggestionsFromQuery(
}

const numbersPartsOfQueryString = query.substring(bookName.length);
const numbers = numbersPartsOfQueryString.split(separatorRegex);

const chapterNumber = parseInt(numbers[0]);
const verseNumber = numbers.length > 1 ? parseInt(numbers[1]) : undefined;
const verseEndNumber =
numbers.length === 3 ? parseInt(numbers[2]) : undefined;
const [chapter_str, ...verses_str] = numbersPartsOfQueryString.split(
chapterSeparatorRegex
);
const verses = processVerses(verses_str);
const chapterNumber = parseInt(chapter_str);

return booksUrl.flatMap(
(bookUrl) =>
Expand All @@ -134,17 +150,15 @@ export function getSuggestionsFromQuery(
bookUrl,
bookName,
chapterNumber,
verseNumber,
verseEndNumber
verses
);
} else if (verseNumber !== undefined) {
} else if (verses.length !== 0) {
return new VerseEmbed(
version,
bookUrl,
bookName,
chapterNumber,
verseNumber,
verseEndNumber
verses
);
}
})
Expand Down
18 changes: 14 additions & 4 deletions src/GenerateLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,32 @@ import { Editor, MarkdownView } from "obsidian";
import { linkRegex } from "./Regex";
import { getSuggestionsFromQuery } from "./EditorSuggester";
import { ObsidianYouversionLinkerSettings } from "./SettingsData";
import Verse from "./Verse";

export default function GenerateLinks(
editor: Editor,
view: MarkdownView,
settings: ObsidianYouversionLinkerSettings
) {
const removeDuplicatedSuggestionsHandler = (suggestions: Verse[]) => {
// remove duplicated suggestions
suggestions.map((suggestion) => {
const index = suggestions.indexOf(suggestion);
if (index >= 0) {
suggestions = suggestions.slice(index, 1);
}
});
return suggestions;
};

const lines = editor.lineCount();
for (let i = 0; i < lines; i++) {
const line = editor.getLine(i);

const match = [...line.matchAll(linkRegex)];
match.forEach((match) => {
const suggestions = getSuggestionsFromQuery(
match[0],
true,
settings
const suggestions = removeDuplicatedSuggestionsHandler(
getSuggestionsFromQuery(match[0], true, settings)
);
suggestions.forEach(async (s) => {
if (match.index === undefined) return;
Expand Down
7 changes: 4 additions & 3 deletions src/Regex.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
export const linkRegex =
/([12345]\s?)?\p{L}+\s?\d{1,3}([:,.]\s?\d{1,3}([-–]\d{1,3})?)?/gu;
export const linkRegex =
/([12345]\s?)?\p{L}+\s?\d{1,3}([:,.]\s?\d{1,3}([-–]\d{1,3})?)?([,.]\s?\d{1,3}([-–—]\d{1,3})?)*/gu;

export const bookRegex = /([12345]\s?)?\p{L}+/u;

export const separatorRegex = /[-:,.–]+/;
export const chapterSeparatorRegex = /[:,.]+/;
export const rangeSeparatorRegex = /[-–—]+/;

export const htmlDataRegex =
/<script\s*id="__NEXT_DATA__"\s*type="application\/json"\s*>.+?(?=<\/script>\s*<\/body>\s*<\/html>)/;
Expand Down
30 changes: 21 additions & 9 deletions src/Verse.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import { BibleVersion } from "./SettingsData";
import VERSIONS from "../data/versions.json";

export class VerseElement {
public start: number;
public end: number | undefined;

public constructor(start: number, end: number | undefined) {
this.start = start;
this.end = end;
}

public toString() {
return `${this.start}${this.end ? `-${this.end}` : ""}`;
}
}

export default abstract class Verse {
constructor(
private version: BibleVersion,
private bookUrl: string,
private book: string,
private chapter: number,
private verse: number | undefined,
private verseEnd: number | undefined
private verses: Array<VerseElement>
) {}

public render(el: HTMLElement) {
Expand All @@ -24,10 +37,10 @@ export default abstract class Verse {
}

toSimpleText() {
return this.verse
? this.verseEnd
? `${this.book} ${this.chapter}:${this.verse}-${this.verseEnd}`
: `${this.book} ${this.chapter}:${this.verse}`
return this.verses.length > 0
? `${this.book} ${this.chapter}:${this.verses
.map((verse) => verse.toString())
.join(", ")}`
: `${this.book} ${this.chapter}`;
}

Expand All @@ -36,9 +49,8 @@ export default abstract class Verse {
getUrl(): string {
const base = "https://www.bible.com/bible";
let url = `${base}/${this.version.id}/${this.bookUrl}.${this.chapter}`;
if (this.verse) {
url += `.${this.verse}`;
if (this.verseEnd) url += `-${this.verseEnd}`;
if (this.verses.length > 0) {
url += `.${this.verses.map((verse) => verse.toString()).join(",")}`;
}
return url;
}
Expand Down
Loading

0 comments on commit 1780eca

Please sign in to comment.