antwarjs / antwar

A static site generator built with React and Webpack.
https://antwar.js.org/
MIT License
460 stars 35 forks source link

Reflect Content Structure in Routing Structure #124

Closed skipjack closed 7 years ago

skipjack commented 7 years ago

Given the following antwar config:

//...
  '/': {
    title: 'webpack',
    path: function() {
        return require.context(
          'json-loader!yaml-frontmatter-loader!./content',
          false,
          /^\.\/.*\.md$/
        );
    },
    processPage: processPage(), 
    layouts: { 
      index: function() {
        return require('./components/splash/splash.jsx').default
      },
      page: function() {
        return require('./components/page/page.jsx').default
      }
    }
  }
// ...

and this content directory...

image

it would be great if antwar produced the routes and content in this manner:

/ - content from index.md /another-page - content from another-page.md /examples - content from examples/index.md /examples/another-child - content from examples/another-child.md

... and so on, treating each directory as a new route with children and the index.md file as the content for the base route. Then within the section.all() data, these pages would be represented as:

[
  {
    title: 'webpack',
    url: '/',
    content: 'content from index.md...',
    pages: [
      { title: 'Another Page', url: '/another-page', content: 'content from another-page.md' },
      { title: 'Examples', url: '/examples', content: 'content from examples/index.md', pages: [
        { title: 'Another Child', url: '/examples/another-child', content: 'content from examples/another-child.md' }
      ]
    ]
  }
]

This way your content structure always reflects your directory structure and much less manual configuration is needed to lay out your project (especially if all files are markdown).

bebraw commented 7 years ago

Maybe it would be possible to tie this together with the schema change discussed at #126.

bebraw commented 7 years ago

I've been thinking about this. The most sensible option seems to be to implement this as an Antwar plugin like prevnext currently. The algorithm would have to go through the entire path definition and attach the information there. I imagine it's not needed for all sites so that's another reason why a plugin feels like the right direction.

skipjack commented 7 years ago

The most sensible option seems to be to implement this as an Antwar plugin like prevnext currently.

Idk, a lot of other static site generators have this as the default behavior that can then be overridden (e.g. via the frontmatter in phenomic). Personally, I think this makes sense as it's more intuitive and easier to manage.

I imagine it's not needed for all sites so that's another reason why a plugin feels like the right direction.

I think there may be some instances where overrides are needed but I can't picture that many sites where this wouldn't be the desired base behavior for the majority of routes/content. I guess it's not a huge deal to have it as a plugin but I think supporting it by default would make more sense.

bebraw commented 7 years ago

Ok. I guess I'll develop it as a plugin (easier to test etc.) and just enable the behavior by default. So it could become an opt-out if necessary.

skipjack commented 7 years ago

That sounds good, I can't argue with it being modularized and if we find out I'm wrong then it'll be even easier to fix 😁.

bebraw commented 7 years ago

There's something like this built-in now behind section.all(). I didn't pluginify the behavior just yet and the design can likely be improved. Better open separate issues for those based on need.

skipjack commented 7 years ago

Sure we can also discuss more in webpack/webpack.js.org#980... it seems the data, section.all() was the last thing (i.e. dynamic route generation seemed fine, just getting programmatic access to those routes was the problem). I've actually been doing some work on this for a separate project as in a webpack only solution that came out pretty nicely. Let's talk more on slack at some point...