sergeyzwezdin / hexo-related-posts

Hexo plugin that generates related posts list with TF/IDF algorithm
https://hexo.io/
MIT License
21 stars 8 forks source link

Possible to show post title instead of URL? #5

Closed s-moon closed 2 years ago

s-moon commented 3 years ago

Thank you for this package - it's great and does what I am looking for.

I have a question regarding the example output though, please. In the suggested post code you showed this:

<li><a href="<%= url %>"><%= url %></a></li>

which uses the URL as the A tags text. Is it possible to refer to the posts actual title instead, rather than url?

Thanks

sergeyzwezdin commented 3 years ago

What do you mean refer to the posts actual title instead, rather than url? Could you give an example?

s-moon commented 3 years ago

Hi Sergey,

Thank you for your prompt response and sorry, I should have pasted in an example. Here's one of my blog posts with the links at the bottom:

image

What I would rather have is to have the post's title as the display text, instead of the actual URL. So taking the first example, it would look like this:

It feels like that information isn't available but I am fairly new to Hexo so I am not certain. Is it possible somehow?

Thanks,

adnan360 commented 2 years ago

Just trying out this plugin and had the same issue. Here is a solution I found:

<% if (page.related_posts && page.related_posts.length > 0) { %>
    <section class="related-posts">
        <h2>Related posts</h2>
        <ul>
        <%
        for (const path of page.related_posts) {
            var posts = site.posts.filter(function(post) {
              return post.path === path;
            });
            if (posts && posts.length > 0) {
              posts.each(function(apost) {
              %>
              <li><a href="<%- url_for(apost.path) %>"><%= apost.title || '(no title)' %></a></li>
              <%
              });
            }
        }
        %>
        </ul>
    </section>
<% } %>

Got the idea from here.

It would've been nice if the plugin had something like this built in. But if it's not possible, I think at least this code should be included in the readme.

sergeyzwezdin commented 2 years ago

@s-moon @adnan360 In my templates I use this template:

<% if (posts && posts.length > 0) { %>
<section class="post__related">
    <h2 class="post__related-title"><%= __('post.related') %></h2>
    <ul class="post__related-list">
        <% for (const path of posts) { %> <% const url = post_url(path) %> <% const title = post_title(path) %> <% if (url && title) { %>
        <li class="post__related-item"><a href="<%= url %>" class="post__related-link"><%= title %></a></li>
        <% } %> <% } %>
    </ul>
</section>
<% } %>

and somewhere in helpers:

const { magenta } = require('chalk');

hexo.extend.helper.register('resolve_post', (p) => {
    const { log } = hexo;

    const post = hexo.locals.get('posts').data.find(({ slug, path }) => (slug === p) || (path === p));
    if (post) {
        return post;
    } else {
        log.warn('Unable to resolve %s post', magenta(p));
    }
});

// ...
hexo.extend.helper.register('post_title', (path) => {
    const resolve_post = hexo.extend.helper.get('resolve_post').bind(hexo);
    const post = resolve_post(path);

    if (post) {
        return post.title;
    }
});

This is how it's actually looks like:

https://blog.zwezdin.com/2017/asp-net-core-logging-into-kibana/

2021-12-24-06-06-03

Could you please create the PR if you want to update readme.

adnan360 commented 2 years ago

@sergeyzwezdin Your code seems like a good way to do it. But I guess my code is ok for average people and it requires editing only 1 file. So I made a PR (#7) with my code. Feel free to let me know if any change is necessary.

sergeyzwezdin commented 2 years ago

@adnan360 thanks!