更好的 Hexo 标题标签
Better Hexo title tag
我已经在 Hexo 中设置了我的 post 并为每个 post 分配了标签。然而 title 标签没有按照我想要的方式大写。
这是渲染后的HTML:
<title>Viewing pizza | My site</title>
但我会实现这个:
<title>Viewing Pizza | My site</title>
标签:pizza 是小写的,不知道如何在 title 标签中让标签以大写字母开头(例如披萨、意大利面、意大利等)。
我的代码:
<%
function capitalize (str) { return str.charAt(0).toUpperCase() + str.substring(1).toLowerCase() }
var title = page.title;
if (is_archive()) {
title = capitalize(__('Viewing'));
if (is_month()) {
title += ': ' + page.year + '/' + page.month;
} else if (is_year()) {
title += ': ' + page.year;
}
} else if (is_category()) {
title = capitalize(__('Viewing')) + ': ' + page.category;
} else if (is_tag()) {
title = capitalize(__('Viewing')) + ': ' + page.tag;
}
%>
<title><% if (title) { %><%= title %> | <% } %><%= config.title %></title>
提前致谢!
这是一个函数,可以将句子中的每个单词大写:
function capWords(str) {
// we split string by words in an array
// and we iterate on each word to capitalize the first letter
// and we join each element with a space
return str.split(' ').map(function(str) {
return str[0].toUpperCase() + str.substr(1).toLowerCase()
}).join(' ');
}
在您的代码中:
<%
function capWords(str) {
// we split string by words in an array
// and we iterate on each word to capitalize the first letter
// and we join each element with a space
return str.split(' ').map(function(str) {
return str[0].toUpperCase() + str.substr(1).toLowerCase()
}).join(' ');
}
var title = page.title;
if (is_archive()) {
title = __('Viewing');
if (is_month()) {
title += ': ' + page.year + '/' + page.month;
} else if (is_year()) {
title += ': ' + page.year;
}
} else if (is_category()) {
title = __('Viewing') + ': ' + page.category;
} else if (is_tag()) {
title = __('Viewing') + ': ' + page.tag;
}
%>
<title>
<% if (title) { %>
<%= capWords(title) + ' | ' %>
<% } %>
<%= config.title %>
</title>
我不知道这是否是 hexo 中的新功能,但如果您仍在寻找,titlecase 是您可以在模板中使用的功能。
现在你的 hexo 安装应该已经有了。如果您使用 ejs
作为渲染器,您可以按照文档所述使用它:
你可以 <%- titlecase('pizza') %>
得到你想要的。
如果您需要编写自己的函数,首选方法是将它们写入 /scripts/my_helpers.js
文件(将 .js 文件命名为您想要的任何名称,但它必须位于 scripts
在你的项目目录中)。或者,发布带有 hexo-
前缀的模块并将其导入到您的项目中(如果您这样做,请确保它在 package.json 中列出)。
然后您可以在您的 .js 文件中使用以下咒语使您的 javascript 功能可用:
// note, I haven't tested this code.
hexo.extend.helper.register('capitalize', (aString) => {
return aString.split(" ").map(function(word) {
return word[0].toUpperCase() + word.substring(1).toLowerCase()}).join(" ")
});
那你可以用<%- capitalize("i am a strInG") %>
<title>
<% if (page.title) { %>
<%= capitalize(page.title) %> |
<% } %>
<%= config.title %>
</title>
`
我已经在 Hexo 中设置了我的 post 并为每个 post 分配了标签。然而 title 标签没有按照我想要的方式大写。
这是渲染后的HTML:
<title>Viewing pizza | My site</title>
但我会实现这个:
<title>Viewing Pizza | My site</title>
标签:pizza 是小写的,不知道如何在 title 标签中让标签以大写字母开头(例如披萨、意大利面、意大利等)。
我的代码:
<%
function capitalize (str) { return str.charAt(0).toUpperCase() + str.substring(1).toLowerCase() }
var title = page.title;
if (is_archive()) {
title = capitalize(__('Viewing'));
if (is_month()) {
title += ': ' + page.year + '/' + page.month;
} else if (is_year()) {
title += ': ' + page.year;
}
} else if (is_category()) {
title = capitalize(__('Viewing')) + ': ' + page.category;
} else if (is_tag()) {
title = capitalize(__('Viewing')) + ': ' + page.tag;
}
%>
<title><% if (title) { %><%= title %> | <% } %><%= config.title %></title>
提前致谢!
这是一个函数,可以将句子中的每个单词大写:
function capWords(str) {
// we split string by words in an array
// and we iterate on each word to capitalize the first letter
// and we join each element with a space
return str.split(' ').map(function(str) {
return str[0].toUpperCase() + str.substr(1).toLowerCase()
}).join(' ');
}
在您的代码中:
<%
function capWords(str) {
// we split string by words in an array
// and we iterate on each word to capitalize the first letter
// and we join each element with a space
return str.split(' ').map(function(str) {
return str[0].toUpperCase() + str.substr(1).toLowerCase()
}).join(' ');
}
var title = page.title;
if (is_archive()) {
title = __('Viewing');
if (is_month()) {
title += ': ' + page.year + '/' + page.month;
} else if (is_year()) {
title += ': ' + page.year;
}
} else if (is_category()) {
title = __('Viewing') + ': ' + page.category;
} else if (is_tag()) {
title = __('Viewing') + ': ' + page.tag;
}
%>
<title>
<% if (title) { %>
<%= capWords(title) + ' | ' %>
<% } %>
<%= config.title %>
</title>
我不知道这是否是 hexo 中的新功能,但如果您仍在寻找,titlecase 是您可以在模板中使用的功能。
现在你的 hexo 安装应该已经有了。如果您使用 ejs
作为渲染器,您可以按照文档所述使用它:
你可以 <%- titlecase('pizza') %>
得到你想要的。
如果您需要编写自己的函数,首选方法是将它们写入 /scripts/my_helpers.js
文件(将 .js 文件命名为您想要的任何名称,但它必须位于 scripts
在你的项目目录中)。或者,发布带有 hexo-
前缀的模块并将其导入到您的项目中(如果您这样做,请确保它在 package.json 中列出)。
然后您可以在您的 .js 文件中使用以下咒语使您的 javascript 功能可用:
// note, I haven't tested this code.
hexo.extend.helper.register('capitalize', (aString) => {
return aString.split(" ").map(function(word) {
return word[0].toUpperCase() + word.substring(1).toLowerCase()}).join(" ")
});
那你可以用<%- capitalize("i am a strInG") %>
<title>
<% if (page.title) { %>
<%= capitalize(page.title) %> |
<% } %>
<%= config.title %>
</title>
`