Skip to content

Commit

Permalink
Add footnote urls
Browse files Browse the repository at this point in the history
  • Loading branch information
ustc-zzzz committed Nov 26, 2023
1 parent a29a832 commit 7601f9b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
10 changes: 10 additions & 0 deletions src/main/java/org/teacon/chahoutan/entity/Revision.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ public Map<String, String> getFootnotes()
return Map.copyOf(map);
}

public Map<String, String> getFootnoteUrls()
{
var footnoteCount = this.footnotes.size();
var map = new LinkedHashMap<String, String>(footnoteCount);
for (int i = 0; i < footnoteCount; ++i) {
map.put(String.format("[%d]", i + 1), "#footnote-" + this.footnotes.get(i));
}
return Map.copyOf(map);
}

public String getRssPlainText()
{
var editors = this.post.getEditors();
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/org/teacon/chahoutan/network/PostResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,39 @@ public record PostResponse(@JsonProperty(value = "id") int id,
@JsonProperty(value = "anchor_urls") List<String> anchorUrls,
@JsonProperty(value = "images") List<ImageResponse> postImages,
@JsonProperty(value = "footnotes") Map<String, String> footnotes,
@JsonProperty(value = "footnote_urls") Map<String, String> footnoteUrls,
@JsonProperty(value = "publish_time") OffsetDateTime publishTime)
{
public static PostResponse from(Post post)
{
var revision = post.getRevision();
var revisionName = revision.getTitle();
var publishTime = post.getPublishTime();
var footnotes = revision.getFootnotes();
var urlPrefix = URI.create(ChahoutanConfig.BACKEND_URL_PREFIX);
var url = urlPrefix.resolve("v1/posts/" + post.getId());
var revisionUrl = urlPrefix.resolve("v1/posts/" + revision.getId());
var type = post.getId() <= Post.getLastPublicPostId(null) ? "post" : "draft";
var editors = post.getEditors().stream().sorted().toList();
var images = revision.getImages().stream().map(ImageResponse::from).toList();
return new PostResponse(post.getId(), url, type, revisionName, revision.getText(), revision.getId(),
revisionUrl, editors, revision.getAnchors(), revision.getAnchorUrls(), images, footnotes, publishTime);
return new PostResponse(post.getId(), url, type,
revisionName, revision.getText(), revision.getId(),
revisionUrl, editors, revision.getAnchors(), revision.getAnchorUrls(),
images, revision.getFootnotes(), revision.getFootnoteUrls(), post.getPublishTime());
}

public static PostResponse from(Revision revision)
{
var post = revision.getPost();
var revisionName = revision.getTitle();
var publishTime = post.getPublishTime();
var footnotes = revision.getFootnotes();
var isPost = post.getRevision() != null && post.getRevision().getId().equals(revision.getId());
var urlPrefix = URI.create(ChahoutanConfig.BACKEND_URL_PREFIX);
var url = isPost ? urlPrefix.resolve("v1/posts/" + post.getId()) : null;
var revisionUrl = urlPrefix.resolve("v1/posts/" + revision.getId());
var type = isPost ? post.getId() <= Post.getLastPublicPostId(null) ? "post" : "draft" : "revision";
var editors = isPost ? post.getEditors().stream().sorted().toList() : null;
var images = revision.getImages().stream().map(ImageResponse::from).toList();
return new PostResponse(post.getId(), url, type, revisionName, revision.getText(), revision.getId(),
revisionUrl, editors, revision.getAnchors(), revision.getAnchorUrls(), images, footnotes, publishTime);
return new PostResponse(post.getId(), url, type,
revisionName, revision.getText(), revision.getId(),
revisionUrl, editors, revision.getAnchors(), revision.getAnchorUrls(),
images, revision.getFootnotes(), revision.getFootnoteUrls(), post.getPublishTime());
}
}

0 comments on commit 7601f9b

Please sign in to comment.