_This is a suggestion, not a bug._
Given that one of the project’s goals is:
Intuitive directory-based URLs. The URL of a page is derived from its spot on the file system.
...it seems odd that the path attribute in the front-matter of an index.md file is always treated as absolute.
Imagine I have a directory structure:
/pages
/writes
/1-hello-world
index.md
/another-page
index.md
And the front-matter of /pages/writes/1-hello-world/index.md includes:
path: "hello-world"
(note the lack of starting “/”)
Based on the goal quoted above, I'd expect the resulting URL to be /writes/hello-world. Currently, though, the resulting URL is /hello-world.
I realize I can simply leave off the path attribute (which results in /writes/1-hello-world), but I like using a preceding number or date to keep the pages in the correct order in the filesystem, and I would like that number or date to _not_ be in the URL. Right now, the workaround is to add a path like “writes/hello-world”.
For people that like the current behavior, using an absolute instead of relative path could provide that.
I’m just diving into this stuff, but if someone can point me in the right direction, I’ll try to investigate further toward an eventual PR.
I also realize this is breaking change, so maybe it should just stay as-is.
Hmmm... interesting point. I don't have a great intuition for this as I don't regularly hard-code paths in markdown files (part of my reason for wanting to derive urls from the directory position ;-)).
@gatsbyjs/gatsby-core-maintainers any thoughts?
(there's also I should note the poorly documented rewritePath where you can programmatically rewrite paths to remove leading values from the path which is what I do on my blog)
That’ll probably work just fine for my needs. Thanks!
Edit:
Adding this to app.js works exactly as I need it to. But is there a way to remove the hard-coded “writes” parent directory name?
exports.rewritePath = function(parsedFilePath, metadata) {
if (metadata.file.ext === 'md') {
return "/writes/" + parsedFilePath.dirname.split('---')[1] + "/"
}
}
Coming from Jekyll, I liked being able to separate out pages from posts in different directories and you could have a permalink option set in the site's config that would be applied to anything in the post directory.
With gatsby's focus on providing the minimal feature set and relying on plug-ins to fill in the gaps, I think building out a plug-in with rewritePath would be a good solution.
@kylegach hard coded where? In the parsed file info?
@michaeljdeeb great point. I'm going to be working on fine-tuning/filling out the API + adding a plugin architecture soon so yeah, someone could create a plugin easily enough to add this sort of functionality.
Hard-coded in that snippet I included. Ideally, I could replace “writes” with some representation of the parent of parsedFilePath.dirname, but I don't know how or if it's possible.
Ah ok. Yeah writes should be somewhere in the parsedFilePath object if you want to make this work across multiple directories.
It seems that #223 would make this a moot issue. Or rather, it shows that I had an incorrect understanding of the way the path is intended to work in front-matter.
Yeah I think defaulting to paths that are relative to the site root is the
simplest default. And with the future plunging architecture as Michael
mentioned someone could easily create a relative path plugin.
On Sat, Apr 9, 2016 at 4:03 PM kylegach [email protected] wrote:
It seems that #223 https://github.com/gatsbyjs/gatsby/pull/223 would
make this a moot issue. Or rather, it shows that I had an incorrect
understanding of the way the path is intended to work in front-matter.—
You are receiving this because you are on a team that was mentioned.Reply to this email directly or view it on GitHub
https://github.com/gatsbyjs/gatsby/issues/221#issuecomment-207868075
Most helpful comment
Coming from Jekyll, I liked being able to separate out pages from posts in different directories and you could have a permalink option set in the site's config that would be applied to anything in the post directory.
With gatsby's focus on providing the minimal feature set and relying on plug-ins to fill in the gaps, I think building out a plug-in with rewritePath would be a good solution.