Skip to content

Commit

Permalink
refactor: generate summaries only for content changes
Browse files Browse the repository at this point in the history
  • Loading branch information
guqing committed Jan 14, 2025
1 parent e8ca933 commit f3913d2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public enum Constant {
public static final String PERMALINK_PATTERN_ANNO = "content.halo.run/permalink-pattern";

public static final String CHECKSUM_CONFIG_ANNO = "checksum/config";

public static final String CONTENT_CHECKSUM_ANNO = "checksum/content";
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import run.halo.app.extension.ExtensionClient;
import run.halo.app.extension.ExtensionOperator;
import run.halo.app.extension.ListOptions;
import run.halo.app.extension.MetadataUtil;
import run.halo.app.extension.Ref;
import run.halo.app.extension.controller.Controller;
import run.halo.app.extension.controller.ControllerBuilder;
Expand Down Expand Up @@ -386,6 +387,16 @@ private String getExcerpt(Post post) {
return StringUtils.EMPTY;
}
var content = contentWrapper.get();

var contentChecksum = Hashing.sha256().hashString(content.getContent(), UTF_8).toString();
var annotations = MetadataUtil.nullSafeAnnotations(post);
var oldChecksum = annotations.get(Constant.CONTENT_CHECKSUM_ANNO);
if (Objects.equals(oldChecksum, contentChecksum)) {
return post.getStatusOrDefault().getExcerpt();
}
// update the checksum and generate new excerpt
annotations.put(Constant.CONTENT_CHECKSUM_ANNO, contentChecksum);

var tags = listTagDisplayNames(post);

var keywords = new HashSet<>(tags);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.springframework.web.util.UriUtils.encodePath;

import com.google.common.hash.Hashing;
import java.time.Instant;
import java.util.HashSet;
import java.util.List;
Expand All @@ -25,6 +26,7 @@
import run.halo.app.content.comment.CommentService;
import run.halo.app.core.counter.CounterService;
import run.halo.app.core.counter.MeterUtils;
import run.halo.app.core.extension.content.Constant;
import run.halo.app.core.extension.content.Post;
import run.halo.app.core.extension.content.SinglePage;
import run.halo.app.core.extension.content.Snapshot;
Expand Down Expand Up @@ -372,6 +374,16 @@ private String getExcerpt(SinglePage singlePage) {
return StringUtils.EMPTY;
}
var content = contentWrapper.get();

var contentChecksum = Hashing.sha256().hashString(content.getContent(), UTF_8).toString();
var annotations = MetadataUtil.nullSafeAnnotations(singlePage);
var oldChecksum = annotations.get(Constant.CONTENT_CHECKSUM_ANNO);
if (Objects.equals(oldChecksum, contentChecksum)) {
return singlePage.getStatusOrDefault().getExcerpt();
}
// update the checksum and generate new excerpt
annotations.put(Constant.CONTENT_CHECKSUM_ANNO, contentChecksum);

var context = new ExcerptGenerator.Context()
.setRaw(content.getRaw())
.setContent(content.getContent())
Expand Down

0 comments on commit f3913d2

Please sign in to comment.