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

hide single post or posts in specific categories on the home page #385

Closed
wants to merge 4 commits into from
Closed

Conversation

seven-steven
Copy link

@seven-steven seven-steven commented Aug 11, 2018

PR Checklist

Please check if your PR fulfills the following requirements:

  • The commit message follows our guidelines.
  • Tests for the changes have been added (for bug fixes / features).
    • Muse | Mist have been tested.
    • Pisces | Gemini have been tested.
  • Docs have been added / updated (for bug fixes / features).

PR Type

What kind of change does this PR introduce?

  • Bugfix.
  • Feature.
  • Code style update (formatting, local variables).
  • Refactoring (no functional changes, no api changes).
  • Build related changes.
  • CI related changes.
  • Documentation content changes.
  • Other... Please describe:

What is the current behavior?

Hide single post or posts in specific categories on the home page.

Issue Number(s): #385

What is the new behavior?

Hide single post or posts in specific categories on the home page.

  • Screens with this changes:

    emmmmmmm......
    I think screens can't show this feature well, what I did is just hide some posts on the home page.

  • Link to demo site with this changes: Seven's blog

How to use?

In NexT _config.yml:

# Hide single post or posts in specific categories on the home page
#  If `hide_post` is `true`, you need to add `hide: true` in your post which you want to hide on the home page.
#  If you enabled the item `hide_categories`, you need just list the categories which you want to hide on the home page under the item `categories`.
hide:
  hide_post: false
  hide_categories:
    enable: false
    categories:
#     - categorie1
#     - categorie2

Does this PR introduce a breaking change?

  • Yes.
  • No.

@seven-steven
Copy link
Author

Here is a document of how to use this feature. Document

@ivan-nginx
Copy link
Member

@seven-steven can u add docs in English?

@seven-steven
Copy link
Author

@ivan-nginx
emmmm..... So embarrassed, I just don't know where to add the docs in.
Should I write the docs in _config.yml or somewhere else?

@seven-steven
Copy link
Author

@ivan-nginx
I think it's better to add some comments in the _config.yml .

like this:

# Hide single post or posts in specific categories on the home page
#  If `hide_post` is `true`, you need to add `hide: true` in your post which you want to hide on the home page.
#  If you enabled the item `hide_categories`, you need just list the categories which you want to hide on the home page under the item `categories`.
hide:
  hide_post: false
  hide_categories:
    enable: false
    categories:
#     - categorie1
#     - categorie2

And my English is not very good actually, so is there anythig wrong in my comments?

@ivan-nginx
Copy link
Member

ivan-nginx commented Aug 11, 2018

I think it's better to add some comments in the _config.yml .

Yeah, pretty nice!

@seven-steven
Copy link
Author

@ivan-nginx
Thanks a lot for your affirmation.

@ivan-nginx
Copy link
Member

Need to check it for speed. With many posts. It's index file and i think will slowing down during generate site with ~500 posts or more. Please, need to test it.

@seven-steven
Copy link
Author

I'll try to make it better, thanks for your advice.

@stevenjoezhang
Copy link
Contributor

Any progress you've already made? I'm happy to help with this issue.

@stevenjoezhang
Copy link
Contributor

@seven-steven Another problem, the changes in layout/index.swig will not only affect the home page.

@seven-steven
Copy link
Author

@seven-steven Another problem, the changes in layout/index.swig will not only affect the home page.

emmmm... that's all what I want todo. Do you have other ideas?

@stevenjoezhang
Copy link
Contributor

@seven-steven Use page.current === 1. In index.swig:

{% extends '_layout.swig' %}
{% import '_macro/post.swig' as post_template %}
{% import '_macro/sidebar.swig' as sidebar_template %}

{% block title %}{{ title }}{% if theme.index_with_subtitle and subtitle %} – {{ subtitle }}{% endif %}{% endblock %}

{% block page_class %}
  {% if is_home() %}page-home{% endif -%}
{% endblock %}

{% block content %}
  <section id="posts" class="posts-expand">
    {% for post in page.posts %}
      {% if is_home() && page.current === 1 %}
        {% set hide = false %}
        {% if theme.hide.hide_post && post.hide %}
          {% set hide = true %}
        {% elif theme.hide.hide_categories.enable %}
          {% for cat in post.categories %}
            {% if !hide %}
              {% for categorie in cat %}
                {% if !hide %}
                  {% for item in theme.hide.hide_categories.categories %}
                    {% if !hide && item == categorie %}
                      {% set hide = true %}
                    {% endif %}
                  {% endfor %}
                {% endif %}
              {% endfor %}
            {% endif %}
          {% endfor %}
        {% endif %}
        {% if !hide %}
          {{ post_template.render(post, true) }}
        {% endif %}
      {% else %}
        {{ post_template.render(post, true) }}
      {% endif %}
    {% endfor %}
  </section>

  {% include '_partials/pagination.swig' %}
{% endblock %}

