Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for "---" front matter #12

Merged
merged 1 commit into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 + "\n" 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>
20 changes: 20 additions & 0 deletions test/fixtures/front-matter.html.expected.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
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>