/
Section.ts
95 lines (82 loc) · 1.98 KB
/
Section.ts
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { Parent } from './interfaces';
import { Asset, Note } from './index';
import { NPXObject } from './NPXObject';
import { MarkdownNote } from './Note';
export default class Section extends NPXObject implements Parent {
constructor(
public readonly title: string,
public readonly sections: Section[] = [],
public readonly notes: Note[] = [],
internalRef?: string,
parent?: Parent | string
) {
super(title, internalRef, parent);
}
public addSection(section: Section): Section {
const sectionClone = section.clone();
const parent = this.clone({
sections: [
...this.sections,
sectionClone
]
});
sectionClone.parent = parent;
return parent;
}
public addNote(note: Note): Section {
const noteClone = note.clone();
const parent = this.clone({
notes: [
...this.notes,
noteClone
]
});
noteClone.parent = parent;
return parent;
}
public search(query: string): Note[] {
const subSectionNotes: Note[] = this.sections
.map(s => s.search(query))
.reduce((acc, val) => acc.concat(val), []);
return [
...this.notes.filter(n => n.search(query).length > 0),
...subSectionNotes
];
}
public toXmlObject(): any {
return {
section: {
$: {
title: this.title
},
section: this.sections.map(s => s.toXmlObject().section),
note: this.notes.map(n => n.toXmlObject().note)
}
};
}
public async toMarkdown(assets: Asset[]): Promise<MarkdownNote[]> {
const subSectionNotes: MarkdownNote[] = (
await Promise.all(
this.sections.map(s => s.toMarkdown(assets))
)).reduce((acc, val) => acc.concat(val), []);
return [
...await Promise.all([
...this.notes.map(n => n.toMarkdown(assets)),
]),
...subSectionNotes
];
}
public clone(opts: Partial<Section> = {}): Section {
return new Section(
opts.title || this.title,
[
...(opts.sections || this.sections),
],
[
...(opts.notes || this.notes),
],
opts.internalRef || this.internalRef,
opts.parent || this.parent
);
}
}