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

post_link should throw error if no slug/post found #1594

Closed
GazEdge opened this issue Nov 17, 2015 · 3 comments · Fixed by #4426
Closed

post_link should throw error if no slug/post found #1594

GazEdge opened this issue Nov 17, 2015 · 3 comments · Fixed by #4426
Labels
enhancement New feature or request

Comments

@GazEdge
Copy link

GazEdge commented Nov 17, 2015

I have had a few errors creep into my posts because the post_link plugin returns null if no slug or post is found. The result of this is a very hard to find missing link and text in the generated post. It may be better instead to throw an error, or at least return an anchor with an error href e.g.

'use strict';

/**
 * Post link tag
 *
 * Syntax:
 *   {% post_link slug [title] %}
 */
module.exports = function(ctx) {
  var Post = ctx.model('Post');

  return function postLinkTag(args) {
    var slug = args.shift();
    var title = args.length ? args.join(' ') : post.title;
    var error =  '<a href="#error-' + args[0] + '" title="' + title + '">' + title + '</a>';

    if (!slug) return error;

    var post = Post.findOne({slug: slug});
    if (!post) return error;

    return '<a href="' + ctx.config.root + post.path + '" title="' + title + '">' + title + '</a>';
  };
};
@leesei leesei added the enhancement New feature or request label Dec 15, 2015
@leesei
Copy link
Member

leesei commented Feb 23, 2016

Tracked in #1771

@leesei
Copy link
Member

leesei commented Aug 6, 2018

The proposed solution is not yet implemented.
#1771 only handles rendering of errors.

@leesei leesei reopened this Aug 6, 2018
@stevenjoezhang stevenjoezhang changed the title post_link should throw error id no slug/post found post_link should throw error if no slug/post found Mar 3, 2020
@stevenjoezhang
Copy link
Member

stevenjoezhang commented Mar 3, 2020

Pull Request: #4426

if (!post) return;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants