hexojs / hexo

A fast, simple & powerful blog framework, powered by Node.js.
https://hexo.io
MIT License
39.16k stars 4.81k forks source link

how can I get drafts through API? 怎么获取草稿 用 API #4588

Closed tmirun closed 3 years ago

tmirun commented 3 years ago

Check List

Please check followings before submitting a new issue.

Question

how can I get drafts? when I executed hexo list post it shows Draft like follow terminal log:

INFO  Start processing
Date        Title        Path                    Category       Tags
2020-11-25  Hello World  _posts/hello-world.md   programming    some tags, some tagsfdsafdas, some tagsfdafdsa
2020-11-28  test         _posts/test.md
Draft       fdafdafdsad  _drafts/fdafdafdsad.md  uncategorized  untaged

but when I use hexo API hexo.locals.toObject(), it doesn't show drafts. the following console log shows 2 posts, drafts are not included

{
  posts: _Query { data: [ [_Document], [_Document] ], length: 2 },
  pages: _Query { data: [ [_Document] ], length: 1 },
  categories: _Query { data: [ [_Document] ], length: 1 },
  tags: _Query { data: [ [_Document], [_Document], [_Document] ], length: 3 },
  data: {}
}

also, I tried with hexo.locals.get('posts') but drafts is also not included.

So I want to ask how can I get the drafts?

Thank to help me with this Question 😉

DeepSpaceHarbor commented 3 years ago
const Post = hexo.model('Post'); //This will give you access to posts
let allPosts = Post.toArray(); //All posts
allPosts[ x ].published? // false means the post is draft, true means the post is published.
tmirun commented 3 years ago

@DeepSpaceHarbor thank guy, it is working, I can continue to work!

I will keep it open because I think we need to add it in hexo.locals.get('posts').

and this "hexo.model()" we should writing in API doc @SukkaW ?

tmirun commented 3 years ago

these days I think we can use directlly https://github.com/hexojs/warehouse. to get informations. without hexo.locals.get('Post')

github-actions[bot] commented 3 years ago

This issue has been automatically marked as stale because lack of recent activity. It will be closed if no further activity occurs. Thank you for your contributions.