Skip to content

Improve posts extension with slugs, filename as default slug, and config-based default for layout #30

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

Merged
merged 5 commits into from
Oct 28, 2023

Conversation

paradox460
Copy link
Contributor

@paradox460 paradox460 commented Oct 28, 2023

Changes up a bit of logic in the posts extension to support very thin frontmatters, aiming for some defaults common to other SSGs

  • If permalink is supplied, permalink is where the "post" is generated. No questions asked
  • If a slug is supplied, a config value of path is read. Default is posts/:slug. Post is placed at this path
  • If no value for permalink or slug is provided, filename, stripped of its extension and path components, is used, with the configured default path

This lets you write posts like this:

---
id: Post.4
title: "My new blogpost"
date: "~N[2023-10-27T21:06:43-06:00]"
---

Welcome to my blog!

And have it show up at a normal default spot in your routing tree.

@mhanberg
Copy link
Collaborator

Thanks for the contribution! Overall this looks good, and I am on board with the direction, but I have a couple of tweaks in mind.

If permalink is supplied, permalink is where the "post" is generated. No questions asked

Nice 😎

If a slug is supplied, a config value of path is read. Default is posts/:slug. Post is placed at this path

What do you think about making the config key permalink? I think that path here looks to be the same thing as permalink. The other change would be that you document that :title and :slug are both frontmatter keys that will be replaced in the permalink.

You could even make it capable of using any frontmatter key, but we cant punt that to another patch.

If no value for permalink or slug is provided, filename, stripped of its extension and path components, is used, with the configured default path

😎 , but path would be the default permalink, per my above suggestion.

Also, add some docs for the possible config, and noting which items will be overridden by frontmatter keys

@paradox460
Copy link
Contributor Author

The other change would be that you document that :title and :slug are both frontmatter keys that will be replaced in the permalink.

The config-backed permalink doesn't actually support :title, only :slug. Expanding it to support title, date, and others would probably be pretty useful, but probably something that should be done as a separate transformer

@mhanberg
Copy link
Collaborator

The other change would be that you document that :title and :slug are both frontmatter keys that will be replaced in the permalink.

The config-backed permalink doesn't actually support :title, only :slug. Expanding it to support title, date, and others would probably be pretty useful, but probably something that should be done as a separate transformer

Ahh, sorry. What I meant with my feedback was that you'd retrieve the permalink from the config, then maybe override from the frontmatter, then replace the template variables.

@mhanberg
Copy link
Collaborator

Actually, now that I think about it, I think it might be better to not have a default value for the permalink, and only fallback to the filename.

So

# _posts/articles/my-post.md
id: Foo

would turn into /articles/my-post

config :tableau, Tablea.PostExtension, permalink: "/posts/:title"
# _posts/articles/my-post.md
id: Foo
title: "how to to a post"

would turn into /posts/how-to-to-a-post

and

config :tableau, Tablea.PostExtension, permalink: "/posts/:title"
# _posts/articles/my-post.md
id: Foo
title: "how to to a post"
permalink: "/tips-and-tricks/:title

would turn into /tips-and-tricks/-how-to-do-a-post


I think this is simpler because by default, the logic is not

  • if post has slug, use default permalink
  • else use filename (which also has some logic for the slug, which i'm not sure would even be used)

and becomes

  • if there is a global or local permalink, use it
  • else, use filename

and i think in a subsequent PR, we can introduce replacing the template variables with any arbitrary frontmatter keys, which would accomplish the goal of having a :slug (which would actually just be a userspace convention)

@mhanberg
Copy link
Collaborator

Thanks again for the contribution and with being receptive to feedback!

@paradox460
Copy link
Contributor Author

Removed the slugs, and went with:

  • frontmatter :permalink is always used
  • If config :permalink, use title if present
  • Else use filename

Doesn't work for the _posts/articles/2023-10-28-foo.md case, because the globber doesn't scan for articles beyond the first level. Future PR might be to replace dir with a glob pattern matcher

@mhanberg
Copy link
Collaborator

Doesn't work for the _posts/articles/2023-10-28-foo.md case, because the globber doesn't scan for articles beyond the first level. Future PR might be to replace dir with a glob pattern matcher

oh, yes i see what you mean now.

Nice, this is awesome, thanks!

@mhanberg
Copy link
Collaborator

I'm letting CI run now, will merge when it passes

@mhanberg
Copy link
Collaborator

Looks like i had an errant dbg on main, i went ahead and remvoed that. you'll need to rebase and also run the formatter.

paradox460 and others added 5 commits October 28, 2023 12:07
Changes up a bit of logic in the posts extension to support very thin
frontmatters, aiming for some defaults common to other SSGs

- If permalink is supplied, permalink is where the "post" is generated.
  No questions asked
- If a slug is supplied, a config value of `path` is read. Default is
  `posts/:slug`. Post is placed at this path
- If no value for permalink or slug is provided, filename, stripped of
  its extension and path components, is used, with the configured
  default path
@paradox460
Copy link
Contributor Author

When this gets merged in I'll take a crack at the frontmatter term replacer.

@mhanberg mhanberg merged commit c47e9e5 into elixir-tools:main Oct 28, 2023
@mhanberg
Copy link
Collaborator

merged!

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

Successfully merging this pull request may close these issues.

2 participants