十一、从零模拟新浪微博-艾特和回复
代码仓库:https://github.com/changeclass/koa2-weibo
艾特功能
艾特功能使用at.js库。
实现接口
// 获取 at 列表
router.get('/getAtList', loginCheck, async (ctx, next) => {
const { id: userId } = ctx.session.userInfo
const result = await getFollowers(userId)
const { followersList } = result.data
const list = followersList.map((user) => {
return `${user.nickName}-${user.userName}`
})
ctx.body = list
})艾特用户转为链接
在格式化微博时将@符号转为链接
/**
* 格式化微博内容
* @param {Object} obj 微博数据对象
*/
function _formatContent(obj) {
obj.contentFormat = obj.content
// 格式化 @
// from '哈喽 @张三 - zhangsan 你好'
// to '哈喽 <a href="/profile/zhangsan">张三</a> 你好'
obj.contentFormat = obj.contentFormat.replace(
REG_FOR_AT_WHO,
(matchStr, nickName, userName) => {
return `<a href="/profile/${userName}">@${nickName}</a>`
}
)
return obj
}正则表达式
/@(.+?)\s-\s(\w+?)\b/g
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 小康博客!
评论
TwikooDisqusjs









