Skip to content

Commit

Permalink
Making it possible to have files for posts
Browse files Browse the repository at this point in the history
  • Loading branch information
einari committed Feb 29, 2024
1 parent 83b8aa1 commit 3e7cec3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions _plugins/custom-posts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ def process_post(site, post_file)
post.data["slug"] = post.data["title"]

post.content = post.content.gsub(/\(images\//, '({{ page.url | relative_url }}images/')
post.content = post.content.gsub(/\(files\//, '({{ page.url | relative_url }}files/')

if post.data["title"] == "Cross AppDomain Singleton"
puts post.content
end

site.collections['posts'].docs << post
end
Expand Down
21 changes: 21 additions & 0 deletions _plugins/files-from-posts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Jekyll::Hooks.register :site, :post_write do |site|
site.posts.each do |post|
anchor_regex = /a href="(.*?)"/
file_regex = /files\/(.*?)"/
file_urls = post.content.scan(anchor_regex).flatten

source_directory = File.dirname(post.path)
destination_directory = File.dirname(post.destination('/'))

file_urls.each do |file_url|
file_url = File.join("files", File.basename(file_url))
file_path = File.join(source_directory, file_url)

if File.exist?(file_path)
file_dest = File.join(destination_directory, file_url)
FileUtils.mkdir_p(File.dirname(file_dest))
FileUtils.cp(file_path, file_dest)
end
end
end
end

0 comments on commit 3e7cec3

Please sign in to comment.