Commit d8563272 by 段启岩

动态加html标签

parent dbc0e054
......@@ -112,6 +112,7 @@ public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IP
User user = userService.getById(post.getUserId());
post.setUserNick(user.getNickName());
post.setUserAvatar(user.getUserAvatar());
post.setContent(TopicUtils.encodeTopic(post.getContent()));
//2.保存动态
save(post);
......
package cn.meteor.beyondclouds.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.common.protocol.types.Field;
import org.springframework.util.Assert;
import java.util.ArrayList;
......@@ -19,6 +20,20 @@ public class TopicUtils {
private static final Pattern topicPattern = Pattern.compile("(#([^#]+?)#)");
public static String encodeTopic(String str) {
Assert.hasText(str, "str must not be empty");
Matcher matcher = topicPattern.matcher(str);
StringBuffer buffer = new StringBuffer();
while (matcher.find()) {
String tmp = "<a href='/topic/detail/" + matcher.group(2) + "'>" + matcher.group(1) + "</a>";
matcher.appendReplacement(buffer, tmp);
}
matcher.appendTail(buffer);
return buffer.toString();
}
public static List<String> parseTopics(String str) {
Assert.hasText(str, "str must not be empty");
......@@ -37,6 +52,7 @@ public class TopicUtils {
public static void main(String[] args) {
System.out.println(TopicUtils.parseTopics("#哈哈哈#今#哈哈哈#天你吃饭了吗#吃了#######"));
System.out.println(TopicUtils.encodeTopic("#哈哈哈#今#哈哈哈#天你吃饭了吗#吃了#######哈哈#a#b"));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment