mirror of https://github.com/halo-dev/halo
* chore: build.gradle add mavenLocal()(#975) * feat: markdown import compatible with hugo(#975)pull/983/head
parent
46000fd0d0
commit
91a54a4bb3
|
@ -15,6 +15,7 @@ java {
|
||||||
}
|
}
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
|
mavenLocal()
|
||||||
maven {
|
maven {
|
||||||
url "https://maven.aliyun.com/nexus/content/groups/public"
|
url "https://maven.aliyun.com/nexus/content/groups/public"
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package run.halo.app.service.impl;
|
package run.halo.app.service.impl;
|
||||||
|
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.context.ApplicationEventPublisher;
|
import org.springframework.context.ApplicationEventPublisher;
|
||||||
|
@ -318,8 +319,11 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
||||||
|
|
||||||
// Gets frontMatter
|
// Gets frontMatter
|
||||||
Map<String, List<String>> frontMatter = MarkdownUtils.getFrontMatter(markdown);
|
Map<String, List<String>> frontMatter = MarkdownUtils.getFrontMatter(markdown);
|
||||||
|
// remove frontMatter
|
||||||
|
markdown = MarkdownUtils.removeFrontMatter(markdown);
|
||||||
|
|
||||||
PostParam post = new PostParam();
|
PostParam post = new PostParam();
|
||||||
|
post.setStatus(null);
|
||||||
|
|
||||||
List<String> elementValue;
|
List<String> elementValue;
|
||||||
|
|
||||||
|
@ -331,6 +335,11 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
||||||
for (String key : frontMatter.keySet()) {
|
for (String key : frontMatter.keySet()) {
|
||||||
elementValue = frontMatter.get(key);
|
elementValue = frontMatter.get(key);
|
||||||
for (String ele : elementValue) {
|
for (String ele : elementValue) {
|
||||||
|
ele = StrUtil.strip(ele, "[", "]");
|
||||||
|
ele = StrUtil.strip(ele, "\"");
|
||||||
|
if ("".equals(ele)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
switch (key) {
|
switch (key) {
|
||||||
case "title":
|
case "title":
|
||||||
post.setTitle(ele);
|
post.setTitle(ele);
|
||||||
|
|
|
@ -27,6 +27,8 @@ import run.halo.app.model.support.HaloConst;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Markdown utils.
|
* Markdown utils.
|
||||||
|
@ -122,4 +124,21 @@ public class MarkdownUtils {
|
||||||
visitor.visit(document);
|
visitor.visit(document);
|
||||||
return visitor.getData();
|
return visitor.getData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final Pattern FRONT_MATTER = Pattern.compile("^---[\\s\\S]*?---");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* remove front matter
|
||||||
|
*
|
||||||
|
* @param markdown markdown
|
||||||
|
* @return markdown
|
||||||
|
*/
|
||||||
|
public static String removeFrontMatter(String markdown) {
|
||||||
|
markdown = markdown.trim();
|
||||||
|
Matcher matcher = FRONT_MATTER.matcher(markdown);
|
||||||
|
if (matcher.find()) {
|
||||||
|
return markdown.replace(matcher.group(), "");
|
||||||
|
}
|
||||||
|
return markdown;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
package run.halo.app.utils;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.Assert;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhixiang.yuan
|
||||||
|
* @since 2020/07/19 20:22:58
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
class MarkdownUtilsTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void removeFrontMatter() {
|
||||||
|
String markdown = "---\n" +
|
||||||
|
"title: \"test remove\"\n" +
|
||||||
|
"---";
|
||||||
|
Assert.isTrue("".equals(MarkdownUtils.removeFrontMatter(markdown)));
|
||||||
|
|
||||||
|
markdown = "---\n" +
|
||||||
|
"title: \"test remove\"\n" +
|
||||||
|
"---" +
|
||||||
|
"test";
|
||||||
|
Assert.isTrue("test".equals(MarkdownUtils.removeFrontMatter(markdown)));
|
||||||
|
|
||||||
|
markdown = "---\n" +
|
||||||
|
"title: \"test remove\"\n" +
|
||||||
|
"---" +
|
||||||
|
"test---";
|
||||||
|
Assert.isTrue("test---".equals(MarkdownUtils.removeFrontMatter(markdown)));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue