jondashkyle / nanopage

super handy utilities for traversing flat content state
Apache License 2.0
42 stars 4 forks source link

Use key as url if unavailable #6

Open jondashkyle opened 6 years ago

jondashkyle commented 6 years ago

The new pages regex checks against a key named url to find children. This is fine when using with something like hypha as it defines a url key in each page object. Would be cool to fallback to the page key name somehow if that url is unavailable, but gets a little tricky with how value is continually set as methods are called with function chainability.

Anyway, quick though and dropping here to think about later.

jongacnik commented 6 years ago

Yea I was already thinking about this! Using url feels really fragile and would much rather use key. I could see this working a couple ways. Demonstrating here with v psuedo code (imagine this making sense in the context of the class):

  1. Store a reference to the current _key, in addition to _state and _value
function page (key) {
  this._key = key
  this._value = this._state[key]
}

function children () {
  var children = // regex using `this._key`
  return children
}
  1. Store an object rather than just the value when looking up a page:

function page (key) {
  this._value = {
    [key]: this._state[key]
  }
}

function children () {
  var key = Object.keys(this._value)[0]
  var children = // regex using `key`
  return children
}
jondashkyle commented 6 years ago

something closer to the second option might be right, as i’m unsure how we’d handle that with a heavy chain such as:

page('/example').pages().sortBy('date', 'asc').first().find('wtf').v()

that Object.keys solution seems like a bit of a hack, in that if you run children when not on a page you might get some wild results, but that might be nbd.