2
2
3
3
Halo 2.0 的 RSS 订阅链接生成插件
4
4
5
+ ## 如何扩展 RSS 源
6
+
7
+ > 从 feed 插件 v1.4.0 版本开始,支持扩展 RSS 功能。
8
+
9
+ ` feed ` 插件提供了扩展点,允许其他插件扩展 RSS 源。
10
+
11
+ ### 步骤 1:在插件中引入 feed 依赖
12
+
13
+ 在你的插件项目中添加 ` feed ` 插件的依赖:
14
+
15
+ ``` groovy
16
+ dependencies {
17
+ // ...
18
+ compileOnly "run.halo.feed:api:{version}"
19
+ }
20
+ ```
21
+
22
+ 将 ` {version} ` 替换为实际的 ` feed ` 插件版本号。
23
+
24
+ ### 步骤 2:实现 ` RssRouteItem ` 扩展点接口
25
+
26
+ 创建一个类实现 ` run.halo.feed.RssRouteItem ` 接口,提供自定义的 RSS 数据源。例如:
27
+
28
+ ``` java
29
+ public class MomentRssProvider implements RssRouteItem {
30
+ // 实现具体的 RSS 提供逻辑
31
+ }
32
+ ```
33
+
34
+ 你可以参考 [ PostRssProvider] ( ./app/src/main/java/run/halo/feed/provider/PostRssProvider.java ) 示例。
35
+
36
+ ### 步骤 3:声明扩展点
37
+
38
+ 在 ` src/main/resources/extensions `
39
+ 目录下,声明你的扩展。你可以参考 [ ext-definition.yaml] ( app/src/main/resources/extensions/ext-definition.yaml ) 文件来完成此步骤。
40
+
41
+ ### 步骤 4:定义配置类并清理 RSS 缓存
42
+
43
+ 在插件中定义一个配置类,使用 ` @ConditionalOnClass ` 注解确保只有在 ` run.halo.feed.RssRouteItem ` 类存在时才会创建对应的
44
+ Bean。同时,定义事件监听器来清理缓存。
45
+
46
+ ` @ConditionalOnClass ` 注解只能使用 name 属性来指定类全限定名,不支持使用 value 属性。
47
+
48
+ 示例代码:
49
+
50
+ ``` java
51
+
52
+ @Configuration
53
+ @ConditionalOnClass (name = " run.halo.feed.RssRouteItem" )
54
+ @RequiredArgsConstructor
55
+ public class RssAutoConfiguration {
56
+ private final ApplicationEventPublisher eventPublisher;
57
+
58
+ @Bean
59
+ public MomentRssProvider momentRssProvider () {
60
+ return new MomentRssProvider ();
61
+ }
62
+
63
+ @Async
64
+ @EventListener ({MomentUpdatedEvent . class, MomentDeletedEvent . class, ContextClosedEvent . class})
65
+ public void onMomentUpdatedOrDeleted () {
66
+ var rule = CacheClearRule . forExact(" /feed/moments/rss.xml" );
67
+ var event = RssCacheClearRequested . forRule(this , rule);
68
+ eventPublisher. publishEvent(event);
69
+ }
70
+ }
71
+ ```
72
+
73
+ 此配置确保了当 ` RssRouteItem ` 接口存在时,插件才会自动创建 ` MomentRssProvider ` 并监听相关事件来清理缓存。
74
+
75
+ ### 步骤 5:声明插件依赖
76
+
77
+ 在 ` plugin.yaml ` 文件中声明 ` feed ` 插件为可选依赖,确保当 ` feed ` 插件存在并启用时,插件能够自动注册 RSS 源。
78
+
79
+ ``` yaml
80
+ apiVersion : plugin.halo.run/v1alpha1
81
+ kind : Plugin
82
+ metadata :
83
+ name : moment
84
+ spec :
85
+ pluginDependencies :
86
+ " PluginFeed? " : " >=1.4.0"
87
+ ` ` `
88
+
89
+ 这样,当 ` feed` 插件可用时,插件会自动注册自定义的 RSS 源。
90
+
5
91
# # 开发环境
6
92
7
93
` ` ` bash
@@ -28,15 +114,15 @@ cd path/to/plugin-feed
28
114
29
115
` ` ` yaml
30
116
halo:
31
- plugin :
32
- runtime-mode : development
33
- classes-directories :
34
- - " build/classes"
35
- - " build/resources"
36
- lib-directories :
37
- - " libs"
38
- fixedPluginPath :
39
- - " /path/to/plugin-feed"
117
+ plugin:
118
+ runtime-mode: development
119
+ classes-directories:
120
+ - "build/classes"
121
+ - "build/resources"
122
+ lib-directories:
123
+ - "libs"
124
+ fixedPluginPath:
125
+ - "/path/to/plugin-feed"
40
126
` ` `
41
127
42
128
# # 使用方式
0 commit comments