mirror of https://github.com/halo-dev/halo
Support post and sheet export.
parent
129cafd818
commit
93f146c2c1
|
@ -106,6 +106,24 @@ public interface PostService extends BasePostService<Post> {
|
|||
@NonNull
|
||||
Post importMarkdown(@NonNull String markdown);
|
||||
|
||||
/**
|
||||
* Export post to markdown file by post id.
|
||||
*
|
||||
* @param id post id
|
||||
* @return markdown file content
|
||||
*/
|
||||
@NonNull
|
||||
String exportMarkdown(@NonNull Integer id);
|
||||
|
||||
/**
|
||||
* Export post to markdown file by post.
|
||||
*
|
||||
* @param post current post
|
||||
* @return markdown file content
|
||||
*/
|
||||
@NonNull
|
||||
String exportMarkdown(@NonNull Post post);
|
||||
|
||||
/**
|
||||
* Converts to detail vo.
|
||||
*
|
||||
|
|
|
@ -54,6 +54,24 @@ public interface SheetService extends BasePostService<Sheet> {
|
|||
@NonNull
|
||||
Sheet importMarkdown(@NonNull String markdown);
|
||||
|
||||
/**
|
||||
* Export sheet to markdown file by sheet id.
|
||||
*
|
||||
* @param id sheet id
|
||||
* @return markdown file content
|
||||
*/
|
||||
@NonNull
|
||||
String exportMarkdown(@NonNull Integer id);
|
||||
|
||||
/**
|
||||
* Export sheet to markdown file by sheet.
|
||||
*
|
||||
* @param sheet current sheet
|
||||
* @return markdown file content
|
||||
*/
|
||||
@NonNull
|
||||
String exportMarkdown(@NonNull Sheet sheet);
|
||||
|
||||
/**
|
||||
* Converts to list dto page.
|
||||
*
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package run.halo.app.service.impl;
|
||||
|
||||
import cn.hutool.core.text.StrBuilder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
|
@ -286,6 +287,51 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String exportMarkdown(Integer id) {
|
||||
Assert.notNull(id, "Post id must not be null");
|
||||
Post post = getById(id);
|
||||
return exportMarkdown(post);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String exportMarkdown(Post post) {
|
||||
Assert.notNull(post, "Post must not be null");
|
||||
|
||||
StrBuilder content = new StrBuilder("---\n");
|
||||
|
||||
content.append("type: ").append("post").append("\n");
|
||||
content.append("title: ").append(post.getTitle()).append("\n");
|
||||
content.append("permalink: ").append(post.getUrl()).append("\n");
|
||||
content.append("thumbnail: ").append(post.getThumbnail()).append("\n");
|
||||
content.append("status: ").append(post.getStatus()).append("\n");
|
||||
content.append("date: ").append(post.getCreateTime()).append("\n");
|
||||
content.append("updated: ").append(post.getEditTime()).append("\n");
|
||||
content.append("comments: ").append(!post.getDisallowComment()).append("\n");
|
||||
|
||||
List<Tag> tags = postTagService.listTagsBy(post.getId());
|
||||
|
||||
if (tags.size() > 0) {
|
||||
content.append("tags:").append("\n");
|
||||
for (Tag tag : tags) {
|
||||
content.append(" - ").append(tag.getName()).append("\n");
|
||||
}
|
||||
}
|
||||
|
||||
List<Category> categories = postCategoryService.listCategoryBy(post.getId());
|
||||
|
||||
if (categories.size() > 0) {
|
||||
content.append("categories:").append("\n");
|
||||
for (Category category : categories) {
|
||||
content.append(" - ").append(category.getName()).append("\n");
|
||||
}
|
||||
}
|
||||
|
||||
content.append("---\n\n");
|
||||
content.append(post.getOriginalContent());
|
||||
return content.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PostDetailVO convertToDetailVo(Post post) {
|
||||
return convertTo(post,
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package run.halo.app.service.impl;
|
||||
|
||||
import cn.hutool.core.text.StrBuilder;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
@ -108,6 +109,33 @@ public class SheetServiceImpl extends BasePostServiceImpl<Sheet> implements Shee
|
|||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String exportMarkdown(Integer id) {
|
||||
Assert.notNull(id, "sheet id must not be null");
|
||||
Sheet sheet = getById(id);
|
||||
return exportMarkdown(sheet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String exportMarkdown(Sheet sheet) {
|
||||
Assert.notNull(sheet, "Sheet must not be null");
|
||||
|
||||
StrBuilder content = new StrBuilder("---\n");
|
||||
|
||||
content.append("type: ").append("sheet").append("\n");
|
||||
content.append("title: ").append(sheet.getTitle()).append("\n");
|
||||
content.append("permalink: ").append(sheet.getUrl()).append("\n");
|
||||
content.append("thumbnail: ").append(sheet.getThumbnail()).append("\n");
|
||||
content.append("status: ").append(sheet.getStatus()).append("\n");
|
||||
content.append("date: ").append(sheet.getCreateTime()).append("\n");
|
||||
content.append("updated: ").append(sheet.getEditTime()).append("\n");
|
||||
content.append("comments: ").append(!sheet.getDisallowComment()).append("\n");
|
||||
|
||||
content.append("---\n\n");
|
||||
content.append(sheet.getOriginalContent());
|
||||
return content.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Sheet removeById(Integer id) {
|
||||
Sheet sheet = super.removeById(id);
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
package run.halo.app.service.impl;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("dev")
|
||||
public class PostServiceImplTest {
|
||||
|
||||
@Autowired
|
||||
private PostServiceImpl postService;
|
||||
|
||||
@Test
|
||||
public void getContent(){
|
||||
String exportMarkdown = postService.exportMarkdown(18);
|
||||
System.out.println(exportMarkdown);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue