From 5837bb80ff432d932984b4cc7a394a3cb2d88310 Mon Sep 17 00:00:00 2001
From: Mimi <1119186082@qq.com>
Date: Sat, 3 Jun 2023 01:32:28 +0800
Subject: [PATCH] feat(tags/post_link): use slug when title is empty (#5220)
---
lib/plugins/tag/post_link.ts | 4 ++--
test/scripts/tags/post_link.js | 9 +++++++++
2 files changed, 11 insertions(+), 2 deletions(-)
diff --git a/lib/plugins/tag/post_link.ts b/lib/plugins/tag/post_link.ts
index 4460cef8fe..782714a1d2 100644
--- a/lib/plugins/tag/post_link.ts
+++ b/lib/plugins/tag/post_link.ts
@@ -27,9 +27,9 @@ export = ctx => {
throw new Error(`Post not found: post_link ${slug}.`);
}
- let title = args.length ? args.join(' ') : post.title;
+ let title = args.length ? args.join(' ') : post.title || post.slug;
// Let attribute be the true post title so it appears in tooltip.
- const attrTitle = escapeHTML(post.title);
+ const attrTitle = escapeHTML(post.title || post.slug);
if (escape === 'true') title = escapeHTML(title);
const link = encodeURL(new URL(post.path, ctx.config.url).pathname);
diff --git a/test/scripts/tags/post_link.js b/test/scripts/tags/post_link.js
index d476933d0a..999f2cad48 100644
--- a/test/scripts/tags/post_link.js
+++ b/test/scripts/tags/post_link.js
@@ -22,6 +22,11 @@ describe('post_link', () => {
source: 'fôo',
slug: 'fôo',
title: 'Hello world'
+ },
+ {
+ source: 'no-title',
+ slug: 'no-title',
+ title: ''
}])));
it('default', () => {
@@ -36,6 +41,10 @@ describe('post_link', () => {
postLink(['foo', 'test']).should.eql('test');
});
+ it('no title', () => {
+ postLink(['no-title']).should.eql('no-title');
+ });
+
it('should escape tag in title by default', () => {
postLink(['title-with-tag']).should.eql('"Hello" <new world>!');
});