Skip to content

Commit

Permalink
Compatible with markdown file from hexo (#1492)
Browse files Browse the repository at this point in the history
* hexo语法省略开头的---时无法解析元信息,兼容此种情况

* 修复导入markdown文件时固定条件下出现 [该标签已存在] 的异常

* hexo语法的博客在实际使用时可以省略开头的 --- 分隔符 : add unit test

* fix code style

Co-authored-by: zhanglei <[email protected]>
  • Loading branch information
lrzl and zhanglei authored Oct 8, 2021
1 parent de71f40 commit 8c26430
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main/java/run/halo/app/service/TagService.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public interface TagService extends CrudService<Tag, Integer> {
* @param slug slug
* @return tag
*/
@NonNull
@Nullable
Tag getBySlug(@NonNull String slug);

/**
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/run/halo/app/service/impl/PostServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,14 @@ public PostDetailVO importMarkdown(String markdown, String filename) {
tagName = StringUtils.strip(tagName, "\"");
tagName = StringUtils.strip(tagName, "\'");
tag = tagService.getByName(tagName);
String slug = SlugUtils.slug(tagName);
if (null == tag) {
tag = tagService.getBySlug(slug);
}
if (null == tag) {
tag = new Tag();
tag.setName(tagName);
tag.setSlug(SlugUtils.slug(tagName));
tag.setSlug(slug);
tag = tagService.create(tag);
}
tagIds.add(tag.getId());
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/run/halo/app/utils/MarkdownUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class MarkdownUtils {
private static final Parser PARSER = Parser.builder(OPTIONS).build();

private static final HtmlRenderer RENDERER = HtmlRenderer.builder(OPTIONS).build();
private static final Pattern FRONT_MATTER = Pattern.compile("^---[\\s\\S]*?---");
private static final Pattern FRONT_MATTER = Pattern.compile("^(---)?[\\s\\S]*?---");

// /**
// * Render html document to markdown document.
Expand Down Expand Up @@ -136,6 +136,9 @@ public static Map<String, List<String>> getFrontMatter(String markdown) {
return row;
}
}).collect(Collectors.joining("\n"));
if (!markdown.startsWith("---\n")) {
markdown = "---\n" + markdown;
}
AbstractYamlFrontMatterVisitor visitor = new AbstractYamlFrontMatterVisitor();
Node document = PARSER.parse(markdown);
visitor.visit(document);
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/run/halo/app/utils/MarkdownUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -33,6 +35,15 @@ void removeFrontMatter() {
+ "---"
+ "test---";
assertEquals("test---", MarkdownUtils.removeFrontMatter(markdown));

markdown = "title: \"test remove\"\n"
+ "test---";
assertEquals("", MarkdownUtils.removeFrontMatter(markdown));

markdown = "title: \"test remove\"\n"
+ "---"
+ "test---";
assertEquals("test---", MarkdownUtils.removeFrontMatter(markdown));
}

@Test
Expand Down Expand Up @@ -70,4 +81,18 @@ void footNotesTest() {
+ "</section>\n";
assertTrue(StringUtils.equals(s2Expected, s2));
}

@Test
void getFrontMatter() {
String markdown = "---\n"
+ "title: \"test remove\"\n"
+ "---";
Map<String, List<String>> frontMatter = MarkdownUtils.getFrontMatter(markdown);
assertEquals("\"test remove\"", frontMatter.get("title").get(0));

markdown = "title: \"test remove\"\n"
+ "---";
frontMatter = MarkdownUtils.getFrontMatter(markdown);
assertEquals("\"test remove\"", frontMatter.get("title").get(0));
}
}

0 comments on commit 8c26430

Please sign in to comment.