diff --git a/.gitignore b/.gitignore index b813797..5c8ce15 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ .vscode # npm node_modules +package-lock.json # build main.js diff --git a/src/data/constants.ts b/src/data/constants.ts index 250f54d..af43bfb 100644 --- a/src/data/constants.ts +++ b/src/data/constants.ts @@ -24,6 +24,7 @@ export enum BibleVersionNameLengthEnum { export interface BibleReferencePluginSettings { bibleVersion: string + defaultBibleVersion: string referenceLinkPosition?: BibleVerseReferenceLinkPosition verseFormatting?: BibleVerseFormat verseNumberFormatting?: BibleVerseNumberFormat @@ -46,6 +47,7 @@ export interface BibleReferencePluginSettings { export const DEFAULT_SETTINGS: BibleReferencePluginSettings = { bibleVersion: DEFAULT_BIBLE_VERSION.key, + defaultBibleVersion: DEFAULT_BIBLE_VERSION.key, referenceLinkPosition: BibleVerseReferenceLinkPosition.Header, verseFormatting: BibleVerseFormat.SingleLine, verseNumberFormatting: BibleVerseNumberFormat.Period, diff --git a/src/suggesetor/VerseEditorSuggester.ts b/src/suggesetor/VerseEditorSuggester.ts index d6d8b20..2ceabec 100644 --- a/src/suggesetor/VerseEditorSuggester.ts +++ b/src/suggesetor/VerseEditorSuggester.ts @@ -12,6 +12,8 @@ import { VerseSuggesting } from '../verse/VerseSuggesting' import { BibleReferencePluginSettings } from '../data/constants' import { getSuggestionsFromQuery } from '../utils/getSuggestionsFromQuery' import { EventStats } from '../provider/EventStats' +import { versionMatch } from '../utils/versionMatch' +import { getBibleVersion } from '../data/BibleVersionCollection' /** * Extend the EditorSuggest to suggest bible verses. @@ -53,7 +55,23 @@ export class VerseEditorSuggester extends EditorSuggest { const queryContent = currentContent.substring(2) const match = verseMatch(queryContent) + if (match) { + + const vMatch = versionMatch(queryContent) + if (vMatch) { + this.plugin.settings.bibleVersion = vMatch + if (getBibleVersion(vMatch).key == vMatch) { + this.plugin.saveSettings() + } + } else { + if (this.settings.bibleVersion != this.settings.defaultBibleVersion) { + this.settings.bibleVersion = this.settings.defaultBibleVersion + console.log(`defaultBibleVersion : ${this.settings.defaultBibleVersion}`) + this.plugin.saveSettings() + } + } + console.debug('trigger on', queryContent) EventStats.logUIOpen( 'lookupEditorOpen', @@ -79,6 +97,7 @@ export class VerseEditorSuggester extends EditorSuggest { async getSuggestions( context: EditorSuggestContext ): Promise { + console.log(`context query : ${context.query}`) const suggestions = await getSuggestionsFromQuery( context.query, this.settings diff --git a/src/ui/BibleReferenceSettingTab.ts b/src/ui/BibleReferenceSettingTab.ts index f16ab19..5bf95b3 100644 --- a/src/ui/BibleReferenceSettingTab.ts +++ b/src/ui/BibleReferenceSettingTab.ts @@ -239,6 +239,7 @@ Obsidian Bible Reference is proudly powered by .setValue(this.plugin.settings.bibleVersion) .onChange(async (value) => { this.plugin.settings.bibleVersion = value + this.plugin.settings.defaultBibleVersion = value console.debug('Default Bible Version: ' + value) await this.plugin.saveSettings() pluginEvent.trigger('bible-reference:settings:version', [value]) diff --git a/src/utils/reg.test.ts b/src/utils/reg.test.ts index 55a8efb..1c7db2a 100644 --- a/src/utils/reg.test.ts +++ b/src/utils/reg.test.ts @@ -1,4 +1,4 @@ -import { BOOK_REG, MODAL_REG } from './regs' +import { BOOK_REG, MODAL_REG, VERSION_REG } from './regs' describe('test book name reg matching in different languages', () => { test('should match book name in English', () => { @@ -74,4 +74,23 @@ describe('test modal reg matching in different languages', () => { const reg = new RegExp(MODAL_REG) expect(reg.test(modal)).toBe(false) }) + + test('should match version with only alphabets', () => { + const modal = '-niv2011' + const reg = new RegExp(VERSION_REG) + expect(reg.test(modal)).toBe(true) + }) + + test('should match version with numbers', () => { + const modal = '-niv2011' + const reg = new RegExp(VERSION_REG) + expect(reg.test(modal)).toBe(true) + }) + + test('should not match when there is hyphen', () => { + const modal = '-should-fail' + const reg = new RegExp(MODAL_REG) + expect(reg.test(modal)).toBe(false) + }) + }) diff --git a/src/utils/regs.ts b/src/utils/regs.ts index b279f21..3ee42a5 100644 --- a/src/utils/regs.ts +++ b/src/utils/regs.ts @@ -11,6 +11,8 @@ export const MODAL_REG = /([123])*\s*([\p{L}[\\\]^_`a-zA-Z]{2,100}|\p{Script=Han export const BOOK_REG = /([123])*\s*([\p{L}[\\\]^_`a-zA-Z]{2,100}|\p{Script=Han}{1,})/isu +export const VERSION_REG = /(-[a-zA-Z0-9]+)$/isu + // export const BOOK_REG = /[123]*\s*[A-Z\[\\\]^_`a-z]{2,}/ /** diff --git a/src/utils/versionMatch.ts b/src/utils/versionMatch.ts new file mode 100644 index 0000000..c6e3ec3 --- /dev/null +++ b/src/utils/versionMatch.ts @@ -0,0 +1,16 @@ +import { VERSION_REG } from './regs' + +/** + * check if the given string contains a verseNumber, and return the verseNumber if it does + * @param verseTrigger without the prefix trigger -- + * @returns string the same string if it match + */ +export const versionMatch = (verseTrigger: string): string => { + const matchResults = verseTrigger.match(VERSION_REG) + console.log(`version reg match result : ${matchResults}`) + if (!matchResults) { + return '' + } else { + return matchResults[0].substring(1) + } +} \ No newline at end of file