|
|
|
|
@@ -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,17 +147,24 @@ export default class PassageSuggest extends EditorSuggest<PassageSuggestion> {
|
|
|
|
|
ref: PassageReference
|
|
|
|
|
): Promise<string[] | null> {
|
|
|
|
|
let basePath = '';
|
|
|
|
|
for (const alias of [ref.book.name, ...ref.book.aliases]) {
|
|
|
|
|
basePath = [this.settings.biblesPath, ref.version, alias].join('/');
|
|
|
|
|
basePath = normalizePath(basePath);
|
|
|
|
|
|
|
|
|
|
// if the book exists at this alias, use the alias instead
|
|
|
|
|
// and add the previous book name to the aliases
|
|
|
|
|
if (this.app.vault.getFolderByPath(basePath)) {
|
|
|
|
|
ref.book.aliases.push(ref.book.name);
|
|
|
|
|
ref.book.aliases.remove(alias);
|
|
|
|
|
ref.book.name = alias;
|
|
|
|
|
break;
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
// if the book exists at this alias, use the alias instead
|
|
|
|
|
// and add the previous book name to the aliases
|
|
|
|
|
if (this.app.vault.getFolderByPath(basePath)) {
|
|
|
|
|
ref.book.aliases.push(ref.book.name);
|
|
|
|
|
ref.book.aliases.remove(alias);
|
|
|
|
|
ref.book.name = alias;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -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, '');
|
|
|
|
|
|