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

feat: Super-powerful new Ruby Front Matter syntax #263

Closed
jaredcwhite opened this issue Mar 22, 2021 · 6 comments · Fixed by #285
Closed

feat: Super-powerful new Ruby Front Matter syntax #263

jaredcwhite opened this issue Mar 22, 2021 · 6 comments · Fixed by #285
Labels
enhancement New feature or request

Comments

@jaredcwhite
Copy link
Member

jaredcwhite commented Mar 22, 2021

Suggested by @drush!

Instead of parsing front matter as YAML and then having to do a funky syntax to support Ruby expressions, we could simply add a native Ruby hash syntax. That way:

title: I'm a Page
categories:
 - category1
 - category2

could instead be expressed by:

{
  title: "I'm a Page",
  categories: [1, 2].map { |num| "category#{num}" }
}

For the front matter delimiter, I propose the following since it works well with Markdown syntax highlighting (in VS code anyway):

~~~ruby
{
  key: "value"
}
~~~

Content here.

If we wanted to be more in sync with the style Eleventy uses for dynamic front matter, we could do this:

---ruby
{
  key: "value"
}
---

Content here.

But that doesn't provide syntax highlighting in Markdown.

As it so happens I've already implemented this locally 🤯 (with the first option), and it works extremely well! It even evaluates lambdas in the context of the resource object so you can do stuff like this:

{
  layout: :post,
  title: "Blog post",
  also_title: ->{ data.title },
  permalink: ->{ "/sections/#{collection.label}/#{data.title.gsub(' ', '').downcase}.html" }
}

Whoa.

@jaredcwhite
Copy link
Member Author

jaredcwhite commented Mar 23, 2021

I'm thinking of an alternate front matter syntax that's suited to ERB (or Serbea) syntax. For example:

~~~<%
{
  layout: :post,
  title: "Blog post",
  also_title: ->{ data.title },
  permalink: ->{ "/sections/#{collection.label}/#{data.title.gsub(' ', '').downcase}.html" }
}
%>~~~

Content here.

UPDATE: not planning to further pursue the below. See instead: #284

That not only provides good syntax highlighting, but it also solves a conceptual problem that's been nagging me for a long time now. How do we make it easy to define single-file ERB-based components just like we've had Liquid components? Possible solution:

~~~<%
module Sidebar
  class Card < SiteComponent
    description "A card you can place anywhere in the sidebar."

    interface do
      title String, description: "Display a title for your component."
      kind? enum: [:one, :two, :three], default: :one
    end
  end
end
%>~~~

<div class="card">
  <strong>Hello! <%= @title %></strong>

  <% if @kind == :two %>
    I'm kind two!
  <% end %>
</div>

Then in similar fashion to the ViewComponent library:

<%= render Sidebar::Card.new(title: "I'm a title", kind: :two) %>

@jaredcwhite
Copy link
Member Author

(P. S. Something's wonky about GitHub's syntax highlighting above…works great in VS Code!)

@jaredcwhite
Copy link
Member Author

(made a few edits to the above proposals to standardize on ~~~)

@andreimoment
Copy link

@jaredcwhite this looks very powerful. Will the erb-format ruby frontmatter be applicable to slim-Lang templates?

@jaredcwhite
Copy link
Member Author

The parsing out of the Ruby front matter is done prior to template evaluation, so in theory it should work fine but I'd want to double-check any syntax highlighting issues for editors.

@jaredcwhite
Copy link
Member Author

Resolved in #285

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants