This repository has been archived by the owner on Mar 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutilities.js
141 lines (113 loc) · 3.4 KB
/
utilities.js
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Complete an array of promises in order, each one waiting for the previous one to resolve
function orderPromises(promiseFns) {
if (!Array.isArray(promiseFns) || (promiseFns.length && typeof promiseFns[0] !== 'function')) {
throw new TypeError(`orderPromises expects an array of functions. Received: ${JSON.stringify(promiseFns)}`)
}
if (!promiseFns.length) {
return Promise.resolve()
}
const promise = promiseFns[0]()
if (!promise.then) {
throw new TypeError(`A function in the array passed to orderPromises did not return a promise. Returned: ${JSON.stringify(promise)}`)
}
return promise.then(() => orderPromises(promiseFns.slice(1)))
}
// getNewLine, getNewSegment, getStyles, getStyledArrayFromChapters:
// From an array of Edward chapters, produce an array of Line objects.
// Line { text: string | Segment[], style: string | string[], pageBreak: undefined | 'before' }
// Segment { text: string, style: string[] }
// Line style strings: h1|h2|h3|blockquote|ul|ol|chapterHeading
// Segment style strings: bold|italic|underline|strike
function getNewLine() {
return {
text: [],
style: [],
}
}
function getNewSegment(text, style) {
return {
text: text || '',
style: style || [],
}
}
const listMap = {
bullet: 'ul',
ordered: 'ol',
}
function getStyles(attributes) {
const styles = []
const lineStyles = []
if (!attributes) {
return { styles, lineStyles }
}
for (const attr in attributes) {
if (['bold', 'italic', 'underline', 'strike'].includes(attr)) {
styles.push(attr)
continue
}
if (attr === 'header') {
lineStyles.push(`h${attributes[attr]}`)
continue
}
if (attr === 'blockquote') {
lineStyles.push('blockquote')
continue
}
if (attr === 'list') {
lineStyles.push(listMap[attributes[attr]])
continue
}
}
return { styles, lineStyles }
}
function getStyledArrayFromChapters(chapters) {
const splitContent = []
for (const chapter of chapters) {
splitContent.push({
text: chapter.title,
style: 'chapterHeading',
pageBreak: 'before',
})
let currentLine = getNewLine()
const ops = (chapter.content && chapter.content.ops) || []
for (const op of ops) {
const insert = op.insert
const { styles, lineStyles } = getStyles(op.attributes)
if (lineStyles.length) {
currentLine.style.push(...lineStyles)
}
if (insert === '\n') {
splitContent.push(currentLine)
currentLine = getNewLine()
continue
}
if (insert.includes('\n')) {
const [first, ...lines] = insert.split('\n')
if (first) {
const segment = getNewSegment(first, styles)
currentLine.text.push(segment)
}
splitContent.push(currentLine)
currentLine = getNewLine()
for (let ix = 0; ix < lines.length - 1; ix++) {
const line = lines[ix]
const segment = getNewSegment(line, styles)
currentLine.text.push(segment)
splitContent.push(currentLine)
currentLine = getNewLine()
}
const last = lines[lines.length - 1]
const segment = getNewSegment(last, styles)
currentLine.text.push(segment)
continue
}
const segment = getNewSegment(insert, styles)
currentLine.text.push(segment)
}
}
return splitContent
}
export {
orderPromises,
getStyledArrayFromChapters
}