Skip to content

Commit

Permalink
feat(tag/post_link): throw on post_link error (#4938)
Browse files Browse the repository at this point in the history
  • Loading branch information
xbc5 committed Jun 10, 2022
1 parent afc4d7f commit e62f2a6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
9 changes: 6 additions & 3 deletions lib/plugins/tag/post_link.js
Expand Up @@ -12,9 +12,10 @@ const { postFindOneFactory } = require('./');
*/
module.exports = ctx => {
return function postLinkTag(args) {
const error = `<a href="#">Post not found: ${args.join(' ') || 'Invalid post_link'}</a>`;
const slug = args.shift();
if (!slug) return error;
if (!slug) {
throw new Error(`Post not found: "${slug}" doesn't exist for {% post_link %}`);
}

let escape = args[args.length - 1];
if (escape === 'true' || escape === 'false') {
Expand All @@ -24,7 +25,9 @@ module.exports = ctx => {
}

const post = postFindOneFactory(ctx)({ slug });
if (!post) return error;
if (!post) {
throw new Error(`Post not found: post_link ${slug}.`);
}

let title = args.length ? args.join(' ') : post.title;
const attrTitle = escapeHTML(title);
Expand Down
8 changes: 4 additions & 4 deletions test/scripts/tags/post_link.js
Expand Up @@ -57,11 +57,11 @@ describe('post_link', () => {
.should.eql('<a href="/title-with-tag/" title="This is a &lt;b&gt;Bold&lt;&#x2F;b&gt; &quot;statement&quot;">This is a <b>Bold</b> "statement"</a>');
});

it('no slug', () => {
postLink([]).should.eql('<a href="#">Post not found: Invalid post_link</a>');
it('should throw if no slug', () => {
should.throw(() => postLink([]), Error, /Post not found: "undefined" doesn't exist for \{% post_link %\}/);
});

it('post not found', () => {
postLink(['bar']).should.eql('<a href="#">Post not found: bar</a>');
it('should throw if post not found', () => {
should.throw(() => postLink(['bar']), Error, /Post not found: post_link bar\./);
});
});

0 comments on commit e62f2a6

Please sign in to comment.