mirror of https://github.com/halo-dev/halo
refactor: modify the frontMatter export format to yaml syntax (#1813)
* fix: markdown import multi tag/category parsing problem * fix: markdown import multi tag/category parsing problempull/1820/head
parent
90d1bce9b9
commit
5e2f6e2351
|
@ -652,6 +652,39 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
||||||
private PostMarkdownVO convertToPostMarkdownVo(Post post) {
|
private PostMarkdownVO convertToPostMarkdownVo(Post post) {
|
||||||
PostMarkdownVO postMarkdownVO = new PostMarkdownVO();
|
PostMarkdownVO postMarkdownVO = new PostMarkdownVO();
|
||||||
|
|
||||||
|
// set frontMatter
|
||||||
|
StringBuilder frontMatter = getFrontMatterYaml(post);
|
||||||
|
postMarkdownVO.setFrontMatter(frontMatter.toString());
|
||||||
|
|
||||||
|
// set content
|
||||||
|
PatchedContent postContent = post.getContent();
|
||||||
|
postMarkdownVO.setOriginalContent(postContent.getOriginalContent());
|
||||||
|
postMarkdownVO.setTitle(post.getTitle());
|
||||||
|
postMarkdownVO.setSlug(post.getSlug());
|
||||||
|
return postMarkdownVO;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* frontMatter has a variety of parsing methods, but the most commonly used is the yaml type.
|
||||||
|
* yaml is used here, and public methods can be extracted if needed for extensions.
|
||||||
|
* <p>
|
||||||
|
* Example: <br>
|
||||||
|
* title: default title. <br>
|
||||||
|
* date: 2022-04-03 19:00:00.000 <br>
|
||||||
|
* updated: 2022-04-03 19:00:00.000 <br>
|
||||||
|
* description: default description <br>
|
||||||
|
* categories: <br>
|
||||||
|
* - Java <br>
|
||||||
|
* - Halo <br>
|
||||||
|
* tags: <br>
|
||||||
|
* - tag <br>
|
||||||
|
* - doc <br>
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param post post not be null
|
||||||
|
* @return frontMatter
|
||||||
|
*/
|
||||||
|
private StringBuilder getFrontMatterYaml(Post post) {
|
||||||
StringBuilder frontMatter = new StringBuilder("---\n");
|
StringBuilder frontMatter = new StringBuilder("---\n");
|
||||||
frontMatter.append("title: ").append(post.getTitle()).append("\n");
|
frontMatter.append("title: ").append(post.getTitle()).append("\n");
|
||||||
frontMatter.append("date: ").append(post.getCreateTime()).append("\n");
|
frontMatter.append("date: ").append(post.getCreateTime()).append("\n");
|
||||||
|
@ -661,39 +694,20 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
||||||
frontMatter.append("url: ").append(postAssembler.buildFullPath(post)).append("\n");
|
frontMatter.append("url: ").append(postAssembler.buildFullPath(post)).append("\n");
|
||||||
|
|
||||||
// set category
|
// set category
|
||||||
|
// classification with hierarchies has not been processed yet
|
||||||
List<Category> categories = postCategoryService.listCategoriesBy(post.getId());
|
List<Category> categories = postCategoryService.listCategoriesBy(post.getId());
|
||||||
StringBuilder categoryContent = new StringBuilder();
|
StringBuilder categoryContent = new StringBuilder();
|
||||||
for (int i = 0; i < categories.size(); i++) {
|
categories.forEach(category -> categoryContent.append("- ").append(category.getName())
|
||||||
Category category = categories.get(i);
|
.append("\n"));
|
||||||
String categoryName = category.getName();
|
frontMatter.append("categories: ").append("\n").append(categoryContent);
|
||||||
if (i == 0) {
|
|
||||||
categoryContent.append(categoryName);
|
|
||||||
} else {
|
|
||||||
categoryContent.append(" | ").append(categoryName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
frontMatter.append("categories: ").append(categoryContent.toString()).append("\n");
|
|
||||||
|
|
||||||
// set tags
|
// set tags
|
||||||
List<Tag> tags = postTagService.listTagsBy(post.getId());
|
List<Tag> tags = postTagService.listTagsBy(post.getId());
|
||||||
StringBuilder tagContent = new StringBuilder();
|
StringBuilder tagContent = new StringBuilder();
|
||||||
for (int i = 0; i < tags.size(); i++) {
|
tags.forEach(tag -> tagContent.append("- ").append(tag.getName()).append("\n"));
|
||||||
Tag tag = tags.get(i);
|
frontMatter.append("tags: ").append("\n").append(tagContent);
|
||||||
String tagName = tag.getName();
|
|
||||||
if (i == 0) {
|
|
||||||
tagContent.append(tagName);
|
|
||||||
} else {
|
|
||||||
tagContent.append(" | ").append(tagName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
frontMatter.append("tags: ").append(tagContent).append("\n");
|
|
||||||
|
|
||||||
frontMatter.append("---\n");
|
frontMatter.append("---\n");
|
||||||
postMarkdownVO.setFrontMatter(frontMatter.toString());
|
return frontMatter;
|
||||||
PatchedContent postContent = post.getContent();
|
|
||||||
postMarkdownVO.setOriginalContent(postContent.getOriginalContent());
|
|
||||||
postMarkdownVO.setTitle(post.getTitle());
|
|
||||||
postMarkdownVO.setSlug(post.getSlug());
|
|
||||||
return postMarkdownVO;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -83,7 +83,7 @@ class MarkdownUtilsTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getFrontMatter() {
|
void getFrontMatterYaml() {
|
||||||
String markdown = "---\n"
|
String markdown = "---\n"
|
||||||
+ "title: \"test remove\"\n"
|
+ "title: \"test remove\"\n"
|
||||||
+ "---";
|
+ "---";
|
||||||
|
@ -94,5 +94,20 @@ class MarkdownUtilsTest {
|
||||||
+ "---";
|
+ "---";
|
||||||
frontMatter = MarkdownUtils.getFrontMatter(markdown);
|
frontMatter = MarkdownUtils.getFrontMatter(markdown);
|
||||||
assertEquals("\"test remove\"", frontMatter.get("title").get(0));
|
assertEquals("\"test remove\"", frontMatter.get("title").get(0));
|
||||||
|
|
||||||
|
// halo
|
||||||
|
markdown = "---\n"
|
||||||
|
+ "title: Hello Halo\n"
|
||||||
|
+ "date: 2021-12-04 17:27:26.114\n"
|
||||||
|
+ "updated: 2021-12-04 17:27:26.114\n"
|
||||||
|
+ "url: /archives/hello-halo\n"
|
||||||
|
+ "categories: \n"
|
||||||
|
+ "- default category\n"
|
||||||
|
+ "- multi category\n"
|
||||||
|
+ "tags: \n"
|
||||||
|
+ "---\n";
|
||||||
|
frontMatter = MarkdownUtils.getFrontMatter(markdown);
|
||||||
|
assertEquals("default category", frontMatter.get("categories").get(0));
|
||||||
|
assertEquals("multi category", frontMatter.get("categories").get(1));
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue