-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathraw_content.rb
More file actions
48 lines (40 loc) · 1.23 KB
/
raw_content.rb
File metadata and controls
48 lines (40 loc) · 1.23 KB
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
# _plugins/raw_content.rb
module Jekyll
class RawContentForCollections < Generator
safe true
priority :low
def generate(site)
site.collections.each do |label, collection|
collection.docs.each do |doc|
add_raw_content_to_document(site, doc)
end
end
site.pages.each do |page|
add_raw_content_to_document(site, page)
end
end
private
def add_raw_content_to_document(site, document)
if document.respond_to?(:relative_path) && document.relative_path
source_path = File.join(site.source, document.relative_path)
elsif document.respond_to?(:path) && document.path
source_path = document.path
else
return
end
if File.exist?(source_path)
raw_file_content = File.read(source_path, encoding: 'UTF-8')
content_without_front_matter = remove_front_matter(raw_file_content)
document.data['raw_content'] = content_without_front_matter
end
end
def remove_front_matter(content)
if content =~ /\A---\s*\n.*?\n---\s*\n+/m
result = content.sub(/\A---\s*\n.*?\n---\s*\n+/m, '')
return result
else
return content
end
end
end
end