Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}