@@ -12,15 +12,31 @@ exports.getRelatedDocuments = ({isPartOf, links}) =>
1212 . map ( x => parseReference ( x ) . id )
1313 ) ;
1414
15- exports . emitPassages = ( { text, isPartOf, related} ) => {
15+ // Should have the same definition as in `frontend/src/parallelDocuments.js`
16+ const parseText = ( text ) => {
17+ if ( ! text ) return [ ] ;
1618 const PASSAGE = / { ( [ ^ { ] + ) } ( [ ^ { ] * ) / g;
1719 let passages = [ ...text . matchAll ( PASSAGE ) ] ;
18- passages = ( passages . length ) ? passages : [ [ null , null , text ] ] ;
19- passages . forEach ( ( [ _ , rubric , passage ] ) => {
20- related . forEach ( x => {
21- emit ( [ x , Number ( rubric ) ] , { text : passage , isPartOf, _id : null } ) ;
22- } ) ;
23- } ) ;
20+ passages = ( passages . length ) ? passages : [ [ null , '0' , text ] ] ;
21+ return passages . map ( ( [ _ , rubric , passage ] ) => ( {
22+ rubric,
23+ passage,
24+ parsed_rubric : rubric . match ( / (?: ( \d + ) [: ., ] ) ? ( \d + ) ? ( [ a - z ] ? ) / )
25+ . slice ( 1 )
26+ . filter ( x => ! ! x )
27+ . map ( x => {
28+ let n = Number ( x ) ;
29+ return Number . isNaN ( n ) ? x : n ;
30+ } )
31+ } ) ) ;
32+ }
33+
34+ exports . emitPassages = ( { text, isPartOf, related} ) => {
35+ parseText ( text ) . forEach ( ( { rubric, passage, parsed_rubric} ) =>
36+ related . forEach ( ( x ) => {
37+ emit ( [ x , ...parsed_rubric ] , { text : passage , isPartOf, rubric, _id : null } ) ;
38+ } )
39+ ) ;
2440}
2541
2642exports . emitIncludedDocuments = ( { isPartOf, links} ) => {
0 commit comments