Skip to content

Commit

Permalink
Add keep-raw option to decide how to render post (#1668)
Browse files Browse the repository at this point in the history
* feat: Content add front-end rendering options and refactor post params

* feat: add default value for serverSidemarkdownRender

* refactor: content save api

* fix: generate summary

* refactor: remove useless server-side markdown render code

* refactor: allow the formatContent to be empty

* refactor: Rename serverSideMarkdownRender to keepRaw

* refactor: Rename test case
  • Loading branch information
guqing authored Feb 21, 2022
1 parent 923eb17 commit d889a08
Show file tree
Hide file tree
Showing 11 changed files with 306 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,11 @@ public List<Post> updateStatusInBatch(@PathVariable(name = "status") PostStatus
public BasePostDetailDTO updateDraftBy(
@PathVariable("postId") Integer postId,
@RequestBody PostContentParam contentParam) {
Post postToUse = postService.getById(postId);
String formattedContent = contentParam.decideContentBy(postToUse.getEditorType());
// Update draft content
Post post = postService.updateDraftContent(contentParam.getContent(),
contentParam.getContent(), postId);
Post post = postService.updateDraftContent(formattedContent,
contentParam.getOriginalContent(), postId);
return postService.convertToDetail(post);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,12 @@ public void updateStatusBy(
public BasePostDetailDTO updateDraftBy(
@PathVariable("sheetId") Integer sheetId,
@RequestBody PostContentParam contentParam) {
Sheet sheetToUse = sheetService.getById(sheetId);
String formattedContent = contentParam.decideContentBy(sheetToUse.getEditorType());

// Update draft content
Sheet sheet = sheetService.updateDraftContent(contentParam.getContent(),
contentParam.getContent(), sheetId);
Sheet sheet = sheetService.updateDraftContent(formattedContent,
contentParam.getOriginalContent(), sheetId);
return sheetService.convertToDetail(sheet);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@
import run.halo.app.model.entity.Content.PatchedContent;
import run.halo.app.model.entity.Sheet;
import run.halo.app.model.entity.SheetMeta;
import run.halo.app.model.enums.PostEditorType;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.support.HaloConst;
import run.halo.app.model.vo.SheetDetailVO;
import run.halo.app.service.OptionService;
import run.halo.app.service.SheetMetaService;
import run.halo.app.service.SheetService;
import run.halo.app.service.ThemeService;
import run.halo.app.utils.MarkdownUtils;

/**
* Sheet model.
Expand Down Expand Up @@ -75,12 +73,6 @@ public String content(Sheet sheet, String token, Model model) {
}
// render markdown to html when preview sheet
PatchedContent sheetContent = sheetService.getLatestContentById(sheet.getId());
if (sheet.getEditorType().equals(PostEditorType.MARKDOWN)) {
sheetContent.setContent(
MarkdownUtils.renderHtml(sheetContent.getOriginalContent()));
} else {
sheetContent.setContent(sheetContent.getOriginalContent());
}
sheet.setContent(sheetContent);
}

Expand Down
78 changes: 78 additions & 0 deletions src/main/java/run/halo/app/model/params/BasePostParam.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package run.halo.app.model.params;

import java.util.Date;
import java.util.Objects;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;
import lombok.Data;
import org.springframework.util.Assert;
import run.halo.app.model.entity.BasePost;
import run.halo.app.model.entity.Content;
import run.halo.app.model.enums.PostEditorType;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.service.impl.BasePostServiceImpl;
import run.halo.app.utils.MarkdownUtils;

/**
* @author guqing
* @date 2022-02-21
*/
@Data
public abstract class BasePostParam {

protected String title;

protected PostStatus status = PostStatus.DRAFT;

protected String slug;

protected String password;

protected PostEditorType editorType;

protected String content;

protected String originalContent;

protected String summary;

@Size(max = 1023, message = "封面图链接的字符长度不能超过 {max}")
protected String thumbnail;

protected Boolean disallowComment = false;

@Size(max = 255, message = "模版字符长度不能超过 {max}")
protected String template;

@Min(value = 0, message = "排序字段值不能小于 {value}")
protected Integer topPriority = 0;

protected Date createTime;

protected String metaKeywords;

protected String metaDescription;

/**
* if {@code true}, it means is that do not let the back-end render the original content
* because the content has been rendered, and you only need to store the original content.
*/
protected Boolean keepRaw = false;

protected <T extends BasePost> void populateContent(T post) {
Assert.notNull(post, "The post must not be null.");

Content postContent = new Content();
postContent.setOriginalContent(originalContent);

if (Objects.equals(keepRaw, false)
&& PostEditorType.MARKDOWN.equals(editorType)) {
postContent.setContent(MarkdownUtils.renderHtml(originalContent));
} else if (PostEditorType.RICHTEXT.equals(editorType)) {
postContent.setContent(originalContent);
} else {
postContent.setContent(content);
}
post.setContent(Content.PatchedContent.of(postContent));
}
}
34 changes: 34 additions & 0 deletions src/main/java/run/halo/app/model/params/PostContentParam.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package run.halo.app.model.params;

import java.util.Objects;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import run.halo.app.model.enums.PostEditorType;
import run.halo.app.utils.MarkdownUtils;

/**
* Post content param.
Expand All @@ -9,5 +13,35 @@
*/
@Data
public class PostContentParam {

private String content;

private String originalContent;

/**
* if {@code true}, it means is that do not let the back-end render the original content
* because the content has been rendered, and you only need to store the original content.
* otherwise, need server-side rendering.
*/
private Boolean keepRaw = false;

/**
* Decide on post content based on {@link PostEditorType} and serverSideMarkdownRender.
*
* @param editorType edit type to use
* @return formatted content of post.
*/
public String decideContentBy(PostEditorType editorType) {
String originalContentToUse = StringUtils.defaultString(originalContent, "");
String result;
if (Objects.equals(keepRaw, false)
&& PostEditorType.MARKDOWN.equals(editorType)) {
result = MarkdownUtils.renderHtml(originalContentToUse);
} else if (PostEditorType.RICHTEXT.equals(editorType)) {
result = originalContentToUse;
} else {
result = this.content;
}
return result;
}
}
95 changes: 32 additions & 63 deletions src/main/java/run/halo/app/model/params/PostParam.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package run.halo.app.model.params;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import run.halo.app.model.dto.base.InputConverter;
import run.halo.app.model.entity.Content;
import run.halo.app.model.entity.Content.PatchedContent;
import run.halo.app.model.entity.Post;
import run.halo.app.model.entity.PostMeta;
import run.halo.app.model.enums.PostEditorType;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.utils.MarkdownUtils;
import run.halo.app.utils.SlugUtils;

/**
Expand All @@ -28,48 +23,46 @@
* @date 2019-03-21
*/
@Data
public class PostParam implements InputConverter<Post> {
@EqualsAndHashCode(callSuper = true)
public class PostParam extends BasePostParam implements InputConverter<Post> {

@NotBlank(message = "文章标题不能为空")
@Size(max = 100, message = "文章标题的字符长度不能超过 {max}")
private String title;

private PostStatus status = PostStatus.DRAFT;

@Size(max = 255, message = "文章别名的字符长度不能超过 {max}")
private String slug;

private PostEditorType editorType;
private Set<Integer> tagIds;

private String originalContent;
private Set<Integer> categoryIds;

private String summary;
private Set<PostMetaParam> metas;

@Size(max = 1023, message = "封面图链接的字符长度不能超过 {max}")
private String thumbnail;
@Override
@NotBlank(message = "文章标题不能为空")
@Size(max = 100, message = "文章标题的字符长度不能超过 {max}")
public String getTitle() {
return super.getTitle();
}

private Boolean disallowComment = false;
@Override
@Size(max = 255, message = "文章别名的字符长度不能超过 {max}")
public String getSlug() {
return super.getSlug();
}

@Override
@Size(max = 255, message = "文章密码的字符长度不能超过 {max}")
private String password;

@Size(max = 255, message = "Length of template must not be more than {max}")
private String template;

@Min(value = 0, message = "Post top priority must not be less than {value}")
private Integer topPriority = 0;

private Date createTime;

private String metaKeywords;

private String metaDescription;

private Set<Integer> tagIds;
public String getPassword() {
return super.getPassword();
}

private Set<Integer> categoryIds;
public Set<PostMeta> getPostMetas() {
Set<PostMeta> postMetaSet = new HashSet<>();
if (CollectionUtils.isEmpty(metas)) {
return postMetaSet;
}

private Set<PostMetaParam> metas;
for (PostMetaParam postMetaParam : metas) {
PostMeta postMeta = postMetaParam.convertTo();
postMetaSet.add(postMeta);
}
return postMetaSet;
}

@Override
public Post convertTo() {
Expand Down Expand Up @@ -102,28 +95,4 @@ public void update(Post post) {
populateContent(post);
InputConverter.super.update(post);
}

public Set<PostMeta> getPostMetas() {
Set<PostMeta> postMetaSet = new HashSet<>();
if (CollectionUtils.isEmpty(metas)) {
return postMetaSet;
}

for (PostMetaParam postMetaParam : metas) {
PostMeta postMeta = postMetaParam.convertTo();
postMetaSet.add(postMeta);
}
return postMetaSet;
}

private void populateContent(Post post) {
Content postContent = new Content();
if (PostEditorType.MARKDOWN.equals(editorType)) {
postContent.setContent(MarkdownUtils.renderHtml(originalContent));
} else {
postContent.setContent(postContent.getOriginalContent());
}
postContent.setOriginalContent(originalContent);
post.setContent(PatchedContent.of(postContent));
}
}
Loading

0 comments on commit d889a08

Please sign in to comment.