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

Compatible with markdown file from hexo #1492

Merged
merged 4 commits into from
Oct 8, 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
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);
ruibaby marked this conversation as resolved.
Show resolved Hide resolved
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));
}
}