{% block sidebar %}
  {{ sidebar_template.render(false) }}
{% endblock %}

And you can update _config.yml to the latest version, to resolve conflicts.

@seven-steven
Copy link
Author

Need to check it for speed. With many posts. It's index file and i think will slowing down during generate site with ~500 posts or more. Please, need to test it.

Sorry for the delay.

I hava already made a test today under the environment below:

OS: Ubuntu 18.10
CPU: Intel® Core™ i5-4200U CPU @ 1.60GHz × 4 
RAM: 7.7 GiB
Hexo: 3.8.0
node: 8.11.4

I made 790 posts to run the test:
image
And the file size is 5.1Mb in total:
image

Before set the "hide" param, it took 2.9min to generate all the posts:
image

Then I enabled the setting "hide_post", and add a line "hide: true" for all the posts.
It took 1.9min to generate all the posts:
image

At last, I enabled the setting "hide_categories", and add a line "categories: hide" for all the posts.
It took 1.83 min to generate all the posts:
image

That's all, It seems that it can save a little time to generate posts by hide posts on the home page.

@seven-steven
Copy link
Author

And I find a bug here, if you hide some posts on the home page by this PR, the pagination will still count the hiden posts in.
image
I's not serious for me(uaually, I need just hide a few posts), but I'm not sure if it will trouble others.

@seven-steven
Copy link
Author

@stevenjoezhang
Brilliant! Thank's for your advice! I'll do it later.

@ivan-nginx
Copy link
Member

ivan-nginx commented Dec 27, 2018

@seven-steven how you think, it is better will be use hexo-generator-index2 or similar plugin for Hexo? It seems pagination must work fine with this plugin.

Another variant to fix pagination – add filter from this PR to pagination directly?

Anyway, for now we have problem with slow generation for many posts, which i will fix it today. And after this fix i'll check and compare your changes too.

@seven-steven
Copy link
Author

@ivan-nginx
About this feature, I got an idea, searched for information, tried to realize the function and made this PR to share my achievement. I didn't know there's a plugin which can achieve the same function until now.
I viewed the plugin hexo-generator-index2 just now, and I think this plugin may be better than my way.
If necessary, I can make a compare between the plugin and this PR later.
Thanks very much for your reply.

@ivan-nginx
Copy link
Member

@seven-steven same plugin can be created just under the NexT in this path:
https://github.com/theme-next/hexo-theme-next/tree/master/scripts

@seven-steven
Copy link
Author

seven-steven commented Dec 28, 2018

@ivan-nginx
I am doing the compare between the plugin "hexo-generator-index2" and this PR now. the version of Next is just the same as this repository.
There's 700 posts in my "_posts" directory:

  • using the default setting, it took 2.88 min to generate all the posts:

     INFO  1245 files generated in 2.88 min
  • using the plugin hexo-generator-index2, it took 2.92 min to generate all the posts:

     INFO  1245 files generated in 2.92 min

    (I ran the generate command again, and it took 2.82min.)

  • then I changed the Next version to this PR, and hide the same categories as the plugin hexo-generator-index2, it took 2.92min:

     INFO  1245 files generated in 2.92 min

    Only considering the speed, it seems that the plugin and the PR is just the same. About the paginate the plugin does perform better.

To be honest, I am neither familiar with the Complete architecture of next nor javascript language. To fix the paginate by script, I need some time to learn all these things. It could take a time, really sorry about this.

That's all. Thanks.

@stevenjoezhang
Copy link
Contributor

stevenjoezhang commented Mar 8, 2019

How about only hide posts with hide: option in front matter, not in specific categories?

@stevenjoezhang stevenjoezhang self-requested a review March 8, 2019 07:39
@ivan-nginx
Copy link
Member

Why it's needed, can u explain?

@stevenjoezhang
Copy link
Contributor

Emm.. maybe a hexo plugin would be better. Speeding up the generating process is more important

@ivan-nginx
Copy link
Member

ivan-nginx commented Mar 8, 2019

maybe a hexo plugin would be better

Yes.
But anyway, this is not actually needed. User can create page, not post, with any custom settings inside.

Copy link
Member

@ivan-nginx ivan-nginx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, here is generators what provide same functionality if needed:

hexo-generator-index2
hexo-index-anything
hexo-generator-index-plus

@stevenjoezhang
Copy link
Contributor

stevenjoezhang commented Mar 29, 2019

@seven-steven Thanks for your contributing. I'll fork hexo-generator-index to implement this feature, and maybe also the post-sticky feature (#447).

Try this plugin: https://github.com/stevenjoezhang/hexo-generator-indexed
Run:

npm remove hexo-generator-index
npm install hexo-generator-indexed

@seven-steven
Copy link
Author

@stevenjoezhang Thanks a lot.

@ivan-nginx ivan-nginx reopened this Apr 2, 2019
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission, we really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

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 this pull request may close these issues.

5 participants