scripture-guide 1.0.83 → 1.0.84

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -658,9 +658,621 @@ const raw_lang = {
658
658
  }
659
659
  }
660
660
  ;
661
- import { processReferenceDetection } from './scriptdetect.mjs';
662
- import { detectReferencesWithContext } from './scriptdetectcontext.mjs';
661
+ // Shared utility functions
662
+ /**
663
+ * Shared utilities for scripture reference matching and processing
664
+ * Contains common logic used across detection modules
665
+ */
666
+
667
+ /**
668
+ * Check if two ranges overlap
669
+ * @param {Array} range1 - [start, end] tuple
670
+ * @param {Array} range2 - [start, end] tuple
671
+ * @returns {boolean} True if ranges overlap
672
+ */
673
+ const rangesOverlap = (range1, range2) => {
674
+ return range1[0] < range2[1] && range2[0] < range1[1];
675
+ };
676
+
677
+ /**
678
+ * Remove overlapping matches, with configurable preference logic
679
+ * @param {Array} matches - Array of match tuples
680
+ * @param {string} content - Original content (for debugging)
681
+ * @param {Function} preferenceResolver - Function to choose between overlapping matches
682
+ * @returns {Array} Non-overlapping matches
683
+ */
684
+ const removeOverlaps = (matches, content, preferenceResolver = null) => {
685
+ if (matches.length <= 1) return matches;
686
+
687
+ const result = [];
688
+
689
+ for (let i = 0; i < matches.length; i++) {
690
+ const current = matches[i];
691
+ let shouldInclude = true;
692
+
693
+ // Check for overlaps with already included matches
694
+ for (const included of result) {
695
+ if (rangesOverlap(current, included)) {
696
+ shouldInclude = false;
697
+ break;
698
+ }
699
+ }
700
+
701
+ if (shouldInclude) {
702
+ // Check for overlaps with remaining matches
703
+ for (let j = i + 1; j < matches.length; j++) {
704
+ const next = matches[j];
705
+ if (rangesOverlap(current, next)) {
706
+ // Use preference resolver or default to longer match
707
+ if (preferenceResolver) {
708
+ const preferred = preferenceResolver(current, next, content);
709
+ if (preferred !== current) {
710
+ shouldInclude = false;
711
+ break;
712
+ }
713
+ } else {
714
+ // Default: prefer longer match
715
+ const currentLength = current[1] - current[0];
716
+ const nextLength = next[1] - next[0];
717
+
718
+ if (nextLength > currentLength) {
719
+ shouldInclude = false;
720
+ break;
721
+ }
722
+ }
723
+ }
724
+ }
725
+ }
726
+
727
+ if (shouldInclude) {
728
+ result.push(current);
729
+ }
730
+ }
731
+
732
+ return result;
733
+ };
734
+
735
+ /**
736
+ * Calculate gaps between consecutive matches
737
+ * @param {Array} matches - Array of [start, end] tuples
738
+ * @returns {Array} Array of gap [start, end] tuples
739
+ */
740
+ const calculateGaps = (matches) => {
741
+ return matches.reduce((prev, current, index) => {
742
+ if (index === 0) return prev;
743
+ const lastPair = matches[index - 1];
744
+ const gap = [lastPair[1], current[0]];
745
+ return [...prev, gap];
746
+ }, []);
747
+ };
748
+
749
+ /**
750
+ * Calculate negative space (gaps) for final output construction
751
+ * @param {Array} matches - Array of [start, end] tuples
752
+ * @param {number} contentLength - Total length of content
753
+ * @returns {Array} Array of negative space [start, end] tuples
754
+ */
755
+ const calculateNegativeSpace = (matches, contentLength) => {
756
+ const negativeSpace = matches.reduce((prev, current, index, array) => {
757
+ if (index !== 0) {
758
+ const prevIndex = array[index - 1];
759
+ const gap = [prevIndex[1], current[0]];
760
+ prev.push(gap);
761
+ }
762
+ return prev;
763
+ }, []);
764
+
765
+ if (matches[0][0] !== 0) {
766
+ negativeSpace.unshift([0, matches[0][0]]);
767
+ }
768
+
769
+ if (matches[matches.length - 1][1] !== contentLength) {
770
+ negativeSpace.push([matches[matches.length - 1][1], contentLength]);
771
+ }
772
+
773
+ return negativeSpace;
774
+ };
775
+
776
+ /**
777
+ * Merge adjacent matches based on joiner patterns
778
+ * @param {Array} matches - Array of match tuples
779
+ * @param {Array} gapMergeFlags - Boolean array indicating which gaps can be merged
780
+ * @param {Function} compatibilityCheck - Optional function to check if matches can be merged
781
+ * @returns {Array} Merged matches
782
+ */
783
+ const mergeAdjacentMatches = (matches, gapMergeFlags, compatibilityCheck = null) => {
784
+ return matches.reduce((prev, current, index) => {
785
+ if (index === 0) {
786
+ return [current];
787
+ } else {
788
+ const prevIndex = prev[prev.length - 1];
789
+ const shouldMerge = gapMergeFlags[index - 1];
790
+
791
+ // Check compatibility if function provided
792
+ if (shouldMerge && compatibilityCheck) {
793
+ const isCompatible = compatibilityCheck(prevIndex, current);
794
+ if (!isCompatible) {
795
+ prev.push(current);
796
+ return prev;
797
+ }
798
+ }
799
+
800
+ if (shouldMerge) {
801
+ // Merge logic - keep first position, extend to second end, combine text and verse IDs
802
+ if (prevIndex.length > 2 && current.length > 2) {
803
+ // Extended format: [start, end, text, verseIds]
804
+ const startPos = prevIndex[0];
805
+ const endPos = current[1];
806
+ const combinedText = prevIndex[2] + ";" + current[2]; // Combine with semicolon
807
+ const combinedVerseIds = [...(prevIndex[3] || []), ...(current[3] || [])]; // Merge verse ID arrays
808
+ const merged = [startPos, endPos, combinedText, combinedVerseIds];
809
+ prev[prev.length - 1] = merged;
810
+ } else {
811
+ // Basic format: [start, end]
812
+ const merged = [prevIndex[0], current[1], ...(prevIndex.slice(2))];
813
+ prev[prev.length - 1] = merged;
814
+ }
815
+ } else {
816
+ prev.push(current);
817
+ }
818
+ return prev;
819
+ }
820
+ }, []);
821
+ };
822
+
823
+ /**
824
+ * Build final output by alternating between cut items and negative space
825
+ * @param {Array} cutItems - Processed match content
826
+ * @param {Array} negativeItems - Unprocessed content between matches
827
+ * @param {boolean} firstReferenceIsAtStart - Whether first match starts at position 0
828
+ * @returns {string} Final merged output
829
+ */
830
+ const buildFinalOutput = (cutItems, negativeItems, firstReferenceIsAtStart) => {
831
+ const maxCount = Math.max(cutItems.length, negativeItems.length);
832
+ const merged = [];
833
+
834
+ for (let i = 0; i < maxCount; i++) {
835
+ const firstItem = firstReferenceIsAtStart ? cutItems[i] : negativeItems[i];
836
+ const secondItem = firstReferenceIsAtStart ? negativeItems[i] : cutItems[i];
837
+ if (firstItem) merged.push(firstItem);
838
+ if (secondItem) merged.push(secondItem);
839
+ }
840
+
841
+ return merged.join("");
842
+ };
843
+
844
+ /**
845
+ * Default tie-breaker for scriptdetect.mjs overlap resolution
846
+ * @param {Array} pair1 - First match tuple
847
+ * @param {Array} pair2 - Second match tuple
848
+ * @param {string} content - Original content
849
+ * @returns {Array} Preferred match tuple
850
+ */
851
+ const defaultTieBreaker = (pair1, pair2, content) => {
852
+ const string1 = content.substring(pair1[0], pair1[1]);
853
+ const string2 = content.substring(pair2[0], pair2[1]);
854
+
855
+ // If one pair is all lower case, return the other one
856
+ if(/[^A-Z]/.test(string1) && !/[^A-Z]/.test(string2)) return pair2;
857
+ if(/[^A-Z]/.test(string2) && !/[^A-Z]/.test(string1)) return pair1;
858
+
859
+ if(string1.length > string2.length) return pair1;
860
+ if(string2.length > string1.length) return pair2;
861
+
862
+ return pair1;
863
+ };
864
+
865
+ // Script detection functions
866
+ const findMatchingBooks = (content,books) => {
867
+ const matchingBooks = books.filter(i=>(new RegExp(i,"ig")).test(content));
868
+ return matchingBooks;
869
+ }
870
+
871
+ const findMatches = (content,books,lang_extra) => {
872
+ // Pre-process content to normalize HTML entities to Unicode dashes
873
+ const htmlEntityMap = {
874
+ '&ndash;': '–',
875
+ '&mdash;': '—',
876
+ '&minus;': '−',
877
+ '&#8211;': '–',
878
+ '&#8212;': '—',
879
+ '&#8722;': '−'
880
+ };
881
+
882
+ let normalizedContent = content;
883
+ Object.entries(htmlEntityMap).forEach(([entity, unicode]) => {
884
+ normalizedContent = normalizedContent.replace(new RegExp(entity, 'g'), unicode);
885
+ });
886
+
887
+ const tail = lang_extra.tail ? new RegExp(lang_extra.tail,"ig") : /[^0-9]+$/;
888
+ const preBookMatch = lang_extra.book || `(First|I|1|1st|Second|II|2|2nd|Third|III|3|3rd|Fourth|IV|4|4th)*\\s*(books* of)*\\s*`;
889
+ const matchingBooks = findMatchingBooks(normalizedContent,books);
890
+ const postBookMatch = lang_extra.chapter || "([0-9:.;,~ —–-]|cf)*[0-9]+";
891
+ const fullBookMatches = matchingBooks.map(bookMatch=>{
892
+ const patternString = preBookMatch + bookMatch ;
893
+ const pattern = (new RegExp(patternString,"ig"));
894
+ const stringMatch = pattern.test(normalizedContent) ? patternString : null;
895
+ //console.log({pattern,content,stringMatch});
896
+ return stringMatch;
897
+ }).filter(x=>!!x);
898
+
899
+ const bookSubStrings = fullBookMatches.map(bookMatch=>{
900
+ const substrings = normalizedContent.match(new RegExp(bookMatch,"ig")).flat();
901
+ return substrings;
902
+ }).flat().reduce((prev,current)=>{
903
+ if(prev.includes(current)) return prev;
904
+ return [...prev,current]
905
+ },[]).filter(i=>!!i).map(substring=>{
906
+ substring = substring.trim();
907
+ let positions = [];
908
+ let index = normalizedContent.indexOf(substring);
909
+ while (index != -1) {
910
+ positions.push(index);
911
+ index = normalizedContent.indexOf(substring, index + 1);
912
+ } return [substring,positions];
913
+ }).map(([substring,positions])=>{
914
+ const preChars = positions.map(i=>normalizedContent.substring(i-1,i));
915
+ positions = positions.filter((i,index)=>{
916
+ const charRightBeforeMatch = preChars[index];
917
+ const leadingCharIsInvalid = !/^(\s|\W|)$/.test(charRightBeforeMatch);
918
+ return !leadingCharIsInvalid;
919
+ });
920
+ if(!positions.length) return false;
921
+ return [substring,positions];
922
+ }).filter(i=>!!i);
923
+
924
+ const possiblyOverlappingMatches = bookSubStrings.map(([substring,positions])=>{
925
+ return positions.map(i=>{
926
+ let pattern = null;
927
+ try{
928
+ pattern = new RegExp(substring+postBookMatch,"ig");
929
+ }catch(e){
930
+ return null;
931
+ }
932
+ const match = normalizedContent.slice(i).match(pattern)?.[0]?.replace(tail,"").trim();
933
+ if(!match) return null;
934
+ const len = match.length;
935
+ const pos = i;
936
+ const [posIn, posOut] = [pos,pos+len];
937
+ //console.log({content:content.slice(i),pattern,match,posIn,posOut});
938
+ return [match,posIn,posOut];
939
+ }
940
+ )}).flat().filter(i=>!!i);
941
+ const matchesWithReferences = possiblyOverlappingMatches.map(([string,start,end])=>{
942
+ const overLappingItems = possiblyOverlappingMatches.filter(([s,s1,e1])=>s!==string && s1<end && e1>end);
943
+ const hasOverlap = overLappingItems.length > 0;
944
+ const newEnd = hasOverlap ? Math.min(...overLappingItems.map(([s,s1,e1])=>s1)) : end;
945
+ const newString = normalizedContent.slice(start,newEnd);
946
+ return newString.replace(tail,"").trim();
947
+ }).filter(i=>!!i);
948
+ const matches = matchesWithReferences.map(string=>{
949
+ const pattern = (new RegExp(string,"ig"));
950
+ const matches = normalizedContent.match(pattern)?.map(i=>i.trim().replace(tail,""));
951
+ //console.log({string,matches,tail});
952
+ return matches;
953
+ }).flat()
954
+ .reduce((prev,current)=>{
955
+ if(prev.includes(current)) return prev;
956
+ return [...prev,current]
957
+ },[]).filter(i=>!!i);
958
+ return matches;
959
+ }
960
+
961
+
962
+
963
+ function findMatchIndexes(content, matches,lookupReference, lang_extra) {
964
+ const tail = lang_extra.tail ? new RegExp(lang_extra.tail,"ig") : /[^0-9]+$/;
965
+ const indexes = matches
966
+ .sort((b, a) => b.length - a.length)
967
+ .map(i=>{
968
+ const length = i.length;
969
+ let positions = [];
970
+ let strPos = content.indexOf(i);
971
+
972
+ while (strPos != -1) {
973
+ positions.push(strPos);
974
+ strPos = content.indexOf(i, strPos + 1);
975
+ }
976
+ return positions.map(i=>[i,i+length+2]);
977
+ }).flat()
978
+ .map(a=>{
979
+
980
+ const substring = content.substring(a[0],a[1]).trim().replace(tail,"").trim();
981
+ if(!substring) return false;
982
+ const verse_ids = lookupReference(substring).verse_ids;
983
+ //console.log({substring,verse_ids});
984
+ if(verse_ids.length > 0) return [a[0],a[0]+substring.length];
985
+
986
+ return false;
987
+ })
988
+ .filter(i=>!!i)
989
+ .sort((a, b) => a[0] - b[0])
990
+ .map(([start, end], index, array) => {
991
+ const nextStart = array[index+1]?.[0] || null;
992
+ end = nextStart ? Math.min(end,nextStart) : end;
993
+ return [start,end];
994
+ });
995
+
996
+ if(!indexes.length) return false;
997
+
998
+ // Use shared overlap removal with custom tie-breaker
999
+ const preferenceResolver = (pair1, pair2, content) => defaultTieBreaker(pair1, pair2, content);
1000
+ const nonOverlappingIndeces = removeOverlaps(indexes, content, preferenceResolver)
1001
+ .filter(([start,end])=>start!==end);
1002
+
1003
+ return nonOverlappingIndeces;
1004
+
1005
+
1006
+
1007
+ }
663
1008
 
1009
+
1010
+
1011
+
1012
+
1013
+ const processReferenceDetection = (content,books,lang_extra,lookupReference,callback) =>
1014
+ {
1015
+ try{
1016
+ lang_extra = lang_extra || {};
1017
+ const matches = findMatches(content,books,lang_extra);
1018
+ const matchIndeces = findMatchIndexes(content,matches,lookupReference,lang_extra);
1019
+ if(!matchIndeces) return content;
1020
+
1021
+ const gapsBetweenIndeces = calculateGaps(matchIndeces);
1022
+ const gapStrings = gapsBetweenIndeces.map(([start,end])=>content.substring(start,end).trim());
1023
+ //console.log({matches,matchIndeces,gapsBetweenIndeces,gapStrings});
1024
+ const joiners = lang_extra.joiners || ["^[;, ]*(and|c\\.*f\\.*)*$"];
1025
+ const gapThatMayBeMerged = gapsBetweenIndeces.map(([start,end],i)=>{
1026
+ const gapString = gapStrings[i];
1027
+ const canBeMerged = joiners.some(joiner=>(new RegExp(joiner,"ig")).test(gapString));
1028
+ const matchingJoiner = joiners.find(joiner=>(new RegExp(joiner,"ig")).test(gapString));
1029
+ //console.log({gapString,matchingJoiner});
1030
+ return canBeMerged;
1031
+ });
1032
+
1033
+ // Use shared merge logic
1034
+ const mergedIndeces = mergeAdjacentMatches(matchIndeces, gapThatMayBeMerged);
1035
+
1036
+ // Use shared negative space calculation
1037
+ const negativeSpace = calculateNegativeSpace(mergedIndeces, content.length);
1038
+
1039
+ //check content between matches. If puctuation only (or 'and'), then merge
1040
+ const cutItems = mergedIndeces.map(([start,end])=>{
1041
+ const string = content.substring(start,end);
1042
+ const lookupResult = lookupReference(string);
1043
+ return callback(lookupResult.query, lookupResult.verse_ids);
1044
+ });
1045
+
1046
+ const negativeItems = negativeSpace.map(([start,end])=>content.substring(start,end));
1047
+ const firstReferenceIsAtStart = mergedIndeces[0][0] === 0;
1048
+
1049
+ return buildFinalOutput(cutItems, negativeItems, firstReferenceIsAtStart);
1050
+ }catch(e){
1051
+ return content;
1052
+ }
1053
+ }
1054
+ // Context detection functions
1055
+ /**
1056
+ * Enhanced contextual scripture reference detection module
1057
+ * Handles detection of implied references using context from explicit references
1058
+ */
1059
+ // Enhanced reference detection with contextual processing
1060
+ const detectReferencesWithContext = (content, books, lang_extra, lookupReference, callback, options, generateReference = null) => {
1061
+ try {
1062
+ // Step 1: Find explicit references using existing logic
1063
+ const explicitMatches = findMatches(content, books, lang_extra);
1064
+ const explicitIndices = findMatchIndexes(content, explicitMatches, lookupReference, lang_extra);
1065
+
1066
+ // Step 2: Find implied references using context
1067
+ const impliedMatches = findImpliedReferences(content, explicitMatches, explicitIndices, lookupReference, options);
1068
+
1069
+ // Step 3: Merge and sort all matches - store both original text and verse IDs
1070
+ const explicitMatchesWithRefs = (explicitIndices || []).map(([start, end]) => {
1071
+ const originalText = content.substring(start, end);
1072
+ const verification = lookupReference(originalText);
1073
+ return [start, end, originalText, verification.verse_ids];
1074
+ });
1075
+
1076
+ // Convert implied matches to include verse IDs
1077
+ const impliedMatchesWithVerseIds = impliedMatches.map(([start, end, resolvedRef]) => {
1078
+ const originalText = content.substring(start, end);
1079
+ const verification = lookupReference(resolvedRef);
1080
+ return [start, end, originalText, verification.verse_ids];
1081
+ });
1082
+
1083
+ const allMatches = [...explicitMatchesWithRefs, ...impliedMatchesWithVerseIds];
1084
+ allMatches.sort((a, b) => a[0] - b[0]);
1085
+
1086
+ // Step 4: Remove overlaps, preferring explicit matches
1087
+ const nonOverlappingMatches = removeOverlaps(allMatches, content);
1088
+
1089
+ if (!nonOverlappingMatches || nonOverlappingMatches.length === 0) {
1090
+ return content;
1091
+ }
1092
+
1093
+ // Step 5: Apply the same gap merging logic as original
1094
+ const gapsBetweenIndices = calculateGaps(nonOverlappingMatches);
1095
+
1096
+ const joiners = lang_extra.joiners || ["^[;]*(and|cf\\.)*$", "^\\s*[;,]\\s*$"]; // More restrictive joiners
1097
+ const gapThatMayBeMerged = gapsBetweenIndices.map(([start, end]) => {
1098
+ const gapString = content.substring(start, end).trim();
1099
+ return joiners.some(joiner => (new RegExp(joiner, "ig")).test(gapString));
1100
+ });
1101
+
1102
+ // Compatibility check for merging references
1103
+ const compatibilityCheck = (prevIndex, current) => {
1104
+ // Always allow merging for explicit references separated by joiners
1105
+ // The user's intention is clear when they list references like "Isa 45.22;Morm 3.18;Moro 10.24"
1106
+ return true;
1107
+ };
1108
+
1109
+ // Merge adjacent matches if gaps are joiners and references are compatible
1110
+ const mergedIndices = mergeAdjacentMatches(nonOverlappingMatches, gapThatMayBeMerged, compatibilityCheck);
1111
+
1112
+ // Step 6: Build final output
1113
+ const negativeSpace = calculateNegativeSpace(mergedIndices, content.length);
1114
+
1115
+ // Use the stored reference and verse IDs
1116
+ const cutItems = mergedIndices.map(([start, end, originalText, verseIds]) => {
1117
+ // Pass verseIds to callback for compatibility with expected signature
1118
+ return callback(originalText, verseIds);
1119
+ });
1120
+
1121
+ const negativeItems = negativeSpace.map(([start, end]) => content.substring(start, end));
1122
+ const firstReferenceIsAtStart = mergedIndices[0][0] === 0;
1123
+
1124
+ const result = buildFinalOutput(cutItems, negativeItems, firstReferenceIsAtStart);
1125
+ return result;
1126
+ } catch (e) {
1127
+ console.warn('Enhanced reference detection failed, falling back to basic detection:', e);
1128
+ return processReferenceDetection(content, books, lang_extra, lookupReference, callback);
1129
+ }
1130
+ };
1131
+
1132
+ /**
1133
+ * Find all book contexts from explicit references
1134
+ * @param {string} content - The text content
1135
+ * @param {Array} explicitMatches - Array of explicit reference strings
1136
+ * @param {Array} explicitIndices - Array of [start, end] positions for explicit matches
1137
+ * @param {Function} lookupReference - Function to validate references
1138
+ * @returns {Array} Array of context objects with book, chapter, and position info
1139
+ */
1140
+ const findBookContexts = (content, explicitMatches, explicitIndices, lookupReference) => {
1141
+ const contexts = [];
1142
+
1143
+ // Use the already found explicit matches and their positions
1144
+ if (explicitMatches && explicitIndices) {
1145
+ for (let i = 0; i < explicitMatches.length; i++) {
1146
+ const match = explicitMatches[i];
1147
+ const [start, end] = explicitIndices[i];
1148
+
1149
+ const verification = lookupReference(match);
1150
+
1151
+ if (verification.verse_ids && verification.verse_ids.length > 0) {
1152
+ // Extract book and chapter from the verified reference
1153
+ // Handle both "Book Chapter:Verse" and "Book Chapter" (like D&C 76) patterns
1154
+ let refParts = verification.ref.match(/^(.+?)\s+(\d+):/);
1155
+ if (!refParts) {
1156
+ // Try pattern for section-only references like "Doctrine and Covenants 76"
1157
+ refParts = verification.ref.match(/^(.+?)\s+(\d+)$/);
1158
+ }
1159
+
1160
+ if (refParts) {
1161
+ const cleanBook = refParts[1];
1162
+ const chapter = parseInt(refParts[2]);
1163
+
1164
+ contexts.push({
1165
+ position: start,
1166
+ book: cleanBook,
1167
+ chapter: chapter,
1168
+ endPosition: end
1169
+ });
1170
+ }
1171
+ }
1172
+ }
1173
+ }
1174
+
1175
+ const result = contexts.sort((a, b) => a.position - b.position);
1176
+ return result;
1177
+ };
1178
+
1179
+ /**
1180
+ * Get the nearest book context for a given position
1181
+ * @param {number} position - Character position in the text
1182
+ * @param {Array} bookContexts - Array of context objects from findBookContexts
1183
+ * @param {number} maxDistance - Maximum distance to look for context
1184
+ * @returns {Object|null} The nearest context object or null if none found
1185
+ */
1186
+ const getNearestBookContext = (position, bookContexts, maxDistance) => {
1187
+ if (!bookContexts.length) return null;
1188
+
1189
+ // Find the most recent context before this position
1190
+ let bestContext = null;
1191
+ let bestDistance = Infinity;
1192
+
1193
+ for (const context of bookContexts) {
1194
+ if (context.position <= position) {
1195
+ const distance = position - context.endPosition;
1196
+ if (distance <= maxDistance && distance < bestDistance) {
1197
+ bestContext = context;
1198
+ bestDistance = distance;
1199
+ }
1200
+ }
1201
+ }
1202
+
1203
+ return bestContext;
1204
+ };
1205
+
1206
+ /**
1207
+ * Find implied references using context from explicit references
1208
+ * @param {string} content - The text content
1209
+ * @param {Array} explicitMatches - Array of explicit reference strings
1210
+ * @param {Array} explicitIndices - Array of [start, end] positions for explicit matches
1211
+ * @param {Function} lookupReference - Function to validate references
1212
+ * @param {Object} options - Detection options including maxContextDistance
1213
+ * @returns {Array} Array of [start, end, resolvedReference] tuples for implied references
1214
+ */
1215
+ const findImpliedReferences = (content, explicitMatches, explicitIndices, lookupReference, options) => {
1216
+ const matches = [];
1217
+
1218
+ // Find all explicit book references to establish context
1219
+ const bookContexts = findBookContexts(content, explicitMatches, explicitIndices, lookupReference);
1220
+
1221
+ // Simple pattern to match "vv. X, Y, Z:A, B:C" - treat it as one unit
1222
+ const verseGroupPattern = /\b(?:vv?\.?\s*|vs\.?\s*|verses?\s+)([\d:,\s–—-]+?)(?=\s*[).;\n]|$)/gi;
1223
+
1224
+ let match;
1225
+ while ((match = verseGroupPattern.exec(content)) !== null) {
1226
+ const fullMatch = match[0];
1227
+ const verseSpec = match[1].trim();
1228
+ const position = match.index;
1229
+
1230
+ // Find the nearest book context
1231
+ const context = getNearestBookContext(position, bookContexts, options.maxContextDistance);
1232
+ if (context) {
1233
+ // Replace the "vv." with the actual book and chapter
1234
+ const impliedRef = `${context.book} ${context.chapter}:${verseSpec}`;
1235
+ const verification = lookupReference(impliedRef);
1236
+
1237
+ if (verification.verse_ids && verification.verse_ids.length > 0) {
1238
+ matches.push([position, position + fullMatch.length, impliedRef]);
1239
+ }
1240
+ }
1241
+ }
1242
+
1243
+ // Also handle standalone chapter:verse patterns
1244
+ const chapterVersePattern = /\b(\d+):(\d+(?:[–—-]\d+)?(?:\s*,\s*\d+(?:[–—-]\d+)?)*)/g;
1245
+ while ((match = chapterVersePattern.exec(content)) !== null) {
1246
+ const fullMatch = match[0];
1247
+ const chapter = match[1];
1248
+ const verses = match[2];
1249
+ const position = match.index;
1250
+
1251
+ // Skip if this is part of an explicit reference we already found
1252
+ const isPartOfExplicit = explicitIndices && explicitIndices.some(([start, end]) =>
1253
+ position >= start && position < end
1254
+ );
1255
+
1256
+ if (isPartOfExplicit) {
1257
+ continue;
1258
+ }
1259
+
1260
+ // Find the nearest book context
1261
+ const context = getNearestBookContext(position, bookContexts, options.maxContextDistance);
1262
+ if (context) {
1263
+ const impliedRef = `${context.book} ${chapter}:${verses}`;
1264
+ const verification = lookupReference(impliedRef);
1265
+
1266
+ if (verification.verse_ids && verification.verse_ids.length > 0) {
1267
+ matches.push([position, position + fullMatch.length, impliedRef]);
1268
+ }
1269
+ }
1270
+ }
1271
+
1272
+ return matches;
1273
+ };
1274
+
1275
+ ;
664
1276
  // Browser localStorage key for language preference
665
1277
  const LANGUAGE_STORAGE_KEY = 'scriptureGuideUtils_language';
666
1278
 
@@ -658,9 +658,621 @@ const raw_lang = {
658
658
  }
659
659
  }
660
660
  ;
661
- import { processReferenceDetection } from './scriptdetect.mjs';
662
- import { detectReferencesWithContext } from './scriptdetectcontext.mjs';
661
+ // Shared utility functions
662
+ /**
663
+ * Shared utilities for scripture reference matching and processing
664
+ * Contains common logic used across detection modules
665
+ */
666
+
667
+ /**
668
+ * Check if two ranges overlap
669
+ * @param {Array} range1 - [start, end] tuple
670
+ * @param {Array} range2 - [start, end] tuple
671
+ * @returns {boolean} True if ranges overlap
672
+ */
673
+ const rangesOverlap = (range1, range2) => {
674
+ return range1[0] < range2[1] && range2[0] < range1[1];
675
+ };
676
+
677
+ /**
678
+ * Remove overlapping matches, with configurable preference logic
679
+ * @param {Array} matches - Array of match tuples
680
+ * @param {string} content - Original content (for debugging)
681
+ * @param {Function} preferenceResolver - Function to choose between overlapping matches
682
+ * @returns {Array} Non-overlapping matches
683
+ */
684
+ const removeOverlaps = (matches, content, preferenceResolver = null) => {
685
+ if (matches.length <= 1) return matches;
686
+
687
+ const result = [];
688
+
689
+ for (let i = 0; i < matches.length; i++) {
690
+ const current = matches[i];
691
+ let shouldInclude = true;
692
+
693
+ // Check for overlaps with already included matches
694
+ for (const included of result) {
695
+ if (rangesOverlap(current, included)) {
696
+ shouldInclude = false;
697
+ break;
698
+ }
699
+ }
700
+
701
+ if (shouldInclude) {
702
+ // Check for overlaps with remaining matches
703
+ for (let j = i + 1; j < matches.length; j++) {
704
+ const next = matches[j];
705
+ if (rangesOverlap(current, next)) {
706
+ // Use preference resolver or default to longer match
707
+ if (preferenceResolver) {
708
+ const preferred = preferenceResolver(current, next, content);
709
+ if (preferred !== current) {
710
+ shouldInclude = false;
711
+ break;
712
+ }
713
+ } else {
714
+ // Default: prefer longer match
715
+ const currentLength = current[1] - current[0];
716
+ const nextLength = next[1] - next[0];
717
+
718
+ if (nextLength > currentLength) {
719
+ shouldInclude = false;
720
+ break;
721
+ }
722
+ }
723
+ }
724
+ }
725
+ }
726
+
727
+ if (shouldInclude) {
728
+ result.push(current);
729
+ }
730
+ }
731
+
732
+ return result;
733
+ };
734
+
735
+ /**
736
+ * Calculate gaps between consecutive matches
737
+ * @param {Array} matches - Array of [start, end] tuples
738
+ * @returns {Array} Array of gap [start, end] tuples
739
+ */
740
+ const calculateGaps = (matches) => {
741
+ return matches.reduce((prev, current, index) => {
742
+ if (index === 0) return prev;
743
+ const lastPair = matches[index - 1];
744
+ const gap = [lastPair[1], current[0]];
745
+ return [...prev, gap];
746
+ }, []);
747
+ };
748
+
749
+ /**
750
+ * Calculate negative space (gaps) for final output construction
751
+ * @param {Array} matches - Array of [start, end] tuples
752
+ * @param {number} contentLength - Total length of content
753
+ * @returns {Array} Array of negative space [start, end] tuples
754
+ */
755
+ const calculateNegativeSpace = (matches, contentLength) => {
756
+ const negativeSpace = matches.reduce((prev, current, index, array) => {
757
+ if (index !== 0) {
758
+ const prevIndex = array[index - 1];
759
+ const gap = [prevIndex[1], current[0]];
760
+ prev.push(gap);
761
+ }
762
+ return prev;
763
+ }, []);
764
+
765
+ if (matches[0][0] !== 0) {
766
+ negativeSpace.unshift([0, matches[0][0]]);
767
+ }
768
+
769
+ if (matches[matches.length - 1][1] !== contentLength) {
770
+ negativeSpace.push([matches[matches.length - 1][1], contentLength]);
771
+ }
772
+
773
+ return negativeSpace;
774
+ };
775
+
776
+ /**
777
+ * Merge adjacent matches based on joiner patterns
778
+ * @param {Array} matches - Array of match tuples
779
+ * @param {Array} gapMergeFlags - Boolean array indicating which gaps can be merged
780
+ * @param {Function} compatibilityCheck - Optional function to check if matches can be merged
781
+ * @returns {Array} Merged matches
782
+ */
783
+ const mergeAdjacentMatches = (matches, gapMergeFlags, compatibilityCheck = null) => {
784
+ return matches.reduce((prev, current, index) => {
785
+ if (index === 0) {
786
+ return [current];
787
+ } else {
788
+ const prevIndex = prev[prev.length - 1];
789
+ const shouldMerge = gapMergeFlags[index - 1];
790
+
791
+ // Check compatibility if function provided
792
+ if (shouldMerge && compatibilityCheck) {
793
+ const isCompatible = compatibilityCheck(prevIndex, current);
794
+ if (!isCompatible) {
795
+ prev.push(current);
796
+ return prev;
797
+ }
798
+ }
799
+
800
+ if (shouldMerge) {
801
+ // Merge logic - keep first position, extend to second end, combine text and verse IDs
802
+ if (prevIndex.length > 2 && current.length > 2) {
803
+ // Extended format: [start, end, text, verseIds]
804
+ const startPos = prevIndex[0];
805
+ const endPos = current[1];
806
+ const combinedText = prevIndex[2] + ";" + current[2]; // Combine with semicolon
807
+ const combinedVerseIds = [...(prevIndex[3] || []), ...(current[3] || [])]; // Merge verse ID arrays
808
+ const merged = [startPos, endPos, combinedText, combinedVerseIds];
809
+ prev[prev.length - 1] = merged;
810
+ } else {
811
+ // Basic format: [start, end]
812
+ const merged = [prevIndex[0], current[1], ...(prevIndex.slice(2))];
813
+ prev[prev.length - 1] = merged;
814
+ }
815
+ } else {
816
+ prev.push(current);
817
+ }
818
+ return prev;
819
+ }
820
+ }, []);
821
+ };
822
+
823
+ /**
824
+ * Build final output by alternating between cut items and negative space
825
+ * @param {Array} cutItems - Processed match content
826
+ * @param {Array} negativeItems - Unprocessed content between matches
827
+ * @param {boolean} firstReferenceIsAtStart - Whether first match starts at position 0
828
+ * @returns {string} Final merged output
829
+ */
830
+ const buildFinalOutput = (cutItems, negativeItems, firstReferenceIsAtStart) => {
831
+ const maxCount = Math.max(cutItems.length, negativeItems.length);
832
+ const merged = [];
833
+
834
+ for (let i = 0; i < maxCount; i++) {
835
+ const firstItem = firstReferenceIsAtStart ? cutItems[i] : negativeItems[i];
836
+ const secondItem = firstReferenceIsAtStart ? negativeItems[i] : cutItems[i];
837
+ if (firstItem) merged.push(firstItem);
838
+ if (secondItem) merged.push(secondItem);
839
+ }
840
+
841
+ return merged.join("");
842
+ };
843
+
844
+ /**
845
+ * Default tie-breaker for scriptdetect.mjs overlap resolution
846
+ * @param {Array} pair1 - First match tuple
847
+ * @param {Array} pair2 - Second match tuple
848
+ * @param {string} content - Original content
849
+ * @returns {Array} Preferred match tuple
850
+ */
851
+ const defaultTieBreaker = (pair1, pair2, content) => {
852
+ const string1 = content.substring(pair1[0], pair1[1]);
853
+ const string2 = content.substring(pair2[0], pair2[1]);
854
+
855
+ // If one pair is all lower case, return the other one
856
+ if(/[^A-Z]/.test(string1) && !/[^A-Z]/.test(string2)) return pair2;
857
+ if(/[^A-Z]/.test(string2) && !/[^A-Z]/.test(string1)) return pair1;
858
+
859
+ if(string1.length > string2.length) return pair1;
860
+ if(string2.length > string1.length) return pair2;
861
+
862
+ return pair1;
863
+ };
864
+
865
+ // Script detection functions
866
+ const findMatchingBooks = (content,books) => {
867
+ const matchingBooks = books.filter(i=>(new RegExp(i,"ig")).test(content));
868
+ return matchingBooks;
869
+ }
870
+
871
+ const findMatches = (content,books,lang_extra) => {
872
+ // Pre-process content to normalize HTML entities to Unicode dashes
873
+ const htmlEntityMap = {
874
+ '&ndash;': '–',
875
+ '&mdash;': '—',
876
+ '&minus;': '−',
877
+ '&#8211;': '–',
878
+ '&#8212;': '—',
879
+ '&#8722;': '−'
880
+ };
881
+
882
+ let normalizedContent = content;
883
+ Object.entries(htmlEntityMap).forEach(([entity, unicode]) => {
884
+ normalizedContent = normalizedContent.replace(new RegExp(entity, 'g'), unicode);
885
+ });
886
+
887
+ const tail = lang_extra.tail ? new RegExp(lang_extra.tail,"ig") : /[^0-9]+$/;
888
+ const preBookMatch = lang_extra.book || `(First|I|1|1st|Second|II|2|2nd|Third|III|3|3rd|Fourth|IV|4|4th)*\\s*(books* of)*\\s*`;
889
+ const matchingBooks = findMatchingBooks(normalizedContent,books);
890
+ const postBookMatch = lang_extra.chapter || "([0-9:.;,~ —–-]|cf)*[0-9]+";
891
+ const fullBookMatches = matchingBooks.map(bookMatch=>{
892
+ const patternString = preBookMatch + bookMatch ;
893
+ const pattern = (new RegExp(patternString,"ig"));
894
+ const stringMatch = pattern.test(normalizedContent) ? patternString : null;
895
+ //console.log({pattern,content,stringMatch});
896
+ return stringMatch;
897
+ }).filter(x=>!!x);
898
+
899
+ const bookSubStrings = fullBookMatches.map(bookMatch=>{
900
+ const substrings = normalizedContent.match(new RegExp(bookMatch,"ig")).flat();
901
+ return substrings;
902
+ }).flat().reduce((prev,current)=>{
903
+ if(prev.includes(current)) return prev;
904
+ return [...prev,current]
905
+ },[]).filter(i=>!!i).map(substring=>{
906
+ substring = substring.trim();
907
+ let positions = [];
908
+ let index = normalizedContent.indexOf(substring);
909
+ while (index != -1) {
910
+ positions.push(index);
911
+ index = normalizedContent.indexOf(substring, index + 1);
912
+ } return [substring,positions];
913
+ }).map(([substring,positions])=>{
914
+ const preChars = positions.map(i=>normalizedContent.substring(i-1,i));
915
+ positions = positions.filter((i,index)=>{
916
+ const charRightBeforeMatch = preChars[index];
917
+ const leadingCharIsInvalid = !/^(\s|\W|)$/.test(charRightBeforeMatch);
918
+ return !leadingCharIsInvalid;
919
+ });
920
+ if(!positions.length) return false;
921
+ return [substring,positions];
922
+ }).filter(i=>!!i);
923
+
924
+ const possiblyOverlappingMatches = bookSubStrings.map(([substring,positions])=>{
925
+ return positions.map(i=>{
926
+ let pattern = null;
927
+ try{
928
+ pattern = new RegExp(substring+postBookMatch,"ig");
929
+ }catch(e){
930
+ return null;
931
+ }
932
+ const match = normalizedContent.slice(i).match(pattern)?.[0]?.replace(tail,"").trim();
933
+ if(!match) return null;
934
+ const len = match.length;
935
+ const pos = i;
936
+ const [posIn, posOut] = [pos,pos+len];
937
+ //console.log({content:content.slice(i),pattern,match,posIn,posOut});
938
+ return [match,posIn,posOut];
939
+ }
940
+ )}).flat().filter(i=>!!i);
941
+ const matchesWithReferences = possiblyOverlappingMatches.map(([string,start,end])=>{
942
+ const overLappingItems = possiblyOverlappingMatches.filter(([s,s1,e1])=>s!==string && s1<end && e1>end);
943
+ const hasOverlap = overLappingItems.length > 0;
944
+ const newEnd = hasOverlap ? Math.min(...overLappingItems.map(([s,s1,e1])=>s1)) : end;
945
+ const newString = normalizedContent.slice(start,newEnd);
946
+ return newString.replace(tail,"").trim();
947
+ }).filter(i=>!!i);
948
+ const matches = matchesWithReferences.map(string=>{
949
+ const pattern = (new RegExp(string,"ig"));
950
+ const matches = normalizedContent.match(pattern)?.map(i=>i.trim().replace(tail,""));
951
+ //console.log({string,matches,tail});
952
+ return matches;
953
+ }).flat()
954
+ .reduce((prev,current)=>{
955
+ if(prev.includes(current)) return prev;
956
+ return [...prev,current]
957
+ },[]).filter(i=>!!i);
958
+ return matches;
959
+ }
960
+
961
+
962
+
963
+ function findMatchIndexes(content, matches,lookupReference, lang_extra) {
964
+ const tail = lang_extra.tail ? new RegExp(lang_extra.tail,"ig") : /[^0-9]+$/;
965
+ const indexes = matches
966
+ .sort((b, a) => b.length - a.length)
967
+ .map(i=>{
968
+ const length = i.length;
969
+ let positions = [];
970
+ let strPos = content.indexOf(i);
971
+
972
+ while (strPos != -1) {
973
+ positions.push(strPos);
974
+ strPos = content.indexOf(i, strPos + 1);
975
+ }
976
+ return positions.map(i=>[i,i+length+2]);
977
+ }).flat()
978
+ .map(a=>{
979
+
980
+ const substring = content.substring(a[0],a[1]).trim().replace(tail,"").trim();
981
+ if(!substring) return false;
982
+ const verse_ids = lookupReference(substring).verse_ids;
983
+ //console.log({substring,verse_ids});
984
+ if(verse_ids.length > 0) return [a[0],a[0]+substring.length];
985
+
986
+ return false;
987
+ })
988
+ .filter(i=>!!i)
989
+ .sort((a, b) => a[0] - b[0])
990
+ .map(([start, end], index, array) => {
991
+ const nextStart = array[index+1]?.[0] || null;
992
+ end = nextStart ? Math.min(end,nextStart) : end;
993
+ return [start,end];
994
+ });
995
+
996
+ if(!indexes.length) return false;
997
+
998
+ // Use shared overlap removal with custom tie-breaker
999
+ const preferenceResolver = (pair1, pair2, content) => defaultTieBreaker(pair1, pair2, content);
1000
+ const nonOverlappingIndeces = removeOverlaps(indexes, content, preferenceResolver)
1001
+ .filter(([start,end])=>start!==end);
1002
+
1003
+ return nonOverlappingIndeces;
1004
+
1005
+
1006
+
1007
+ }
663
1008
 
