mirror of https://github.com/halo-dev/halo
Compatible with markdown file from hexo (#1492)
* hexo语法省略开头的---时无法解析元信息,兼容此种情况 * 修复导入markdown文件时固定条件下出现 [该标签已存在] 的异常 * hexo语法的博客在实际使用时可以省略开头的 --- 分隔符 : add unit test * fix code style Co-authored-by: zhanglei <zhanglei9588@foxmail.com>pull/1494/head
parent
de71f40de6
commit
8c26430b31
|
@ -31,7 +31,7 @@ public interface TagService extends CrudService<Tag, Integer> {
|
||||||
* @param slug slug
|
* @param slug slug
|
||||||
* @return tag
|
* @return tag
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@Nullable
|
||||||
Tag getBySlug(@NonNull String slug);
|
Tag getBySlug(@NonNull String slug);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -412,10 +412,14 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
||||||
tagName = StringUtils.strip(tagName, "\"");
|
tagName = StringUtils.strip(tagName, "\"");
|
||||||
tagName = StringUtils.strip(tagName, "\'");
|
tagName = StringUtils.strip(tagName, "\'");
|
||||||
tag = tagService.getByName(tagName);
|
tag = tagService.getByName(tagName);
|
||||||
|
String slug = SlugUtils.slug(tagName);
|
||||||
|
if (null == tag) {
|
||||||
|
tag = tagService.getBySlug(slug);
|
||||||
|
}
|
||||||
if (null == tag) {
|
if (null == tag) {
|
||||||
tag = new Tag();
|
tag = new Tag();
|
||||||
tag.setName(tagName);
|
tag.setName(tagName);
|
||||||
tag.setSlug(SlugUtils.slug(tagName));
|
tag.setSlug(slug);
|
||||||
tag = tagService.create(tag);
|
tag = tagService.create(tag);
|
||||||
}
|
}
|
||||||
tagIds.add(tag.getId());
|
tagIds.add(tag.getId());
|
||||||
|
|
|
@ -71,7 +71,7 @@ public class MarkdownUtils {
|
||||||
private static final Parser PARSER = Parser.builder(OPTIONS).build();
|
private static final Parser PARSER = Parser.builder(OPTIONS).build();
|
||||||
|
|
||||||
private static final HtmlRenderer RENDERER = HtmlRenderer.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.
|
// * Render html document to markdown document.
|
||||||
|
@ -136,6 +136,9 @@ public class MarkdownUtils {
|
||||||
return row;
|
return row;
|
||||||
}
|
}
|
||||||
}).collect(Collectors.joining("\n"));
|
}).collect(Collectors.joining("\n"));
|
||||||
|
if (!markdown.startsWith("---\n")) {
|
||||||
|
markdown = "---\n" + markdown;
|
||||||
|
}
|
||||||
AbstractYamlFrontMatterVisitor visitor = new AbstractYamlFrontMatterVisitor();
|
AbstractYamlFrontMatterVisitor visitor = new AbstractYamlFrontMatterVisitor();
|
||||||
Node document = PARSER.parse(markdown);
|
Node document = PARSER.parse(markdown);
|
||||||
visitor.visit(document);
|
visitor.visit(document);
|
||||||
|
|
|
@ -3,6 +3,8 @@ package run.halo.app.utils;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
@ -33,6 +35,15 @@ class MarkdownUtilsTest {
|
||||||
+ "---"
|
+ "---"
|
||||||
+ "test---";
|
+ "test---";
|
||||||
assertEquals("test---", MarkdownUtils.removeFrontMatter(markdown));
|
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
|
@Test
|
||||||
|
@ -70,4 +81,18 @@ class MarkdownUtilsTest {
|
||||||
+ "</section>\n";
|
+ "</section>\n";
|
||||||
assertTrue(StringUtils.equals(s2Expected, s2));
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue