Skip to content

Commit 4e4117a

Browse files
committed
feat: add hash before each user link in the topic detail
1 parent 40762d2 commit 4e4117a

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

src/actions/topics.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff 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
}

src/hl.worker.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
11
const hljs = require('highlight.js')
22
const unescape = require('unescape-alltypes-html')
33

4-
onmessage = e => {
5-
const { content } = e.data
6-
const codeRE = /<code>([\s\S]*?)<\/code>/gm
7-
const highlighted = content.replace(codeRE, (...args) => {
4+
const codeRE = /<code>([\s\S]*?)<\/code>/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 href="\/user)/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
}

0 commit comments

Comments
 (0)