feat: custom meta tags from md front-matter

This commit is contained in:
ppoffice 2016-02-11 22:50:31 +08:00
parent b421b47e18
commit f2ed331917
2 changed files with 40 additions and 0 deletions

View File

@ -27,6 +27,7 @@
twitter_id: theme.miscellaneous.open_graph.twitter_id,
google_plus: theme.miscellaneous.open_graph.google_plus,
}) %>
<%- meta(page) %>
<% if (theme.rss) { %>
<link rel="alternative" href="<%- theme.rss %>" title="<%= config.title %>" type="application/atom+xml" />

39
scripts/meta.js Normal file
View File

@ -0,0 +1,39 @@
/**
* Meta Helper
* @description Generate meta tags for HTML header
* @example
* <%- meta(post) %>
*/
function trim (str) {
return str.trim().replace(/^"(.*)"$/, '$1').replace(/^'(.*)'$/, '$1');
}
function split (str, sep) {
var result = [];
var matched = null;
while (matched = sep.exec(str)) {
result.push(matched[0]);
}
return result;
}
hexo.extend.helper.register('meta', function (post) {
var metas = post.meta || [];
var output = '';
var metaDOMArray = metas.map(function (meta) {
var entities = split(meta, /(?:[^\\;]+|\\.)+/g);
var entityArray = entities.map(function (entity) {
var keyValue = split(entity, /(?:[^\\=]+|\\.)+/g);
if (keyValue.length < 2) {
return null;
}
var key = trim(keyValue[0]);
var value = trim(keyValue[1]);
return key + '="' + value + '"';
}).filter(function (entity) {
return entity;
});
return '<meta ' + entityArray.join(' ') + ' />';
});
return metaDOMArray.join('\n');
});