Nexmoe

Nexmoe

一个开发者。关于勇敢与热爱,互联网/创造/赛博朋克
twitter
github

Hexo Previous and Next Articles in Categories

In the "Remaining Books" theme, I treat categories as books, and the articles in the categories form the content of a book.

At this time, it is not possible to simply call all the articles on the site as the content of the previous and next articles.

Therefore, combining with Hexo Output Previous and Next Articles Based on Categories - Shadow Dream (nexmoe.com), I wrote a script to output the previous and next articles based on categories.

I'll just paste the code here, you should be able to understand it from the comments.

<nav class="post-nav">
<% site.categories.map(function(category){  %>
    <% page.categories.map(function(page_category){ %>
        <% if(page_category.name == category.name){ %> <!-- First find the directory that matches the current text -->
            <% let i = 0;%>
            <% category.posts.sort('-date').map(function(post){ %>
                <% i++;  %> 
                <% if(post.title == page.title){ %> <!-- Then find the index of the current article -->
                    <% let ix = 0;%>
                    <% category.posts.sort('-date').map(function(post){ %>
                        <% ix++; %> 
                        
                        <% if(ix == i + 1 && post.title){ %>  <!-- Previous article --> 
                            <div class="old">
                                <span>Previous Chapter</span>
                                <a href="<%- url_for(post.path) %>"> <%= post.title %></a>
                            </div>
                        <% } %>
                        
                        <% if(ix == i - 1 && post.title){ %> <!-- Next article --> 
                            <div class="new">
                                <span>Next Chapter</span>
                                <a href="<%- url_for(post.path) %>"> <%= post.title %></a>
                            </div>
                        <% } %>
                        
                    <% }) %>
                <% } %>
            <% }) %>
        <% } %>
    <% })%>
<% }) %>
</nav>
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.