auto_cover

This commit is contained in:
xaoxuu 2021-07-25 16:44:55 +08:00
parent d5729f50f7
commit dbc4eaa135
3 changed files with 97 additions and 49 deletions

View File

@ -50,6 +50,8 @@ sidebar:
######## Article ######## ######## Article ########
article: article:
# 如果没有指定封面,是否根据 tags 作为关键词搜索封面?
auto_cover: true # search from https://source.unsplash.com/
# 如果没有指定 excerpt 和 description将自动取多长的内容作为文章摘要 # 如果没有指定 excerpt 和 description将自动取多长的内容作为文章摘要
auto_excerpt: 200 auto_excerpt: 200
# 分类颜色 # 分类颜色
@ -64,6 +66,8 @@ article:
related_posts: related_posts:
enable: false enable: false
max_count: 5 max_count: 5
auto_cover: true # 如果没有封面就根据 tags 作为关键词搜索封面,开了此项将不会自动从文章中提取首张图片作为封面了。
######## Comments ######## ######## Comments ########

View File

@ -1,55 +1,84 @@
<% <%
let showCat = false; function layoutDiv() {
if (post.categories && post.categories.length > 0) { var el = '';
showCat = true; // 封面
} if (post.cover || theme.article.auto_cover) {
%> var cover_url;
<% if (post.cover) { %> if (post.cover !== undefined) {
<div class='post-cover'> if (post.cover.includes('/')) {
<img src='<%- post.cover %>'/> cover_url = post.cover;
</div> } else {
<% } %> cover_url = 'https://source.unsplash.com/1280x640/?' + post.cover;
<h2 class="post-title"> }
<%- (post.title || post.seo_title) ? (post.title || post.seo_title) : date(post.date, config.date_format) %> } else {
</h2> // 自动以 tags 作为关键词搜索封面
<div class='excerpt'> if (post.tags) {
<% if (post.excerpt) { %> var params = '';
<p><%- strip_html(post.excerpt) %></p> post.tags.reverse().forEach((tag, i) => {
<% } else if (post.description) { %> if (i > 0) {
<p><%- post.description %></p> params += ',';
<% } else if (post.content && theme.article.auto_excerpt > 0) { %> }
<p><%- truncate(strip_html(post.content), {length: theme.article.auto_excerpt}) %></p> params += tag.name;
<% } %> });
</div> cover_url = 'https://source.unsplash.com/1280x640/?' + params;
<div class='meta cap'> } else {
<span class='cap' id='post-meta'> cover_url = 'https://source.unsplash.com/random/1280x640';
<%- __('meta.created') %>&nbsp;<time datetime='<%- date_xml(post.date) %>'><%= date(post.date, config.date_format) %></time> }
</span> }
<% if (showCat) { %> if (cover_url) {
<% if (post.layout == 'post' && post.categories && post.categories.length > 0) { %> el += '<div class="post-cover">';
<% el += '<img src="' + cover_url + '"/>';
el += '</div>';
}
}
// 标题
el += '<h2 class="post-title">';
el += (post.title || post.seo_title) ? (post.title || post.seo_title) : date(post.date, config.date_format);
el += '</h2>';
// 摘要
el += '<div class="excerpt">';
el += '<p>';
if (post.excerpt) {
el += strip_html(post.excerpt);
} else if (post.description) {
el += post.description;
} else if (post.content && theme.article.auto_excerpt > 0) {
el += truncate(strip_html(post.content), {length: theme.article.auto_excerpt});
}
el += '</p>';
el += '</div>';
// meta
el += '<div class="meta cap">';
// time
el += '<span class="cap" id="post-meta">';
el += __("meta.created") + '&nbsp;' + '<time datetime="' + date_xml(post.date) + '">' + date(post.date, config.date_format) + '</time>';
el += '</span>';
// cat
if (post.categories && post.categories.length > 0) {
if (post.layout === 'post' && post.categories && post.categories.length > 0) {
var cats = []; var cats = [];
if (post.categories) { if (post.categories) {
post.categories.forEach((cat, i) => { post.categories.forEach((cat, i) => {
cats.push(cat.name); cats.push(cat.name);
}); });
} }
function layoutCats() { if (cats.length > 0) {
if (cats.length > 0) { let cat = cats.shift();
let cat = cats.shift(); el += '<span class="cap breadcrumb"' + category_color(cat) + '>';
var el = '<span class="cap breadcrumb"' + category_color(cat) + '>'; el += cat;
el += cat; el += '</span>';
el += '</span>';
return el;
} else {
return '';
}
} }
%> }
<%- layoutCats() %> }
<% } %> if (post.pin) {
<% } %> el += '<span class="pin"><img src="https://cdn.jsdelivr.net/gh/cdn-x/placeholder@1.0.1/badge/3279dd441df8b.svg"/></span>';
<% if (post.pin) { %> }
<span class='pin'><img src='https://cdn.jsdelivr.net/gh/cdn-x/placeholder@1.0.1/badge/3279dd441df8b.svg'/></span> el += '<div>';
<% } %>
</div> return el;
}
%>
<%- layoutDiv() %>

View File

@ -35,8 +35,23 @@ hexo.extend.helper.register('popular_posts_wrapper', function(args){
if (p && p.length > 0) { if (p && p.length > 0) {
p = p.data[0]; p = p.data[0];
} }
if (p && p.cover) { if (p) {
list.img = p.cover; if (p.cover) {
if (p.cover.includes('/')) {
list.img = p.cover;
} else {
list.img = 'https://source.unsplash.com/1280x640/?' + p.cover;
}
} else if (cfg.auto_cover && p.tags.length > 0) {
var params = '';
p.tags.reverse().forEach((tag, i) => {
if (i > 0) {
params += ',';
}
params += tag.name;
});
list.img = 'https://source.unsplash.com/1280x640/?' + params;
}
} }
if (hexo.theme.config.default.cover) { if (hexo.theme.config.default.cover) {
el += '<div class="img">' el += '<div class="img">'