/
0-3-2.ts
195 lines (172 loc) · 6.25 KB
/
0-3-2.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { visit, visitArray, compile, Opcodes } from '../../utils/compiler'
import { objectToSortedKVArray } from '../../utils/array-utils'
import { Type } from '../../models/types'
import Post from '../../models/post'
import MarkupSection from '../../models/markup-section'
import ListSection from '../../models/list-section'
import ListItem from '../../models/list-item'
import Image from '../../models/image'
import Card from '../../models/card'
import Marker from '../../models/marker'
import Markup from '../../models/markup'
import Atom from '../../models/atom'
import { Dict } from '../../utils/types'
import { MobiledocSectionKind, MobiledocMarkerKind } from './constants'
import { MobiledocCard, MobiledocAtom, MobiledocMarker, MobiledocSection, MobiledocMarkerType } from './0-3'
export const MOBILEDOC_VERSION = '0.3.2'
export type MobiledocAttributedMarkupSection = [MobiledocSectionKind.MARKUP, string, MobiledocMarker[], string[]]
export type MobiledocAttributedListSection = [MobiledocSectionKind.LIST, string, MobiledocMarker[][], string[]]
export type MobiledocAttributedSection =
| MobiledocSection
| MobiledocAttributedMarkupSection
| MobiledocAttributedListSection
const visitor = {
[Type.POST](node: Post, opcodes: Opcodes) {
opcodes.push(['openPost'])
visitArray(visitor, node.sections, opcodes)
},
[Type.MARKUP_SECTION](node: MarkupSection, opcodes: Opcodes) {
opcodes.push(['openMarkupSection', node.tagName, objectToSortedKVArray(node.attributes)])
visitArray(visitor, node.markers, opcodes)
},
[Type.LIST_SECTION](node: ListSection, opcodes: Opcodes) {
opcodes.push(['openListSection', node.tagName, objectToSortedKVArray(node.attributes)])
visitArray(visitor, node.items, opcodes)
},
[Type.LIST_ITEM](node: ListItem, opcodes: Opcodes) {
opcodes.push(['openListItem'])
visitArray(visitor, node.markers, opcodes)
},
[Type.IMAGE_SECTION](node: Image, opcodes: Opcodes) {
opcodes.push(['openImageSection', node.src])
},
[Type.CARD](node: Card, opcodes: Opcodes) {
opcodes.push(['openCardSection', node.name, node.payload])
},
[Type.MARKER](node: Marker, opcodes: Opcodes) {
opcodes.push(['openMarker', node.closedMarkups.length, node.value])
visitArray(visitor, node.openedMarkups, opcodes)
},
[Type.MARKUP](node: Markup, opcodes: Opcodes) {
opcodes.push(['openMarkup', node.tagName, objectToSortedKVArray(node.attributes)])
},
[Type.ATOM](node: Atom, opcodes: Opcodes) {
opcodes.push(['openAtom', node.closedMarkups.length, node.name, node.value, node.payload])
visitArray(visitor, node.openedMarkups, opcodes)
},
}
class PostOpcodeCompiler {
markupMarkerIds!: number[]
markers!: MobiledocMarker[]
sections!: MobiledocAttributedSection[]
items!: MobiledocMarker[][]
markerTypes!: MobiledocMarkerType[]
atomTypes!: MobiledocAtom[]
cardTypes!: MobiledocCard[]
result!: MobiledocV0_3_2
_markerTypeCache!: Dict<number>
openMarker(closeCount: number, value: string) {
this.markupMarkerIds = []
this.markers.push([MobiledocMarkerKind.MARKUP, this.markupMarkerIds, closeCount, value || ''])
}
openAtom(closeCount: number, name: string, value: string, payload: {}) {
const index = this._addAtomTypeIndex(name, value, payload)
this.markupMarkerIds = []
this.markers.push([MobiledocMarkerKind.ATOM, this.markupMarkerIds, closeCount, index])
}
openMarkupSection(tagName: string, attributes: string[]) {
this.markers = []
if (attributes && attributes.length !== 0) {
this.sections.push([MobiledocSectionKind.MARKUP, tagName, this.markers, attributes])
} else {
this.sections.push([MobiledocSectionKind.MARKUP, tagName, this.markers])
}
}
openListSection(tagName: string, attributes: string[]) {
this.items = []
if (attributes && attributes.length !== 0) {
this.sections.push([MobiledocSectionKind.LIST, tagName, this.items, attributes])
} else {
this.sections.push([MobiledocSectionKind.LIST, tagName, this.items])
}
}
openListItem() {
this.markers = []
this.items.push(this.markers)
}
openImageSection(url: string) {
this.sections.push([MobiledocSectionKind.IMAGE, url])
}
openCardSection(name: string, payload: {}) {
const index = this._addCardTypeIndex(name, payload)
this.sections.push([MobiledocSectionKind.CARD, index])
}
openPost() {
this.atomTypes = []
this.cardTypes = []
this.markerTypes = []
this.sections = []
this.result = {
version: MOBILEDOC_VERSION,
atoms: this.atomTypes,
cards: this.cardTypes,
markups: this.markerTypes,
sections: this.sections,
}
}
openMarkup(tagName: string, attributes: string[]) {
const index = this._findOrAddMarkerTypeIndex(tagName, attributes)
this.markupMarkerIds.push(index)
}
_addCardTypeIndex(cardName: string, payload: {}) {
let cardType: MobiledocCard = [cardName, payload]
this.cardTypes.push(cardType)
return this.cardTypes.length - 1
}
_addAtomTypeIndex(atomName: string, atomValue: string, payload: {}) {
let atomType: MobiledocAtom = [atomName, atomValue, payload]
this.atomTypes.push(atomType)
return this.atomTypes.length - 1
}
_findOrAddMarkerTypeIndex(tagName: string, attributesArray: string[]) {
if (!this._markerTypeCache) {
this._markerTypeCache = {}
}
const key = `${tagName}-${attributesArray.join('-')}`
let index = this._markerTypeCache[key]
if (index === undefined) {
let markerType: MobiledocMarkerType = [tagName]
if (attributesArray.length) {
markerType.push(attributesArray)
}
this.markerTypes.push(markerType)
index = this.markerTypes.length - 1
this._markerTypeCache[key] = index
}
return index
}
}
export interface MobiledocV0_3_2 {
version: typeof MOBILEDOC_VERSION
atoms: MobiledocAtom[]
cards: MobiledocCard[]
markups: MobiledocMarkerType[]
sections: MobiledocAttributedSection[]
}
/**
* Render from post -> mobiledoc
*/
const MobiledocRenderer = {
/**
* @param {Post}
* @return {Mobiledoc}
*/
render(post: Post): MobiledocV0_3_2 {
let opcodes: Opcodes = []
visit(visitor, post, opcodes)
let compiler = new PostOpcodeCompiler()
compile(compiler, opcodes)
return compiler.result
},
}
export default MobiledocRenderer