-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathlinks.js
More file actions
58 lines (54 loc) · 1.76 KB
/
Copy pathlinks.js
File metadata and controls
58 lines (54 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
exports.parseReference = parseReference = (reference) => {
let [id, fragment] = reference.split('#');
return {id, fragment};
}
// Note: The result set includes also the document itself or its main document
exports.getRelatedDocuments = ({isPartOf, links}) =>
new Set(
links.reduce((l, {object, subject}) => [...l, subject, object], [])
.filter(x => !!x)
.concat(isPartOf)
.map(x => parseReference(x).id)
);
// Should have the same definition as in `frontend/src/parallelDocuments.js`
const parseText = (text) => {
if (!text) return [];
const PASSAGE = /{([^{]+)} ([^{]*)/g;
let passages = [...text.matchAll(PASSAGE)];
passages = (passages.length) ? passages : [[null, '0', text]];
return passages.map(([_, rubric, passage]) => ({
rubric,
passage,
parsed_rubric: rubric.match(/(?:(\d+)[:., ])?(\d+) ?([a-z]?)/)
.slice(1)
.filter(x => !!x)
.map(x => {
let n = Number(x) ;
return Number.isNaN(n) ? x : n;
})
}));
}
exports.emitPassages = ({text, isPartOf, related, beingEditedBy}) => {
parseText(text).forEach(({rubric, passage, parsed_rubric}) =>
related.forEach((x) => {
emit([x, ...parsed_rubric], {
text: passage,
isPartOf,
rubric,
_id: null,
...(beingEditedBy && {beingEditedBy})
});
})
);
}
exports.emitIncludedDocuments = ({isPartOf, links}) => {
let includedDocuments = links
.filter(x => x.verb === 'includes')
.map(x => parseReference(x.object));
includedDocuments.forEach((x, i) => {
emit([isPartOf, i], {inclusion: x.fragment || 'whole', isPartOf, _id: x.id});
includedDocuments.forEach((y, j) => {
emit([x.id, j], {inclusion: y.fragment || 'whole', isPartOf, _id: y.id});
});
});
}