Skip to content

Commit

Permalink
Repair multi-level category incorrect handle while importing markdown(h…
Browse files Browse the repository at this point in the history
…alo-dev#1380)

* 修复导入markdown时多级分类无法正确识别的问题

* 1.修复导入markdown对单行多tag的支持
2.添加导入markdown时对单引号的正确识别和处理
3.添加导入hexo的markdown的分类列表、标签列表无缩进的支持
4.完善导入markdown文档的测试用例
  • Loading branch information
linshenkx authored Jun 1, 2021
1 parent 638bd29 commit 730b77f
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 17 deletions.
47 changes: 32 additions & 15 deletions src/main/java/run/halo/app/service/impl/PostServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ public PostDetailVO importMarkdown(String markdown, String filename) {
for (String ele : elementValue) {
ele = StrUtil.strip(ele, "[", "]");
ele = StrUtil.strip(ele, "\"");
ele = StrUtil.strip(ele, "\'");
if ("".equals(ele)) {
continue;
}
Expand All @@ -407,25 +408,41 @@ public PostDetailVO importMarkdown(String markdown, String filename) {
post.setDisallowComment(Boolean.parseBoolean(ele));
break;
case "tags":
Tag tag = tagService.getByName(ele);
if (null == tag) {
tag = new Tag();
tag.setName(ele);
tag.setSlug(SlugUtils.slug(ele));
tag = tagService.create(tag);
Tag tag;
for (String tagName : ele.split(",")) {
tagName = tagName.trim();
tagName = StrUtil.strip(tagName, "\"");
tagName = StrUtil.strip(tagName, "\'");
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;
case "categories":
Category category = categoryService.getByName(ele);
if (null == category) {
category = new Category();
category.setName(ele);
category.setSlug(SlugUtils.slug(ele));
category.setDescription(ele);
category = categoryService.create(category);
Integer lastCategoryId = null;
for (String categoryName : ele.split(",")) {
categoryName = categoryName.trim();
categoryName = StrUtil.strip(categoryName, "\"");
categoryName = StrUtil.strip(categoryName, "\'");
Category category = categoryService.getByName(categoryName);
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;
default:
break;
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/run/halo/app/utils/MarkdownUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import run.halo.app.model.support.HaloConst;

Expand Down Expand Up @@ -120,6 +121,18 @@ public static String renderHtml(String markdown) {
* @return Map
*/
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();
Node document = PARSER.parse(markdown);
visitor.visit(document);
Expand Down
38 changes: 36 additions & 2 deletions src/test/java/run/halo/app/service/impl/PostServiceImplTest.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
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 org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
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 run.halo.app.model.dto.CategoryDTO;
import run.halo.app.model.vo.PostDetailVO;

@SpringBootTest
@ActiveProfiles("test")
@Slf4j
@Disabled("Due to spring boot context needed")
class PostServiceImplTest {

String standardMdContent = "---\n"
+ "title: springfox-swagger2配置成功但无法访问/swagger-ui.html\n"
+ "categories: \n"
+ " - [后端,JAVA]\n"
+ " - [Spring]\n"
+ "tags:\n"
+ " - spring boot\n"
+ " - swagger\n"
Expand All @@ -26,6 +40,9 @@ class PostServiceImplTest {

String nonStandardMdContent = "---\n"
+ "title: Basic concepts of JPA\n"
+ "categories: \n"
+ "- [后端,JAVA]\n"
+ "- [Spring]\n"
+ "date: 2018-08-03 11:57:00\n"
+ "tags: ['spring', 'jpa', 'database', 'concept']\n"
+ "---\n"
Expand All @@ -44,7 +61,24 @@ void getContent() {
@Test
@Transactional
void markdownImportTest() {
postService.importMarkdown(standardMdContent, "standard");
postService.importMarkdown(nonStandardMdContent, "nonStandard");
PostDetailVO standardPost = postService.importMarkdown(standardMdContent, "standard");
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);
}
}

0 comments on commit 730b77f

Please sign in to comment.