scripture-guide 1.0.83 → 1.0.85

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