This repository has been archived by the owner on May 9, 2022. It is now read-only.
/
index.tsx
250 lines (225 loc) · 7.8 KB
/
index.tsx
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* @file Markdown 解析服务
* @module components/common/markdown
* @author Surmon <https://github.com/surmon-china>
*/
import marked from 'marked'
import Hljs from 'highlight.js'
import React, { Component } from 'react'
import { StyleSheet, View, ViewStyle } from 'react-native'
import { observable, computed, action } from 'mobx'
import { observer } from 'mobx-react/native'
import { boundMethod } from 'autobind-decorator'
import { webUrl } from '@app/config'
import { EHomeRoutes } from '@app/routes'
import { INavigationProps } from '@app/types/props'
import { LANGUAGE_KEYS } from '@app/constants/language'
import { ImageViewerModal } from '@app/components/common/image-viewer'
import { AutoActivityIndicator } from '@app/components/common/activity-indicator'
import i18n from '@app/services/i18n'
import * as markdownStyles from './styles'
const AutoHeightWebView = require('react-native-autoheight-webview').default
Hljs.registerLanguage('go', require('highlight.js/lib/languages/go'))
Hljs.registerLanguage('css', require('highlight.js/lib/languages/css'))
Hljs.registerLanguage('sql', require('highlight.js/lib/languages/sql'))
Hljs.registerLanguage('php', require('highlight.js/lib/languages/php'))
Hljs.registerLanguage('xml', require('highlight.js/lib/languages/xml'))
Hljs.registerLanguage('json', require('highlight.js/lib/languages/json'))
Hljs.registerLanguage('bash', require('highlight.js/lib/languages/bash'))
Hljs.registerLanguage('less', require('highlight.js/lib/languages/less'))
Hljs.registerLanguage('scss', require('highlight.js/lib/languages/scss'))
Hljs.registerLanguage('shell', require('highlight.js/lib/languages/shell'))
Hljs.registerLanguage('nginx', require('highlight.js/lib/languages/nginx'))
Hljs.registerLanguage('stylus', require('highlight.js/lib/languages/stylus'))
Hljs.registerLanguage('python', require('highlight.js/lib/languages/python'))
Hljs.registerLanguage('javascript', require('highlight.js/lib/languages/javascript'))
Hljs.registerLanguage('typescript', require('highlight.js/lib/languages/typescript'))
enum WebViewEventAction {
Image = 'image',
Url = 'url'
}
export interface IMarkdownProps extends INavigationProps {
markdown: string | null // 内容
padding?: number // 边距
sanitize?: boolean // 是否清洗 HTML
indicator?: boolean // 是否使用渲染动画
style?: ViewStyle // 内容区样式
}
@observer
export class Markdown extends Component<IMarkdownProps> {
constructor(props: IMarkdownProps) {
super(props)
this.initMarked()
this.initRenderer()
}
private images: string[] = []
private renderer: marked.Renderer = new marked.Renderer()
@observable private htmlReadied: boolean = false
@observable private imageModalVisible: boolean = false
@observable private imageModalIndex: number = 0
@action
private updateHtmlReadied(readied: boolean) {
this.htmlReadied = readied
}
@action
private updateImageModalIndex(index: number) {
this.imageModalIndex = index
}
@action
private updateImageModalVisible(visible: boolean) {
this.imageModalVisible = visible
}
@computed
private get isIndicatorEnabled(): boolean {
const { indicator } = this.props
return indicator == null ? true : indicator
}
@computed
private get htmlContent(): string {
const { renderer, props } = this
const { markdown } = props
if (!markdown) {
return ''
}
return `<div id="content">${marked(markdown, {renderer})}</div>`
}
@computed
private get htmlStyle(): string {
const { padding } = this.props
return markdownStyles.ocean +
markdownStyles.content.styles +
`#content { padding: 0 ${padding || 0}px }`
}
@boundMethod
private handleWebViewEvent(event: any) {
const json = event.nativeEvent.data
const eventData = json ? JSON.parse(json) : null
if (eventData) {
// 图片弹窗
if (eventData.action === WebViewEventAction.Image) {
this.updateImageModalIndex(this.images.indexOf(eventData.data))
this.updateImageModalVisible(true)
}
// 打开链接
if (eventData.action === WebViewEventAction.Url) {
const url: string = eventData.data
const articleUrlPrefix = `${webUrl}/article/`
const isArticleUrl = url.startsWith(articleUrlPrefix)
if (isArticleUrl) {
const articleId = url.replace(articleUrlPrefix, '')
this.props.navigation.navigate({
key: articleId,
routeName: EHomeRoutes.ArticleDetail,
params: { articleId }
})
} else {
this.props.navigation.navigate(
EHomeRoutes.ArticleWebview,
{ url }
)
}
}
}
}
private initMarked() {
marked.setOptions({
renderer: new marked.Renderer(),
gfm: true,
tables: true,
breaks: false,
pedantic: false,
sanitize: this.props.sanitize == null ? true : this.props.sanitize,
smartLists: true,
smartypants: false,
highlight(code: string) {
return Hljs.highlightAuto(code).value
}
})
}
private initRenderer() {
this.renderer.link = this.linkRender
this.renderer.code = this.codeRender
this.renderer.image = this.imageRender
this.renderer.heading = this.headingRender
this.renderer.paragraph = this.paragraphRender
}
private htmlScript: string = `
;window.dispatchMessage = function(action, data) {
window.ReactNativeWebView.postMessage(JSON.stringify({ action, data }));
};
`
// 段落
private paragraphRender(text: string): string {
return `<p>${text}</p>`
}
// 标题
private headingRender(text: string, level: number, raw: string): string {
const id = raw.toLowerCase().replace(/[^a-zA-Z0-9\u4e00-\u9fa5]+/g, '-')
return `<h${level} id=${id}>${text}</h${level}>`
}
private linkRender(href: string, title: string, text: string): string {
return `
<a
href="${href}"
onclick="window.dispatchMessage('${WebViewEventAction.Url}', '${href}');return false;"
>${text}</a>`
}
@boundMethod
private imageRender(src: string, title: string, alt: string): string {
src = src.replace(/^http:\/\//ig, '/proxy/')
this.images.push(src)
return `
<img
src="${src}"
onclick="window.dispatchMessage('${WebViewEventAction.Image}', '${src}')"
/>`
}
// 代码解析器
private codeRender(code: string, lang: string, escaped: boolean): string {
const { options } = this as any
if (options.highlight) {
const out = options.highlight(code, lang)
if (out != null && out !== code) {
escaped = true
code = out
}
}
return `<pre><code>${(escaped ? code : escape(code))}\n</code></pre>`
}
render() {
return (
<View style={{ flex: 1 }}>
{!this.htmlReadied && this.isIndicatorEnabled && (
<View style={[{ flex: 1, zIndex: 1 }, StyleSheet.absoluteFill]}>
<AutoActivityIndicator
style={{ flex: 1 }}
text={i18n.t(LANGUAGE_KEYS.RENDERING)}
/>
</View>
)}
<AutoHeightWebView
style={{
flex: 1,
...this.props.style,
opacity: this.htmlReadied ? 1 : 0
}}
customScript={this.htmlScript}
customStyle={this.htmlStyle}
useWebKit={false}
scalesPageToFit={false}
scrollEnabled={false}
originWhitelist={['*']}
source={{ html: `<style>${this.htmlStyle}</style>` + this.htmlContent }}
onMessage={this.handleWebViewEvent}
onSizeUpdated={() => this.updateHtmlReadied(true)}
/>
<ImageViewerModal
images={this.images}
index={this.imageModalIndex}
visible={this.imageModalVisible}
onClose={() => this.updateImageModalVisible(false)}
/>
</View>
)
}
}