文章

让chirpy支持下载附件功能

你的附件不一定是图片,视频,他可能是某些文件,不能直接展示,那理应有下载功能

让chirpy支持下载附件功能

功能介绍

点击测试下载(不要关心文件内容,就是本项目文件的打包) 建议把所有需要安排下载的附件打包成zip,再导入obsidian, 不然插件识别不了这么多后缀名.目前支持常见的.zip,.rar等文件

使用方法

在_plugins文件夹中创建一份rb文件

1
/_plugins/auto_add_download.rb

内容为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# Jekyll插件:仅对压缩文件和Office文档添加下载属性

# 文件名:_plugins/download_attribute_adder.rb

  

module Jekyll

    class DownloadAttributeAdder < Generator

      safe true

      priority :normal

      # 指定需要添加下载属性的文件扩展名列表(压缩文件和Office文档)

      DOWNLOADABLE_EXTENSIONS = %w[

        # 压缩文件格式

        zip rar tar gz 7z bz2 xz iso dmg pkg

        # Microsoft Office 文档

        doc docx xls xlsx ppt pptx

        # 其他办公文档格式

        odt ods odp pdf txt csv

        # 可执行文件和其他文档

        exe apk deb rpm msi

      ].freeze

      def generate(site)

        # 遍历所有页面和文章

        all_docs = site.pages + site.posts.docs + site.documents

        all_docs.each do |doc|

          next unless doc.content.respond_to?(:gsub)

          # 检查文档是否包含链接

          if doc.content.match(/<a\s+/i)

            original_content = doc.content

            # 处理HTML中的链接标签

            doc.content = doc.content.gsub(/(<a\s+[^>]*href=["']([^"']+)["'][^>]*>)/mi) do |match|

              full_match = $1

              href_url = $2

              # 检查链接是否指向指定类型的文件

              if should_add_download_attribute?(href_url)

                # 如果链接已经有download属性,则跳过

                unless full_match.include?('download=')

                  # 在href属性前添加download属性

                  modified_match = full_match.sub(/href=/, 'download="" href=')

                  puts "DownloadAttributeAdder: 为 #{get_doc_identifier(doc)} 中的链接 #{href_url} 添加了下载属性"

                  modified_match

                else

                  full_match

                end

              else

                full_match

              end

            end

            # 如果内容发生变化,记录一下

            if original_content != doc.content

              puts "DownloadAttributeAdder: 处理了文档 #{get_doc_identifier(doc)} 中的链接"

            end

          end

        end

      end

      private

      def should_add_download_attribute?(url)

        # 检查是否为外部链接

        return false if url.start_with?('http://', 'https://', '//')

        # 检查是否为锚点链接或javascript链接

        return false if url.start_with?('#', 'javascript:', 'mailto:')

        # 提取文件扩展名

        extension = extract_extension(url)

        # 检查扩展名是否在指定的可下载文件类型列表中

        !extension.nil? && DOWNLOADABLE_EXTENSIONS.include?(extension.downcase)

      end

      def extract_extension(url)

        # 移除URL参数和锚点

        clean_url = url.split(/[?#]/).first

        # 获取最后一个点后面的字符串

        File.extname(clean_url).sub('.', '')

      rescue

        nil

      end

      def get_doc_identifier(doc)

        # 为不同类型的文档获取合适的标识符

        if doc.respond_to?(:id)

          doc.id

        elsif doc.respond_to?(:relative_path)

          doc.relative_path

        elsif doc.respond_to?(:path)

          doc.path

        else

          "unknown_document"

        end

      end

    end

    # 同时使用钩子在渲染后处理,确保处理所有转换后的内容

    Hooks.register [:documents, :pages], :post_render do |doc, payload|

      # 只处理包含链接的HTML输出

      if doc.output && doc.output.include?('<a ')

        original_output = doc.output

        # 处理HTML中的链接标签

        doc.output = doc.output.gsub(/(<a\s+[^>]*href=["']([^"']+)["'][^>]*>)/mi) do |match|

          full_match = $1

          href_url = $2

          # 检查链接是否指向指定类型的文件

          if should_add_download_attribute?(href_url)

            # 如果链接已经有download属性,则跳过

            unless full_match.include?('download=')

              # 在href属性前添加download属性

              modified_match = full_match.sub(/href=/, 'download="" href=')

              puts "DownloadAttributeAdder(post_render): 为 #{get_doc_identifier(doc)} 中的链接 #{href_url} 添加了下载属性"

              modified_match

            else

              full_match

            end

          else

            full_match

          end

        end

        # 如果内容发生变化,记录一下

        if original_output != doc.output

          puts "DownloadAttributeAdder(post_render): 处理了 #{get_doc_identifier(doc)} 中的链接"

        end

      end

      doc # 返回文档对象

    end

    def self.should_add_download_attribute?(url)

      # 检查是否为外部链接

      return false if url.start_with?('http://', 'https://', '//')

      # 检查是否为锚点链接或javascript链接

      return false if url.start_with?('#', 'javascript:', 'mailto:')

      # 提取文件扩展名

      extension = extract_extension(url)

      # 检查扩展名是否在指定的可下载文件类型列表中

      !extension.nil? && DOWNLOADABLE_EXTENSIONS.include?(extension.downcase)

    end

    def self.extract_extension(url)

      # 移除URL参数和锚点

      clean_url = url.split(/[?#]/).first

      # 获取最后一个点后面的字符串

      File.extname(clean_url).sub('.', '')

    rescue

      nil

    end

    def self.get_doc_identifier(doc)

      # 为不同类型的文档获取合适的标识符

      if doc.respond_to?(:id)

        doc.id

      elsif doc.respond_to?(:relative_path)

        doc.relative_path

      elsif doc.respond_to?(:path)

        doc.path

      else

        "unknown_document"

      end

    end

    DOWNLOADABLE_EXTENSIONS = %w[

      # 压缩文件格式

      zip rar tar gz 7z bz2 xz iso dmg pkg

      # Microsoft Office 文档

      doc docx xls xlsx ppt pptx

      # 其他办公文档格式

      odt ods odp pdf txt csv

      # 可执行文件和其他文档

      exe apk deb rpm msi

    ].freeze

  end
本文由作者按照 CC BY 4.0 进行授权