Adding bible block method
This commit is contained in:
@@ -158,6 +158,7 @@ export default class LocalBibleRefSettingTab extends PluginSettingTab {
|
||||
.addOptions({
|
||||
[BibleFormat.LocalBibleRef]: 'Local Bible Ref',
|
||||
[BibleFormat.BibleLinker]: 'Bible Linker',
|
||||
[BibleFormat.BlockRef]: 'Block Ref',
|
||||
})
|
||||
.setValue(this.plugin.settings.bibleFormat)
|
||||
.onChange(async (value) => {
|
||||
@@ -329,4 +330,5 @@ export default class LocalBibleRefSettingTab extends PluginSettingTab {
|
||||
export enum BibleFormat {
|
||||
LocalBibleRef = 'localBibleRef',
|
||||
BibleLinker = 'bibleLinker',
|
||||
BlockRef = 'blockRef',
|
||||
}
|
||||
|
||||
@@ -68,13 +68,15 @@ export default class PassageReference
|
||||
|
||||
/** Stringifies the passage reference back into text. */
|
||||
stringify(): string {
|
||||
const versionSuffix = this.version ? ` - ${this.version}` : '';
|
||||
|
||||
// multi-chapter ref
|
||||
if (this.startVerse === 1 && this.endVerse === -1) {
|
||||
if (this.startChapter === this.endChapter)
|
||||
return this.book.name + ` ${this.startChapter} - ${this.version}`;
|
||||
return `${this.book.name} ${this.startChapter}${versionSuffix}`;
|
||||
return (
|
||||
`${this.book.name} ${this.startChapter}-` +
|
||||
`${this.endChapter} - ${this.version}`
|
||||
`${this.endChapter}${versionSuffix}`
|
||||
);
|
||||
}
|
||||
|
||||
@@ -82,19 +84,19 @@ export default class PassageReference
|
||||
if (this.startChapter === this.endChapter) {
|
||||
if (this.startVerse === this.endVerse)
|
||||
return (
|
||||
this.book.name +
|
||||
` ${this.startChapter}:${this.startVerse} - ${this.version}`
|
||||
`${this.book.name} ${this.startChapter}:` +
|
||||
`${this.startVerse}${versionSuffix}`
|
||||
);
|
||||
return (
|
||||
`${this.book.name} ${this.startChapter}:` +
|
||||
`${this.startVerse}-${this.endVerse} - ${this.version}`
|
||||
`${this.startVerse}-${this.endVerse}${versionSuffix}`
|
||||
);
|
||||
}
|
||||
|
||||
// multi-chapter-and-verse ref
|
||||
const a = `${this.startChapter}:${this.startVerse}`;
|
||||
const b = `${this.endChapter}:${this.endVerse}`;
|
||||
return `${this.book.name} ${a}-${b} - ${this.version}`;
|
||||
return `${this.book.name} ${a}-${b}${versionSuffix}`;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -84,7 +84,7 @@ export default class PassageSuggest extends EditorSuggest<PassageSuggestion> {
|
||||
context: EditorSuggestContext
|
||||
): Promise<PassageSuggestion[]> {
|
||||
let version = this.settings.defaultVersionShorthand;
|
||||
if (!version) {
|
||||
if (!version && this.settings.bibleFormat !== BibleFormat.BlockRef) {
|
||||
const folder = this.app.vault.getFolderByPath(this.settings.biblesPath);
|
||||
version =
|
||||
folder?.children?.filter((c) => c instanceof TFolder)?.first()?.name ??
|
||||
@@ -147,6 +147,12 @@ export default class PassageSuggest extends EditorSuggest<PassageSuggestion> {
|
||||
ref: PassageReference
|
||||
): Promise<string[] | null> {
|
||||
let basePath = '';
|
||||
|
||||
if (this.settings.bibleFormat === BibleFormat.BlockRef) {
|
||||
const folderPath = this.findBlockRefBookFolder(ref);
|
||||
if (!folderPath) return null;
|
||||
basePath = folderPath;
|
||||
} else {
|
||||
for (const alias of [ref.book.name, ...ref.book.aliases]) {
|
||||
basePath = [this.settings.biblesPath, ref.version, alias].join('/');
|
||||
basePath = normalizePath(basePath);
|
||||
@@ -160,6 +166,7 @@ export default class PassageSuggest extends EditorSuggest<PassageSuggestion> {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const texts: string[] = [];
|
||||
|
||||
@@ -175,14 +182,55 @@ export default class PassageSuggest extends EditorSuggest<PassageSuggestion> {
|
||||
return texts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Locates the book folder under `<biblesPath>/<N> - <Testament>/<N> - <Book>`.
|
||||
* Updates `ref.book.name` to match the folder's book name on success.
|
||||
*/
|
||||
private findBlockRefBookFolder(ref: PassageReference): string | null {
|
||||
const root = this.app.vault.getFolderByPath(this.settings.biblesPath);
|
||||
if (!root) return null;
|
||||
|
||||
const candidates = [ref.book.name, ...ref.book.aliases].map((s) =>
|
||||
s.toLowerCase()
|
||||
);
|
||||
|
||||
for (const testament of root.children) {
|
||||
if (!(testament instanceof TFolder)) continue;
|
||||
|
||||
for (const bookFolder of testament.children) {
|
||||
if (!(bookFolder instanceof TFolder)) continue;
|
||||
|
||||
const match = bookFolder.name.match(/^\d+ - (.+)$/);
|
||||
if (!match) continue;
|
||||
|
||||
const folderBookName = match[1];
|
||||
if (!candidates.includes(folderBookName.toLowerCase())) continue;
|
||||
|
||||
if (folderBookName !== ref.book.name) {
|
||||
ref.book.aliases.push(ref.book.name);
|
||||
ref.book.aliases.remove(folderBookName);
|
||||
ref.book.name = folderBookName;
|
||||
}
|
||||
|
||||
return bookFolder.path;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Extracts the text in the chapter from the start verse to the end. */
|
||||
private getTextFromStartVerse(
|
||||
text: string,
|
||||
ref: PassageReference
|
||||
): string | null {
|
||||
let pattern = '';
|
||||
let flags = '';
|
||||
if (this.settings.bibleFormat === BibleFormat.BibleLinker) {
|
||||
pattern = `#{1,6} [a-zA-Z]*${ref.startVerse}[a-zA-Z]*\\n\\w+`;
|
||||
} else if (this.settings.bibleFormat === BibleFormat.BlockRef) {
|
||||
pattern = `^${ref.startVerse} `;
|
||||
flags = 'm';
|
||||
} else {
|
||||
const quoteOrList = '(?:[>-] )*';
|
||||
const chapterNum = '(?:\\*\\*\\d{1,3}\\*\\* )?';
|
||||
@@ -190,7 +238,7 @@ export default class PassageSuggest extends EditorSuggest<PassageSuggestion> {
|
||||
pattern = quoteOrList + chapterNum + verseNum;
|
||||
}
|
||||
|
||||
const regExp = new RegExp(pattern);
|
||||
const regExp = new RegExp(pattern, flags);
|
||||
const match = text.match(regExp);
|
||||
if (!match) return null;
|
||||
|
||||
@@ -207,15 +255,19 @@ export default class PassageSuggest extends EditorSuggest<PassageSuggestion> {
|
||||
if (ref.endVerse === -1) return text;
|
||||
|
||||
let pattern = '';
|
||||
let flags = '';
|
||||
if (this.settings.bibleFormat === BibleFormat.BibleLinker) {
|
||||
pattern = `#{1,6} [a-zA-Z]*${ref.endVerse + 1}[a-zA-Z]*\\n\\w+`;
|
||||
} else if (this.settings.bibleFormat === BibleFormat.BlockRef) {
|
||||
pattern = `^${ref.endVerse + 1} `;
|
||||
flags = 'm';
|
||||
} else {
|
||||
const quoteOrList = '(?:[>-] )*';
|
||||
const verseNum = `<sup>${ref.endVerse + 1}</sup>`;
|
||||
pattern = quoteOrList + verseNum;
|
||||
}
|
||||
|
||||
const regex = new RegExp(pattern);
|
||||
const regex = new RegExp(pattern, flags);
|
||||
return text.split(regex, 1)[0].trim();
|
||||
}
|
||||
|
||||
@@ -226,6 +278,8 @@ export default class PassageSuggest extends EditorSuggest<PassageSuggestion> {
|
||||
): string {
|
||||
if (this.settings.bibleFormat === BibleFormat.BibleLinker) {
|
||||
text = this.formatBibleLinkerVerses(text);
|
||||
} else if (this.settings.bibleFormat === BibleFormat.BlockRef) {
|
||||
text = this.formatBlockRefVerses(text);
|
||||
}
|
||||
|
||||
text = this.removeChapterNumbers(text);
|
||||
@@ -234,7 +288,10 @@ export default class PassageSuggest extends EditorSuggest<PassageSuggestion> {
|
||||
text = this.removeBOF(text);
|
||||
text = this.removeEOF(text);
|
||||
|
||||
if (this.settings.bibleFormat === BibleFormat.BibleLinker) {
|
||||
if (
|
||||
this.settings.bibleFormat === BibleFormat.BibleLinker ||
|
||||
this.settings.bibleFormat === BibleFormat.BlockRef
|
||||
) {
|
||||
text = this.removeVerseSpacing(text);
|
||||
}
|
||||
|
||||
@@ -260,6 +317,15 @@ export default class PassageSuggest extends EditorSuggest<PassageSuggestion> {
|
||||
);
|
||||
}
|
||||
|
||||
/** Formats verses that use Block Ref formatting. */
|
||||
private formatBlockRefVerses(text: string): string {
|
||||
// strip trailing block IDs (e.g. ` ^1` at end of line)
|
||||
text = text.replace(/ \^\d+(?=\s|$)/gm, '');
|
||||
// convert leading "N " (at line start) to "<sup>N</sup> "
|
||||
text = text.replace(/^(\d{1,3}) /gm, '<sup>$1</sup> ');
|
||||
return text;
|
||||
}
|
||||
|
||||
/** Removes chapter numbers from the given text. */
|
||||
private removeChapterNumbers(text: string): string {
|
||||
return text.replace(/\*\*\d{1,3}\*\* /g, '');
|
||||
|
||||
Reference in New Issue
Block a user