From d65eb5983726248174696784f682c54e0cc21433 Mon Sep 17 00:00:00 2001 From: Vincenttgao Date: Mon, 28 Oct 2024 20:03:39 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E5=86=85=E5=AE=B9=E7=94=A8=E4=BA=8E?= =?UTF-8?q?follow=E8=AE=A4=E8=AF=81=20&=20=E6=94=AF=E6=8C=81=E5=B0=81?= =?UTF-8?q?=E9=9D=A2=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/run/halo/feed/BasicSetting.java | 2 + .../java/run/halo/feed/FeedServiceImpl.java | 40 +++++++++++++++++-- src/main/java/run/halo/feed/RSS2.java | 8 ++++ src/main/resources/extensions/settings.yaml | 6 +++ 4 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/main/java/run/halo/feed/BasicSetting.java b/src/main/java/run/halo/feed/BasicSetting.java index 7cc269f..aa1d1d4 100644 --- a/src/main/java/run/halo/feed/BasicSetting.java +++ b/src/main/java/run/halo/feed/BasicSetting.java @@ -16,6 +16,8 @@ public class BasicSetting { private Integer outputNum = 20; + private String customContent = ""; // 用户自定义内容,可用于 follow 认证,默认为空 + enum DescriptionType { /** diff --git a/src/main/java/run/halo/feed/FeedServiceImpl.java b/src/main/java/run/halo/feed/FeedServiceImpl.java index b0c0966..a087ba3 100644 --- a/src/main/java/run/halo/feed/FeedServiceImpl.java +++ b/src/main/java/run/halo/feed/FeedServiceImpl.java @@ -180,10 +180,20 @@ private Mono 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("
", ogImage) + content; + } + + return itemBuilder + .description(XmlCharUtils.removeInvalidXmlChar(description)) + .build(); + }); } else { // Set excerpt as description return Mono.just(itemBuilder @@ -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 = " items; + private String customContent; + @Data @Builder public static class Item { @@ -62,6 +65,11 @@ public String toXmlString() { }); } + // 只有当 customContent 不为空且不为空白字符时才添加 + if (StringUtils.hasText(customContent)) { + channel.addElement("custom").addCDATA(customContent); + } + return document.asXML(); } } diff --git a/src/main/resources/extensions/settings.yaml b/src/main/resources/extensions/settings.yaml index 93728b8..734c673 100644 --- a/src/main/resources/extensions/settings.yaml +++ b/src/main/resources/extensions/settings.yaml @@ -32,3 +32,9 @@ spec: label: 内容输出条数 value: 20 validation: required|number|min:1 + - $formkit: code + name: customContent + label: 自定义代码内容 + placeholder: 在此输入要追加到RSS文件末尾的自定义内容,可用于 follow 认证等 + value: "" // 默认值为空 + language: json