-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
54 lines (48 loc) · 1.9 KB
/
index.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
import { Mobiledoc, CreateElement, ElementTypeGetter, Node } from './types'
import * as MobiledocTypes from './types/Mobiledoc'
import SectionRenderer from './SectionRenderer'
import upgradeMobiledoc from './upgradeMobiledoc'
import getMarkupComponentDefault from './getMarkupComponentDefault'
import { throwError, semverMatchesMinor } from './utils'
export { Mobiledoc, MobiledocTypes }
export { upgradeMobiledoc, getMarkupComponentDefault }
export const SUPPORTED_MOBILEDOC_VERSION = '0.3.1'
export const canParse = semverMatchesMinor(SUPPORTED_MOBILEDOC_VERSION)
/* Our friendly renderer */
export interface Options {
createElement: CreateElement
getCardComponent?: ElementTypeGetter
getAtomComponent?: ElementTypeGetter
getMarkupComponent?: ElementTypeGetter
}
export default ({
createElement = () =>
throwError(
`You must pass a \`createElement\` (\`(type, properties, children) => Node\`) function to \`MobiledocVdomRenderer\` in order to render output.`
),
getCardComponent = type =>
throwError(
`You must pass a \`getCardComponent\` (\`type => Component\`) function to \`MobiledocVdomRenderer\` in order to render cards (i.e. \`'${type}'\`).`
),
getAtomComponent = type =>
throwError(
`You must pass a \`getAtomComponent\` (\`type => Component\`) function to \`MobiledocVdomRenderer\` in order to render atoms (i.e. \`'${type}'\`).`
),
getMarkupComponent = getMarkupComponentDefault
}: Options) => (mobiledoc: Mobiledoc): Node[] =>
canParse(mobiledoc.version)
? mobiledoc.sections.map(
SectionRenderer({
createElement,
getCardComponent,
getAtomComponent,
getMarkupComponent
})({
markups: mobiledoc.markups,
cards: mobiledoc.cards,
atoms: mobiledoc.atoms
})
)
: throwError(
`Unable to parse the passed Mobiledoc version \`'${mobiledoc.version}'\`.`
)