diff --git a/src/main/java/run/halo/app/service/TagService.java b/src/main/java/run/halo/app/service/TagService.java index 1805d87abd..14e88d7c63 100644 --- a/src/main/java/run/halo/app/service/TagService.java +++ b/src/main/java/run/halo/app/service/TagService.java @@ -31,7 +31,7 @@ public interface TagService extends CrudService { * @param slug slug * @return tag */ - @NonNull + @Nullable Tag getBySlug(@NonNull String slug); /** diff --git a/src/main/java/run/halo/app/service/impl/PostServiceImpl.java b/src/main/java/run/halo/app/service/impl/PostServiceImpl.java index 4eeb351a74..f432f0b508 100644 --- a/src/main/java/run/halo/app/service/impl/PostServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/PostServiceImpl.java @@ -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()); diff --git a/src/main/java/run/halo/app/utils/MarkdownUtils.java b/src/main/java/run/halo/app/utils/MarkdownUtils.java index 349a338b8b..78389f6c79 100644 --- a/src/main/java/run/halo/app/utils/MarkdownUtils.java +++ b/src/main/java/run/halo/app/utils/MarkdownUtils.java @@ -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. @@ -136,6 +136,9 @@ public static Map> 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); diff --git a/src/test/java/run/halo/app/utils/MarkdownUtilsTest.java b/src/test/java/run/halo/app/utils/MarkdownUtilsTest.java index c42533be19..fb2ffde163 100644 --- a/src/test/java/run/halo/app/utils/MarkdownUtilsTest.java +++ b/src/test/java/run/halo/app/utils/MarkdownUtilsTest.java @@ -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; @@ -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 @@ -70,4 +81,18 @@ void footNotesTest() { + "\n"; assertTrue(StringUtils.equals(s2Expected, s2)); } + + @Test + void getFrontMatter() { + String markdown = "---\n" + + "title: \"test remove\"\n" + + "---"; + Map> 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)); + } } \ No newline at end of file