Skip to content

Commit

Permalink
Merge pull request #242 from Benedictteo05/feat/specify-alternate-ver…
Browse files Browse the repository at this point in the history
…sion

Added ability to specify alternate version
  • Loading branch information
tim-hub authored Dec 5, 2024
2 parents cdb21cc + 430a6fb commit 5bfb00d
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
.vscode
# npm
node_modules
package-lock.json

# build
main.js
Expand Down
2 changes: 2 additions & 0 deletions src/data/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export enum BibleVersionNameLengthEnum {

export interface BibleReferencePluginSettings {
bibleVersion: string
defaultBibleVersion: string
referenceLinkPosition?: BibleVerseReferenceLinkPosition
verseFormatting?: BibleVerseFormat
verseNumberFormatting?: BibleVerseNumberFormat
Expand All @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions src/suggesetor/VerseEditorSuggester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -53,7 +55,23 @@ export class VerseEditorSuggester extends EditorSuggest<VerseSuggesting> {
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',
Expand All @@ -79,6 +97,7 @@ export class VerseEditorSuggester extends EditorSuggest<VerseSuggesting> {
async getSuggestions(
context: EditorSuggestContext
): Promise<VerseSuggesting[]> {
console.log(`context query : ${context.query}`)
const suggestions = await getSuggestionsFromQuery(
context.query,
this.settings
Expand Down
1 change: 1 addition & 0 deletions src/ui/BibleReferenceSettingTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
21 changes: 20 additions & 1 deletion src/utils/reg.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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)
})

})
2 changes: 2 additions & 0 deletions src/utils/regs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,}/

/**
Expand Down
16 changes: 16 additions & 0 deletions src/utils/versionMatch.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 5bfb00d

Please sign in to comment.