Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support to multiples versicles #32

Merged
merged 7 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
17 changes: 6 additions & 11 deletions src/EditorSuggester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ export class EditorSuggester extends EditorSuggest<VerseLink> {
cursor: EditorPosition,
editor: Editor,
file: TFile | null
): EditorSuggestTriggerInfo | null {
): EditorSuggestTriggerInfo | null {
const currentLine = editor.getLine(cursor.line);

const link_pos = currentLine.search(
new RegExp(this.settings.linkTrigger, "u")
);
Expand Down Expand Up @@ -116,14 +115,12 @@ export function getSuggestionsFromQuery(
return [];
}

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

const verses = numbersPartsOfQueryString.split(':')[1].replaceAll(' ', '').trim() // get verses provide by user
const chapterNumber = parseInt(numbers[0]);
const verseNumber = numbers.length > 1 ? parseInt(numbers[1]) : undefined;
const verseEndNumber =
numbers.length === 3 ? parseInt(numbers[2]) : undefined;


return booksUrl.flatMap(
(bookUrl) =>
settings.bibleVersions
Expand All @@ -134,17 +131,15 @@ export function getSuggestionsFromQuery(
bookUrl,
bookName,
chapterNumber,
verseNumber,
verseEndNumber
verses
);
} else if (verseNumber !== undefined) {
return new VerseEmbed(
version,
bookUrl,
bookName,
chapterNumber,
verseNumber,
verseEndNumber
verses
);
}
})
Expand Down
20 changes: 16 additions & 4 deletions src/GenerateLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,37 @@ 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)];
const match = [...line.matchAll(linkRegex)];
match.forEach((match) => {
const suggestions = getSuggestionsFromQuery(
const suggestions = removeDuplicatedSuggestionsHandler(getSuggestionsFromQuery(
match[0],
true,
settings
);
));
suggestions.forEach(async (s) => {
if (match.index === undefined) return;
if (match.index === undefined) return;
editor.replaceRange(
await s.toReplace(),
{
Expand Down
3 changes: 1 addition & 2 deletions src/Regex.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export const linkRegex =
/([12345]\s?)?\p{L}+\s?\d{1,3}([:,.]\s?\d{1,3}([-–]\d{1,3})?)?/gu;
export const linkRegex = /([12345]?\p{L}{2,}\d?\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;

Expand Down
19 changes: 9 additions & 10 deletions src/Verse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ export default abstract class Verse {
private bookUrl: string,
private book: string,
private chapter: number,
private verse: number | undefined,
private verseEnd: number | undefined
private verses: string
) {}

public render(el: HTMLElement) {
Expand All @@ -24,21 +23,21 @@ 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}`
: `${this.book} ${this.chapter}`;
const formatBookNameHandler = (book: string) => {
return book.charAt(0).toUpperCase() + book.toLowerCase().slice(1);
}
return this.verses
? `${formatBookNameHandler(this.book)} ${this.chapter}:${this.verses.replaceAll(',', ', ')}`
: `${formatBookNameHandler(this.book)} ${this.chapter}`;
}

abstract toReplace(): Promise<string>;

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) {
url += `.${this.verses}`;
}
return url;
}
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"DOM",
"ES5",
"ES6",
"ES7"
"ES7",
"ES2021.String"
]
},
"include": [
Expand Down
Loading