Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/run/halo/feed/BasicSetting.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class BasicSetting {

private Integer outputNum = 20;

private String customContent = ""; // 用户自定义内容,可用于 follow 认证,默认为空


enum DescriptionType {
/**
Expand Down
40 changes: 36 additions & 4 deletions src/main/java/run/halo/feed/FeedServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,20 @@ private Mono<ServerResponse> postListResultToXmlServerResponse(
var releaseSnapshot = post.getSpec().getReleaseSnapshot();
var baseSnapshot = post.getSpec().getBaseSnapshot();
return feedSourceFinder.getPostContent(releaseSnapshot, baseSnapshot)
.map(contentWrapper -> itemBuilder
.description(
XmlCharUtils.removeInvalidXmlChar(contentWrapper.getContent()))
.build());
.map(contentWrapper -> {
String content = contentWrapper.getContent();
String description = content;

// 尝试提取 og:image 封面图
String ogImage = extractOgImage(content);
if (ogImage != null) {
description = String.format("<img src=\"%s\" /><br/>", ogImage) + content;
}

return itemBuilder
.description(XmlCharUtils.removeInvalidXmlChar(description))
.build();
});
} else {
// Set excerpt as description
return Mono.just(itemBuilder
Expand Down Expand Up @@ -215,4 +225,26 @@ private RSS2 buildBaseRss(FeedContext feedContext) {
record FeedContext(BasicSetting basicPluginSetting, SystemSetting.Basic systemBasicSetting,
URL externalUrl) {
}

// 提取 og:image, 用于在摘要中显示封面图
private String extractOgImage(String content) {
if (content == null) {
return null;
}

// 使用简单的字符串匹配来提取 og:image
String metaTag = "<meta property=\"og:image\" content=\"";
int start = content.indexOf(metaTag);
if (start == -1) {
return null;
}

start += metaTag.length();
int end = content.indexOf("\"", start);
if (end == -1) {
return null;
}

return content.substring(start, end);
}
}
8 changes: 8 additions & 0 deletions src/main/java/run/halo/feed/RSS2.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.apache.commons.lang3.StringUtils;

import java.time.Instant;
import java.time.ZoneOffset;
Expand All @@ -22,6 +23,8 @@ public class RSS2 {

private List<Item> items;

private String customContent;

@Data
@Builder
public static class Item {
Expand Down Expand Up @@ -62,6 +65,11 @@ public String toXmlString() {
});
}

// 只有当 customContent 不为空且不为空白字符时才添加
if (StringUtils.hasText(customContent)) {
channel.addElement("custom").addCDATA(customContent);
}

return document.asXML();
}
}
6 changes: 6 additions & 0 deletions src/main/resources/extensions/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ spec:
label: 内容输出条数
value: 20
validation: required|number|min:1
- $formkit: code
name: customContent
label: 自定义代码内容
placeholder: 在此输入要追加到RSS文件末尾的自定义内容,可用于 follow 认证等
value: "" // 默认值为空
language: json