<% const ksio_seo = theme.seo || {} %>
<% const ksio_meta = theme.meta || {} %>
<% const ksio_master = theme.master || {} %>
<% const seo_url = config.url %>
<% let seo_site_title = config.title %>
<% const seo_title_suffix = ` - ${seo_site_title}` %>
<% let seo_title %>
<% let seo_page_title %>
<% if (page.ksio_seo_title) { %>
  <% seo_title = page.ksio_seo_title %>
  <% seo_page_title = seo_title %>
  <% if (ksio_seo.suffix !== false) { %>
    <% seo_title = `${seo_title}${seo_title_suffix}` %>
  <% } %>
<% } else if (page.title) { %>
  <% seo_title = page.title %>
  <% seo_page_title = seo_title %>
  <% if (ksio_seo.suffix !== false) { %>
    <% seo_title = `${seo_title}${seo_title_suffix}` %>
  <% } %>
<% } else { %>
  <% if (page.archive) { %>
    <% seo_title = `发表于 ${page.year} 年的文章` %>
    <% seo_page_title = seo_title %>
  <% } else if (seo_site_title) { %>
    <% seo_title = seo_site_title %>
    <% seo_page_title = seo_site_title %>

    <% if (config.subtitle) { %>
      <% seo_title = `${seo_title} - ${config.subtitle}` %>
    <% } %>
  <% } %>
<% } %>
<% if (seo_title) { %>
  <% seo_title = escape_html(trim(strip_html(markdown(seo_title)))) %>
<% } %>
<% if (seo_site_title) { %>
  <% seo_site_title = escape_html(trim(strip_html(markdown(seo_site_title)))) %>
<% } %>
<% if (seo_page_title) { %>
  <% seo_page_title = escape_html(trim(strip_html(markdown(seo_page_title)))) %>
<% } %>
<% let seo_description = page.title ? (page.description || page.excerpt || config.description) : config.description %>
<% const role_type = page.ksio_seo_role %>
<% const master_name = ksio_master.name || config.author %>
<% if (role_type === 'writer') { %>
  <% const posts = site.posts.data || site.posts %>
  <% const first_post_year = date(posts.slice(-1)[0].date, 'YYYY') %>
  <% const last_post_year = date(posts[0].date, 'YYYY') %>
  <% const year_diff = last_post_year - first_post_year %>
  <% seo_description = `${master_name}是一个写作爱好者，从 ${first_post_year} 年起 ${year_diff} 年间共写了 ${posts.length} 篇文章。` %>
<% } else if (role_type === 'developer') { %>
  <% seo_description = `${master_name}是一个码农，写了 ${site.data['local/github'].all.length} 个托管于 GitHub 的开源项目，在 CodePen 上建立了 ${site.data['local/codepen'].length} 个代码演示。` %>
<% } %>
<% if (seo_description) { %>
  <% seo_description = escape_html(trim(strip_html(markdown(seo_description)))) %>
<% } %>
<% let seo_author_name %>
<% let seo_author_twitter %>
<% if (page.author) { %>
  <% seo_author_name = page.author ? (page.author.name || page.author) : master_name %>
  <% seo_author_twitter = page.author ? (page.author.twitter || page.author) : '' %>
<% } %>
<% if (seo_title) { %>
  <title><%= seo_title %></title>
<% } %>
<% if (seo_page_title) { %>
  <meta property="og:title" content="<%= seo_page_title %>">
<% } %>
<% if (seo_description) { %>
  <meta name="description" content="<%= seo_description %>">
  <meta property="og:description" content="<%= seo_description %>">
<% } %>
<% let keywords %>
<% if (page.keywords) { %>
  <% keywords = page.keywords %>
<% } else { %>
  <% if (!keywords) { %>
    <% keywords = config.keywords %>
  <% } %>
<% } %>
<% if (keywords) { %>
  <meta name="keywords" content="<%= keywords %>">
<% } %>
