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

feat(tag/post_link): throw on post_link error #4938

Merged
merged 4 commits into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/plugins/tag/post_link.js
Original file line number Diff line number Diff line change
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: no slug for post_link.');
}
xbc5 marked this conversation as resolved.
Show resolved Hide resolved

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
Original file line number Diff line number Diff line change
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: no slug 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\./);
xbc5 marked this conversation as resolved.
Show resolved Hide resolved
});
});