lunelson / penny

JavaScript ES6, Sass and Pug "on-the-fly" server built on Connect / Browser-Sync
MIT License
3 stars 0 forks source link

Metadata in Locals, incl.pagination #18

Open lunelson opened 5 years ago

lunelson commented 5 years ago

locals

$page keys

$pkg
$env
$data
$page
  url -> pass to link()
  slug ->
  matter -> from gray-matter
  params -> from dynamic segment matchers
  srcFile
  outFile
  ? date -> parsed from creation date; can be overriden from 'matter'
  ? title -> parsed from slug; can be overriden from 'matter'
  ? paginated -> added if matter contains valid `paginate` instructions
      index: __p || 0
      group: chunk(data, size)[__p]
      groups: chunk(data, size)[__p]
      baseUrl: same as $page.url
      prevUrl: prev page's url
      nextUrl: next page's url
      lastUrl: last page's url
$pages

NOTE: the $page local is actually a getter, since on first call it might need to evaluate the paginate.data key from matter. The following is a rough sketch of how this works:

const vm = require('vm')

const locals = {
  get $page() {
    const { matter: { paginate } } = this.__$p;
    if (!paginate) return this.__$p
    if ('__$e' in this) return this.__$e;
    this.__$e = this.__$p;
    this.__$e.matter.paginate.data = vm.runInNewContext(paginate.data, this);
    return this.__$e;
  },
  __$p: {
    matter: {
      paginate: {
        data: '$data.posts',
        size: 5
      }
    }
  },
  $data: {
    posts: [
      1, 2, 3, 4
    ]
  }
}