10
          09/2014
        
      hexo 的 meta description
hexo meta description 部分的代码有点问题
在 light/layout/_partial/head.ejs 的代码
<% if (page.description){ %>
  <meta name="description" content="<%= page.description %>">
  <% } else if (config.description){ %>
  <meta name="description" content="<%= config.description %>">
  <% } else if (page.excerpt){ %>
  <meta name="description" content="<%= strip_html(page.excerpt).replace(/^\s*/, '').replace(/\s*$/, '') %>">
  <% } else if (page.content){ %>
  <meta name="description" content="<%= strip_html(page.content).replace(/^\s*/, '').replace(/\s*$/, '').substring(0, 150) %>">
  <% } %>
  <% if (page.keywords){ %><meta name="keywords" content="<%= page.keywords %>"><% } %>
- 如果 .md 中有 description 就用(如下边代码所示); - title: hexo 的 meta description date: 2013-04-28 09:42:47 category: hexo tags: hexo description: hexo 的 meta description 写在这里
- 否则,用 _config.yml 的站点 description; 
- 否则,用内容简介(如果写文章的时候用过 <!——> 的话),删除所有前后的空格;
- 否则,用内容前 150个字,删除所有前后的空格
- 最后一句是 meta keywords 的
显然,这个顺序不是很理想。修改为下面的代码
<% if (page.description){ %>
  <meta name="description" content="<%= page.description %>">
  <% } else if (page.excerpt){ %>
  <meta name="description" content="<%= strip_html(page.excerpt).replace(/^\s*/, '').replace(/\s*$/, '').replace(/[\n,\r]/g,'') %>">
  <% } else if (page.content){ %>
  <meta name="description" content="<%= strip_html(page.content).replace(/^\s*/, '').replace(/\s*$/, '').substring(0, 150).replace(/[\n,\r]/g,'') %>">
  <% } else if (config.description){ %>
  <meta name="description" content="<%= config.description %>">
  <% } %>
  <% if (page.keywords){ %>
    <meta name="keywords" content="<%= page.keywords %>">
  <% } else if (page.tags){ %>
  <%
    var thistags=[];
    page.tags.each(function(k){ 
    thistags.push(k.name);
    }) %>
    <meta name="keywords" content="<%= thistags %>">
  <% } %>
- 先使用 description,然后 excerpt,然后内容,最后(空文章)才是站点全局的描述。
- 如果有 keywords,就写,否则使用 tags