1009
+
1010
+
1011
+
1012
+
1013
+ const processReferenceDetection = (content,books,lang_extra,lookupReference,callback) =>
1014
+ {
1015
+ try{
1016
+ lang_extra = lang_extra || {};
1017
+ const matches = findMatches(content,books,lang_extra);
1018
+ const matchIndeces = findMatchIndexes(content,matches,lookupReference,lang_extra);
1019
+ if(!matchIndeces) return content;
1020
+
1021
+ const gapsBetweenIndeces = calculateGaps(matchIndeces);
1022
+ const gapStrings = gapsBetweenIndeces.map(([start,end])=>content.substring(start,end).trim());
1023
+ //console.log({matches,matchIndeces,gapsBetweenIndeces,gapStrings});
1024
+ const joiners = lang_extra.joiners || ["^[;, ]*(and|c\\.*f\\.*)*$"];
1025
+ const gapThatMayBeMerged = gapsBetweenIndeces.map(([start,end],i)=>{
1026
+ const gapString = gapStrings[i];
1027
+ const canBeMerged = joiners.some(joiner=>(new RegExp(joiner,"ig")).test(gapString));
1028
+ const matchingJoiner = joiners.find(joiner=>(new RegExp(joiner,"ig")).test(gapString));
1029
+ //console.log({gapString,matchingJoiner});
1030
+ return canBeMerged;
1031
+ });
1032
+
1033
+ // Use shared merge logic
1034
+ const mergedIndeces = mergeAdjacentMatches(matchIndeces, gapThatMayBeMerged);
1035
+
1036
+ // Use shared negative space calculation
1037
+ const negativeSpace = calculateNegativeSpace(mergedIndeces, content.length);
1038
+
1039
+ //check content between matches. If puctuation only (or 'and'), then merge
1040
+ const cutItems = mergedIndeces.map(([start,end])=>{
1041
+ const string = content.substring(start,end);
1042
+ const lookupResult = lookupReference(string);
1043
+ return callback(lookupResult.query, lookupResult.verse_ids);
1044
+ });
1045
+
1046
+ const negativeItems = negativeSpace.map(([start,end])=>content.substring(start,end));
1047
+ const firstReferenceIsAtStart = mergedIndeces[0][0] === 0;
1048
+
1049
+ return buildFinalOutput(cutItems, negativeItems, firstReferenceIsAtStart);
1050
+ }catch(e){
1051
+ return content;
1052
+ }
1053
+ }
1054
+ // Context detection functions
1055
+ /**
1056
+ * Enhanced contextual scripture reference detection module
1057
+ * Handles detection of implied references using context from explicit references
1058
+ */
1059
+ // Enhanced reference detection with contextual processing
1060
+ const detectReferencesWithContext = (content, books, lang_extra, lookupReference, callback, options, generateReference = null) => {
1061
+ try {
1062
+ // Step 1: Find explicit references using existing logic
1063
+ const explicitMatches = findMatches(content, books, lang_extra);
1064
+ const explicitIndices = findMatchIndexes(content, explicitMatches, lookupReference, lang_extra);
1065
+
1066
+ // Step 2: Find implied references using context
1067
+ const impliedMatches = findImpliedReferences(content, explicitMatches, explicitIndices, lookupReference, options);
1068
+
1069
+ // Step 3: Merge and sort all matches - store both original text and verse IDs
1070
+ const explicitMatchesWithRefs = (explicitIndices || []).map(([start, end]) => {
1071
+ const originalText = content.substring(start, end);
1072
+ const verification = lookupReference(originalText);
1073
+ return [start, end, originalText, verification.verse_ids];
1074
+ });
1075
+
1076
+ // Convert implied matches to include verse IDs
1077
+ const impliedMatchesWithVerseIds = impliedMatches.map(([start, end, resolvedRef]) => {
1078
+ const originalText = content.substring(start, end);
1079
+ const verification = lookupReference(resolvedRef);
1080
+ return [start, end, originalText, verification.verse_ids];
1081
+ });
1082
+
1083
+ const allMatches = [...explicitMatchesWithRefs, ...impliedMatchesWithVerseIds];
1084
+ allMatches.sort((a, b) => a[0] - b[0]);
1085
+
1086
+ // Step 4: Remove overlaps, preferring explicit matches
1087
+ const nonOverlappingMatches = removeOverlaps(allMatches, content);
1088
+
1089
+ if (!nonOverlappingMatches || nonOverlappingMatches.length === 0) {
1090
+ return content;
1091
+ }
1092
+
1093
+ // Step 5: Apply the same gap merging logic as original
1094
+ const gapsBetweenIndices = calculateGaps(nonOverlappingMatches);
1095
+
1096
+ const joiners = lang_extra.joiners || ["^[;]*(and|cf\\.)*$", "^\\s*[;,]\\s*$"]; // More restrictive joiners
1097
+ const gapThatMayBeMerged = gapsBetweenIndices.map(([start, end]) => {
1098
+ const gapString = content.substring(start, end).trim();
1099
+ return joiners.some(joiner => (new RegExp(joiner, "ig")).test(gapString));
1100
+ });
1101
+
1102
+ // Compatibility check for merging references
1103
+ const compatibilityCheck = (prevIndex, current) => {
1104
+ // Always allow merging for explicit references separated by joiners
1105
+ // The user's intention is clear when they list references like "Isa 45.22;Morm 3.18;Moro 10.24"
1106
+ return true;
1107
+ };
1108
+
1109
+ // Merge adjacent matches if gaps are joiners and references are compatible
1110
+ const mergedIndices = mergeAdjacentMatches(nonOverlappingMatches, gapThatMayBeMerged, compatibilityCheck);
1111
+
1112
+ // Step 6: Build final output
1113
+ const negativeSpace = calculateNegativeSpace(mergedIndices, content.length);
1114
+
1115
+ // Use the stored reference and verse IDs
1116
+ const cutItems = mergedIndices.map(([start, end, originalText, verseIds]) => {
1117
+ // Pass verseIds to callback for compatibility with expected signature
1118
+ return callback(originalText, verseIds);
1119
+ });
1120
+
1121
+ const negativeItems = negativeSpace.map(([start, end]) => content.substring(start, end));
1122
+ const firstReferenceIsAtStart = mergedIndices[0][0] === 0;
1123
+
1124
+ const result = buildFinalOutput(cutItems, negativeItems, firstReferenceIsAtStart);
1125
+ return result;
1126
+ } catch (e) {
1127
+ console.warn('Enhanced reference detection failed, falling back to basic detection:', e);
1128
+ return processReferenceDetection(content, books, lang_extra, lookupReference, callback);
1129
+ }
1130
+ };
1131
+
1132
+ /**
1133
+ * Find all book contexts from explicit references
1134
+ * @param {string} content - The text content
1135
+ * @param {Array} explicitMatches - Array of explicit reference strings
1136
+ * @param {Array} explicitIndices - Array of [start, end] positions for explicit matches
1137
+ * @param {Function} lookupReference - Function to validate references
1138
+ * @returns {Array} Array of context objects with book, chapter, and position info
1139
+ */
1140
+ const findBookContexts = (content, explicitMatches, explicitIndices, lookupReference) => {
1141
+ const contexts = [];
1142
+
1143
+ // Use the already found explicit matches and their positions
1144
+ if (explicitMatches && explicitIndices) {
1145
+ for (let i = 0; i < explicitMatches.length; i++) {
1146
+ const match = explicitMatches[i];
1147
+ const [start, end] = explicitIndices[i];
1148
+
1149
+ const verification = lookupReference(match);
1150
+
1151
+ if (verification.verse_ids && verification.verse_ids.length > 0) {
1152
+ // Extract book and chapter from the verified reference
1153
+ // Handle both "Book Chapter:Verse" and "Book Chapter" (like D&C 76) patterns
1154
+ let refParts = verification.ref.match(/^(.+?)\s+(\d+):/);
1155
+ if (!refParts) {
1156
+ // Try pattern for section-only references like "Doctrine and Covenants 76"
1157
+ refParts = verification.ref.match(/^(.+?)\s+(\d+)$/);
1158
+ }
1159
+
1160
+ if (refParts) {
1161
+ const cleanBook = refParts[1];
1162
+ const chapter = parseInt(refParts[2]);
1163
+
1164
+ contexts.push({
1165
+ position: start,
1166
+ book: cleanBook,
1167
+ chapter: chapter,
1168
+ endPosition: end
1169
+ });
1170
+ }
1171
+ }
1172
+ }
1173
+ }
1174
+
1175
+ const result = contexts.sort((a, b) => a.position - b.position);
1176
+ return result;
1177
+ };
1178
+
1179
+ /**
1180
+ * Get the nearest book context for a given position
1181
+ * @param {number} position - Character position in the text
1182
+ * @param {Array} bookContexts - Array of context objects from findBookContexts
1183
+ * @param {number} maxDistance - Maximum distance to look for context
1184
+ * @returns {Object|null} The nearest context object or null if none found
1185
+ */
1186
+ const getNearestBookContext = (position, bookContexts, maxDistance) => {
1187
+ if (!bookContexts.length) return null;
1188
+
1189
+ // Find the most recent context before this position
1190
+ let bestContext = null;
1191
+ let bestDistance = Infinity;
1192
+
1193
+ for (const context of bookContexts) {
1194
+ if (context.position <= position) {
1195
+ const distance = position - context.endPosition;
1196
+ if (distance <= maxDistance && distance < bestDistance) {
1197
+ bestContext = context;
1198
+ bestDistance = distance;
1199
+ }
1200
+ }
1201
+ }
1202
+
1203
+ return bestContext;
1204
+ };
1205
+
1206
+ /**
1207
+ * Find implied references using context from explicit references
1208
+ * @param {string} content - The text content
1209
+ * @param {Array} explicitMatches - Array of explicit reference strings
1210
+ * @param {Array} explicitIndices - Array of [start, end] positions for explicit matches
1211
+ * @param {Function} lookupReference - Function to validate references
1212
+ * @param {Object} options - Detection options including maxContextDistance
1213
+ * @returns {Array} Array of [start, end, resolvedReference] tuples for implied references
1214
+ */
1215
+ const findImpliedReferences = (content, explicitMatches, explicitIndices, lookupReference, options) => {
1216
+ const matches = [];
1217
+
1218
+ // Find all explicit book references to establish context
1219
+ const bookContexts = findBookContexts(content, explicitMatches, explicitIndices, lookupReference);
1220
+
1221
+ // Simple pattern to match "vv. X, Y, Z:A, B:C" - treat it as one unit
1222
+ const verseGroupPattern = /\b(?:vv?\.?\s*|vs\.?\s*|verses?\s+)([\d:,\s–—-]+?)(?=\s*[).;\n]|$)/gi;
1223
+
1224
+ let match;
1225
+ while ((match = verseGroupPattern.exec(content)) !== null) {
1226
+ const fullMatch = match[0];
1227
+ const verseSpec = match[1].trim();
1228
+ const position = match.index;
1229
+
1230
+ // Find the nearest book context
1231
+ const context = getNearestBookContext(position, bookContexts, options.maxContextDistance);
1232
+ if (context) {
1233
+ // Replace the "vv." with the actual book and chapter
1234
+ const impliedRef = `${context.book} ${context.chapter}:${verseSpec}`;
1235
+ const verification = lookupReference(impliedRef);
1236
+
1237
+ if (verification.verse_ids && verification.verse_ids.length > 0) {
1238
+ matches.push([position, position + fullMatch.length, impliedRef]);
1239
+ }
1240
+ }
1241
+ }
1242
+
1243
+ // Also handle standalone chapter:verse patterns
1244
+ const chapterVersePattern = /\b(\d+):(\d+(?:[–—-]\d+)?(?:\s*,\s*\d+(?:[–—-]\d+)?)*)/g;
1245
+ while ((match = chapterVersePattern.exec(content)) !== null) {
1246
+ const fullMatch = match[0];
1247
+ const chapter = match[1];
1248
+ const verses = match[2];
1249
+ const position = match.index;
1250
+
1251
+ // Skip if this is part of an explicit reference we already found
1252
+ const isPartOfExplicit = explicitIndices && explicitIndices.some(([start, end]) =>
1253
+ position >= start && position < end
1254
+ );
1255
+
1256
+ if (isPartOfExplicit) {
1257
+ continue;
1258
+ }
1259
+
1260
+ // Find the nearest book context
1261
+ const context = getNearestBookContext(position, bookContexts, options.maxContextDistance);
1262
+ if (context) {
1263
+ const impliedRef = `${context.book} ${chapter}:${verses}`;
1264
+ const verification = lookupReference(impliedRef);
1265
+
1266
+ if (verification.verse_ids && verification.verse_ids.length > 0) {
1267
+ matches.push([position, position + fullMatch.length, impliedRef]);
1268
+ }
1269
+ }
1270
+ }
1271
+
1272
+ return matches;
1273
+ };
1274
+
1275
+ ;
664
1276
  // Browser localStorage key for language preference
665
1277
  const LANGUAGE_STORAGE_KEY = 'scriptureGuideUtils_language';
666
1278
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "scripture-guide",
3
- "version": "1.0.83",
3
+ "version": "1.0.84",
4
4
  "description": "A utility for biblical and scripture reference parsing that enables structured queries based on strings of traditional scripture references (e.g. John 3:16). ",
5
5
  "main": "dist/scriptures.cjs",
6
6
  "module": "dist/scriptures.mjs",