Skip to content

Commit

Permalink
Add support for "---" front matter
Browse files Browse the repository at this point in the history
  • Loading branch information
elia committed Jan 17, 2023
1 parent b21be6c commit d1f85a0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/erb/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def initialize(source, line_width: 80, filename: nil, debug: $DEBUG)
@original_source = source
@filename = filename || '(erb)'
@line_width = line_width
@source = source.dup
@source = remove_front_matter source.dup
@html = +""
@debug = debug

Expand All @@ -103,6 +103,13 @@ def initialize(source, line_width: 80, filename: nil, debug: $DEBUG)
freeze
end

def remove_front_matter(source)
source.sub(/\A---\n[\s\S]*?\n---\n/) do |match|
@front_matter = match
match.gsub(/[^\n]/, ' ')
end
end

attr_accessor \
:source, :html, :tag_stack, :pre_pos, :pre_placeholders, :erb_tags, :erb_tags_regexp,
:pre_placeholders_regexp, :tags_regexp, :line_width
Expand Down Expand Up @@ -368,6 +375,7 @@ def format
html.gsub!(erb_tags_regexp, erb_tags)
html.gsub!(pre_placeholders_regexp, pre_placeholders)
html.strip!
html.prepend @front_matter if @front_matter
html << "\n"
end
end
19 changes: 19 additions & 0 deletions test/fixtures/front-matter.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
foo: :bar
bar: "BAZ"
Baz: |
foobar
foo-bar
foo bar
---
<body class="h-full">
<% flash.each do |type, data| %>
<%= render AlertComponent.new(type: type, data: data) %>
<% end %>

<%= render NavbarComponent.new %>

<main class="mx-auto max-w-7xl pb-10 lg:py-12 lg:px-8">
<%= yield %>
</main>
</body>
19 changes: 19 additions & 0 deletions test/fixtures/front-matter.html.expected.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
foo: :bar
bar: "BAZ"
Baz: |
foobar
foo-bar
foo bar
---
<body class="h-full">
<% flash.each do |type, data| %>
<%= render AlertComponent.new(type: type, data: data) %>
<% end %>

<%= render NavbarComponent.new %>

<main class="mx-auto max-w-7xl pb-10 lg:py-12 lg:px-8">
<%= yield %>
</main>
</body>

0 comments on commit d1f85a0

Please sign in to comment.