Skip to content
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

Site variables documentation is wrong #1567

Open
noraj opened this issue Nov 21, 2020 · 4 comments · May be fixed by #1568
Open

Site variables documentation is wrong #1567

noraj opened this issue Nov 21, 2020 · 4 comments · May be fixed by #1568

Comments

@noraj
Copy link
Contributor

noraj commented Nov 21, 2020

This part of the documentation in https://hexo.io/docs/variables#Site-Variables page is wrong :

image

See this related issue hexojs/hexo#3038

Actually site.posts or site.pages are not arrays returning posts or pages, you actually have to call site.posts.data or site.pages.data. I took me days to figure that out, I had the exact same problem as hexojs/hexo#3038

@SukkaW
Copy link
Member

SukkaW commented Nov 21, 2020

Nope.

site.posts, site.pages (etc.) actually return a Query object, which contains an array of Document object. data is a private property of Query and anyone except hexo's projects should NEVER access that property directly.

@noraj
Copy link
Contributor Author

noraj commented Nov 22, 2020

So same as in #3038 how do I manage the returned (query) object?

Because the answer in #3038 is to call .data and it works but when I call only page.posts and not page.posts.data I have an inerrable object which is not an array where none of the post or page methods are available.

        each po in page.posts.data
          p
            time(datetime=date_xml(po.date) itemprop="datePublished")
              = date(po.date)
            span
              = ' | ' + po.title

The docs say site.pages should return an array of page objects but it doesn't! I can't do this :

        each po in page.posts
          p = po.title

So ok page.posts return a Query objects but this mean the doc is incorrect and must be changed and tell me how to deal with the query object please?
I'm not a JS dev and I find it especially complex to debug JS because there is no inspection methods like var_dump in PHP or .class and .methods in ruby. Usually JS people tell you to put the object in a console.log() but this work only for client side dev in a browser.

The thing is I'm not a JS expert, I wanted to build a theme I referred to the doc, and when reading the actual doc I expect this code to work and actually it doesn't.

        each po in page.posts
          p = po.title

I wasn't aware of this warehouse doc (https://hexojs.github.io/warehouse/) by reading this I understand that the easiest solution for my issue is:

        each po in page.posts.toArray()
          p
            time(datetime=date_xml(po.date) itemprop="datePublished")
              = date(po.date)
            span
              = ' | ' + po.title

Is this the right way to do? I don't need to call an enumerator function like map or each since I'm already using each pug iterator that will work fine with an array.

I'll PR the doc to fix it.

noraj added a commit to noraj/site that referenced this issue Nov 22, 2020
@noraj noraj linked a pull request Nov 22, 2020 that will close this issue
22 tasks
@SukkaW
Copy link
Member

SukkaW commented Nov 22, 2020

page.posts.toArray()

warehouse already provided forEach, sort, find, findOne and many other util implementation, why use JavaScript built-in Array method instead?

@noraj
Copy link
Contributor Author

noraj commented Nov 22, 2020

warehouse already provided forEach, sort, find, findOne and many other util implementation, why use JavaScript built-in Array method instead?

IDK I saw toArray listed on the warehouse and I need an array in the end, sort return a query object for example. I ended up with something like that each po in page.posts.sort('date', -1).toArray(). But as I said I'm not JS dev so I may not take the best choices.

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 a pull request may close this issue.

2 participants