mirror of https://github.com/halo-dev/halo
Repair multi-level category incorrect handle while importing markdown(#1380)
* 修复导入markdown时多级分类无法正确识别的问题 * 1.修复导入markdown对单行多tag的支持 2.添加导入markdown时对单引号的正确识别和处理 3.添加导入hexo的markdown的分类列表、标签列表无缩进的支持 4.完善导入markdown文档的测试用例pull/1396/head
parent
0ade01c518
commit
9de40a0a1c
|
@ -384,6 +384,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
||||||
for (String ele : elementValue) {
|
for (String ele : elementValue) {
|
||||||
ele = StrUtil.strip(ele, "[", "]");
|
ele = StrUtil.strip(ele, "[", "]");
|
||||||
ele = StrUtil.strip(ele, "\"");
|
ele = StrUtil.strip(ele, "\"");
|
||||||
|
ele = StrUtil.strip(ele, "\'");
|
||||||
if ("".equals(ele)) {
|
if ("".equals(ele)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -407,25 +408,41 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
||||||
post.setDisallowComment(Boolean.parseBoolean(ele));
|
post.setDisallowComment(Boolean.parseBoolean(ele));
|
||||||
break;
|
break;
|
||||||
case "tags":
|
case "tags":
|
||||||
Tag tag = tagService.getByName(ele);
|
Tag tag;
|
||||||
if (null == tag) {
|
for (String tagName : ele.split(",")) {
|
||||||
tag = new Tag();
|
tagName = tagName.trim();
|
||||||
tag.setName(ele);
|
tagName = StrUtil.strip(tagName, "\"");
|
||||||
tag.setSlug(SlugUtils.slug(ele));
|
tagName = StrUtil.strip(tagName, "\'");
|
||||||
tag = tagService.create(tag);
|
tag = tagService.getByName(tagName);
|
||||||
|
if (null == tag) {
|
||||||
|
tag = new Tag();
|
||||||
|
tag.setName(tagName);
|
||||||
|
tag.setSlug(SlugUtils.slug(tagName));
|
||||||
|
tag = tagService.create(tag);
|
||||||
|
}
|
||||||
|
tagIds.add(tag.getId());
|
||||||
}
|
}
|
||||||
tagIds.add(tag.getId());
|
|
||||||
break;
|
break;
|
||||||
case "categories":
|
case "categories":
|
||||||
Category category = categoryService.getByName(ele);
|
Integer lastCategoryId = null;
|
||||||
if (null == category) {
|
for (String categoryName : ele.split(",")) {
|
||||||
category = new Category();
|
categoryName = categoryName.trim();
|
||||||
category.setName(ele);
|
categoryName = StrUtil.strip(categoryName, "\"");
|
||||||
category.setSlug(SlugUtils.slug(ele));
|
categoryName = StrUtil.strip(categoryName, "\'");
|
||||||
category.setDescription(ele);
|
Category category = categoryService.getByName(categoryName);
|
||||||
category = categoryService.create(category);
|
if (null == category) {
|
||||||
|
category = new Category();
|
||||||
|
category.setName(categoryName);
|
||||||
|
category.setSlug(SlugUtils.slug(categoryName));
|
||||||
|
category.setDescription(categoryName);
|
||||||
|
if (lastCategoryId != null) {
|
||||||
|
category.setParentId(lastCategoryId);
|
||||||
|
}
|
||||||
|
category = categoryService.create(category);
|
||||||
|
}
|
||||||
|
lastCategoryId = category.getId();
|
||||||
|
categoryIds.add(lastCategoryId);
|
||||||
}
|
}
|
||||||
categoryIds.add(category.getId());
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -26,6 +26,7 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import run.halo.app.model.support.HaloConst;
|
import run.halo.app.model.support.HaloConst;
|
||||||
|
|
||||||
|
@ -120,6 +121,18 @@ public class MarkdownUtils {
|
||||||
* @return Map
|
* @return Map
|
||||||
*/
|
*/
|
||||||
public static Map<String, List<String>> getFrontMatter(String markdown) {
|
public static Map<String, List<String>> getFrontMatter(String markdown) {
|
||||||
|
markdown = markdown.trim();
|
||||||
|
Matcher matcher = FRONT_MATTER.matcher(markdown);
|
||||||
|
if (matcher.find()) {
|
||||||
|
markdown = matcher.group();
|
||||||
|
}
|
||||||
|
markdown = Arrays.stream(markdown.split("\\r?\\n")).map(row -> {
|
||||||
|
if (row.startsWith("- ")) {
|
||||||
|
return " " + row;
|
||||||
|
} else {
|
||||||
|
return row;
|
||||||
|
}
|
||||||
|
}).collect(Collectors.joining("\n"));
|
||||||
AbstractYamlFrontMatterVisitor visitor = new AbstractYamlFrontMatterVisitor();
|
AbstractYamlFrontMatterVisitor visitor = new AbstractYamlFrontMatterVisitor();
|
||||||
Node document = PARSER.parse(markdown);
|
Node document = PARSER.parse(markdown);
|
||||||
visitor.visit(document);
|
visitor.visit(document);
|
||||||
|
|
|
@ -1,17 +1,31 @@
|
||||||
package run.halo.app.service.impl;
|
package run.halo.app.service.impl;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.junit.jupiter.api.Disabled;
|
import org.junit.jupiter.api.Disabled;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import run.halo.app.model.dto.CategoryDTO;
|
||||||
|
import run.halo.app.model.vo.PostDetailVO;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
@ActiveProfiles("test")
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Disabled("Due to spring boot context needed")
|
@Disabled("Due to spring boot context needed")
|
||||||
class PostServiceImplTest {
|
class PostServiceImplTest {
|
||||||
|
|
||||||
String standardMdContent = "---\n"
|
String standardMdContent = "---\n"
|
||||||
+ "title: springfox-swagger2配置成功但无法访问/swagger-ui.html\n"
|
+ "title: springfox-swagger2配置成功但无法访问/swagger-ui.html\n"
|
||||||
|
+ "categories: \n"
|
||||||
|
+ " - [后端,JAVA]\n"
|
||||||
|
+ " - [Spring]\n"
|
||||||
+ "tags:\n"
|
+ "tags:\n"
|
||||||
+ " - spring boot\n"
|
+ " - spring boot\n"
|
||||||
+ " - swagger\n"
|
+ " - swagger\n"
|
||||||
|
@ -26,6 +40,9 @@ class PostServiceImplTest {
|
||||||
|
|
||||||
String nonStandardMdContent = "---\n"
|
String nonStandardMdContent = "---\n"
|
||||||
+ "title: Basic concepts of JPA\n"
|
+ "title: Basic concepts of JPA\n"
|
||||||
|
+ "categories: \n"
|
||||||
|
+ "- [后端,JAVA]\n"
|
||||||
|
+ "- [Spring]\n"
|
||||||
+ "date: 2018-08-03 11:57:00\n"
|
+ "date: 2018-08-03 11:57:00\n"
|
||||||
+ "tags: ['spring', 'jpa', 'database', 'concept']\n"
|
+ "tags: ['spring', 'jpa', 'database', 'concept']\n"
|
||||||
+ "---\n"
|
+ "---\n"
|
||||||
|
@ -44,7 +61,24 @@ class PostServiceImplTest {
|
||||||
@Test
|
@Test
|
||||||
@Transactional
|
@Transactional
|
||||||
void markdownImportTest() {
|
void markdownImportTest() {
|
||||||
postService.importMarkdown(standardMdContent, "standard");
|
PostDetailVO standardPost = postService.importMarkdown(standardMdContent, "standard");
|
||||||
postService.importMarkdown(nonStandardMdContent, "nonStandard");
|
Map<String, CategoryDTO> standardCategoryMap = standardPost.getCategories().stream()
|
||||||
|
.collect(Collectors.toMap(CategoryDTO::getName, post -> post));
|
||||||
|
assertTrue(standardCategoryMap.containsKey("后端"));
|
||||||
|
assertTrue(standardCategoryMap.containsKey("JAVA"));
|
||||||
|
assertTrue(standardCategoryMap.containsKey("Spring"));
|
||||||
|
assertTrue(standardCategoryMap.get("后端").getId()
|
||||||
|
.equals(standardCategoryMap.get("JAVA").getParentId()));
|
||||||
|
assertEquals(standardPost.getTags().size(), 3);
|
||||||
|
PostDetailVO nonStandardPost =
|
||||||
|
postService.importMarkdown(nonStandardMdContent, "nonStandard");
|
||||||
|
Map<String, CategoryDTO> nonStandardCategoryMap = nonStandardPost.getCategories().stream()
|
||||||
|
.collect(Collectors.toMap(CategoryDTO::getName, post -> post));
|
||||||
|
assertTrue(nonStandardCategoryMap.containsKey("后端"));
|
||||||
|
assertTrue(nonStandardCategoryMap.containsKey("JAVA"));
|
||||||
|
assertTrue(nonStandardCategoryMap.containsKey("Spring"));
|
||||||
|
assertTrue(nonStandardCategoryMap.get("后端").getId()
|
||||||
|
.equals(nonStandardCategoryMap.get("JAVA").getParentId()));
|
||||||
|
assertEquals(nonStandardPost.getTags().size(), 4);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue