-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmarkdown.js
180 lines (149 loc) · 3.91 KB
/
markdown.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
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
import React, { Component } from 'react'
import {
View,
Text,
ScrollView,
} from 'react-native'
import { parse } from './parser'
import DEFAULT_STYLES from './styles'
import List from './components/list'
import Image from './components/image'
import LineBreak from './components/linebreak'
import pick from 'object.pick'
import omit from 'object.omit'
import {
text as TextStyleProps,
view as ViewStyleProps
} from './style-props'
import {
name as packageName
} from './package.json'
const log = (...args) => {
args.unshift(packageName)
return console.log(...args)
}
const TextOnlyStyleProps = (function () {
const props = {}
TextStyleProps.forEach(prop => {
props[prop] = true
})
ViewStyleProps.forEach(prop => {
delete props[prop]
})
return Object.keys(props)
}())
const DEFAULT_PADDING = 10
// const WrappedText = ({ ...props }) => {
// return (
// <View>
// <Text {...props}></Text>
// </View>
// )
// }
const DEFAULT_RENDERERS = {
container: ScrollView,
text: Text,
br: LineBreak,
}
const DEFAULT_CUSTOM_RENDERERS = {
image: Image,
list: List
}
export default function createMarkdownRenderer (markedOpts) {
const typeToRenderer = {
...DEFAULT_RENDERERS,
...DEFAULT_CUSTOM_RENDERERS
}
function renderGroup ({ markdown, markdownStyles={}, style, key, passThroughProps }) {
const { type, children, ordered, depth, text } = markdown
let El = typeToRenderer[type]
if (!El) {
El = View
}
const isText = type === 'text'
let elStyles = getStyles({ markdown, markdownStyles, textOnly: isText })
// only for the container
if (isText) {
const ancestorStyles = getAncestorTextStyles({ markdown, markdownStyles })
// counterintuitive, but works better
elStyles = elStyles.concat(ancestorStyles)
}
if (style) elStyles.push(style)
let contents
if (isText) {
contents = text
} else if (children) {
contents = children.map((group, i) => {
return renderGroup({
markdown: group,
markdownStyles,
key: `child-${i}`,
passThroughProps
})
})
}
const elProps = {
style: flatten(elStyles),
// styles,
// markdown,
key,
passThroughProps
}
if (El !== DEFAULT_RENDERERS[type]) {
elProps.markdown = markdown
}
return (
<El {...elProps}>
{contents}
</El>
)
}
const Markdown = ({ children, passThroughProps={}, ...rest }) => {
const parsed = parse(children, markedOpts)
return renderGroup({ markdown: parsed, passThroughProps, ...rest })
}
// allow override renderers and textContainers for this markdown instance
Markdown.renderer = typeToRenderer
return Markdown
}
function getStyles ({ markdown, markdownStyles, textOnly }) {
const { type, depth } = markdown
const styleNames = [type]
if (type === 'heading') styleNames.push(type + depth)
return styleNames
.map(styleName => {
const defaultStyle = DEFAULT_STYLES[styleName]
if (!defaultStyle) {
log(`don't have style mapping for "${styleName}"`)
}
return defaultStyle
})
.concat(styleNames.map(styleName => markdownStyles[styleName]))
.filter(style => style != null)
.map(style => {
if (typeof style !== 'object') return style
if (textOnly) {
return pick(style, TextOnlyStyleProps)
}
return omit(style, TextOnlyStyleProps)
})
}
function getAncestorTextStyles ({ markdown, markdownStyles }) {
let textStyles = []
let current = markdown
while (current = current.parent) {
textStyles = getStyles({
markdown: current,
markdownStyles,
textOnly: true
}).concat(textStyles)
}
return textStyles
}
function flatten (styleArray) {
return styleArray.reduce((flat, next) => {
return { ...flat, ...next }
}, {})
}
// allow override defaults
export const renderer = DEFAULT_RENDERERS