File tree Expand file tree Collapse file tree 2 files changed +27
-9
lines changed
Expand file tree Collapse file tree 2 files changed +27
-9
lines changed Original file line number Diff line number Diff line change @@ -99,16 +99,17 @@ export const fetchTopicDetail = ({ id, accesstoken }) => dispatch => {
9999 data
100100 } )
101101
102- // highlight code and re-render
102+ // highlight code, add hash to user link and re-render
103103 const _data = Object . assign ( { } , data )
104104 const worker = new Worker ( )
105105 worker . onmessage = e => {
106- _data . content = e . data
106+ console . timeEnd ( 'worker' )
107107 dispatch ( {
108108 type : FETCH_TOPIC_DETAIL ,
109- data : _data
109+ data : e . data
110110 } )
111111 }
112- worker . postMessage ( { content : data . content } )
112+ console . time ( 'worker' )
113+ worker . postMessage ( _data )
113114 } )
114115}
Original file line number Diff line number Diff line change 11const hljs = require ( 'highlight.js' )
22const unescape = require ( 'unescape-alltypes-html' )
33
4- onmessage = e => {
5- const { content } = e . data
6- const codeRE = / < c o d e > ( [ \s \S ] * ?) < \/ c o d e > / gm
7- const highlighted = content . replace ( codeRE , ( ...args ) => {
4+ const codeRE = / < c o d e > ( [ \s \S ] * ?) < \/ c o d e > / gm
5+ const handleHighlight = raw => {
6+ return raw . replace ( codeRE , ( ...args ) => {
87 const raw = args [ 1 ]
98 const unescaped = unescape ( raw )
109 const { value } = hljs . highlight ( 'js' , unescaped )
1110 return `<code>${ value } </code>`
1211 } )
13- postMessage ( highlighted )
12+ }
13+
14+ const linksRE = / ( < a h r e f = " \/ u s e r ) / g
15+ const handleAddHash = raw => raw . replace ( linksRE , ( ...args ) => `<a href="#/user` )
16+
17+ const handler = raw => handleAddHash ( handleHighlight ( raw ) )
18+
19+ onmessage = e => {
20+ const { content, replies } = e . data
21+ const _content = handler ( content )
22+ const _replies = replies . map ( reply => {
23+ reply . content = handler ( reply . content )
24+ return reply
25+ } )
26+
27+ postMessage ( Object . assign ( { } , e . data , {
28+ content : _content ,
29+ replies : _replies
30+ } ) )
1431}
You can’t perform that action at this time.
0 commit comments