<head>
  <!-- 元数据 -->
  <meta charset="utf-8">
  <% if (theme.image.favicon) { %>
    <link rel="icon" href="<%- theme.image.favicon %>">
  <% } %>
  <%
  let title = page.title || __(page.title_i18n);
  if (is_archive()){
    title = __('title.archive');
    if (is_month()){
      title += ': ' + page.year + '-' + String(page.month).padStart(2, '0');
    } else if (is_year()){
      title += ': ' + page.year;
    }
  } else if (is_category()){
    title = __('title.category') + ': ' + page.category;
  } else if (is_tag()){
    title = __('title.tag') + ': ' + page.tag;
  } else if (is_post() && !title){
    title = __('title.status');
  }
  %>
  <title><% if (title) { %><%= title %> | <% } %><%= config.title %></title>
  <link rel="canonical" href="<%- full_url_for(page.path) %>">
  <meta name="author" content="<%- page.author || config.author  %>" />
  <meta http-equiv="Cache-Control" content="no-transform" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="robots" content="index,follow" />
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <meta name="format-detection" content="telphone=no, email=no" />
  <% if (is_post()) { %>
    <meta name="keywords" content="<%- strip_html(list_tags(page.tags, { show_count: false, style: 'none' })) %>" />
  <% } else { %>
    <meta name="keywords" content="<%- config.keywords %>" />
  <% } %>
  <%- open_graph({
    image: (function(){
      const cover = page.cover;
      if (cover) {
        if (/^(#|\/\/|http(s)?:)/.test(cover)) {
          return cover;
        }
        const coverPath = cover.startsWith('/') ? 
          cover : 
          page.path.replace(/(\/index)?\.html?$/, '') + '/' + cover;
        return full_url_for(coverPath);
      }
      return theme.image.favicon ? full_url_for(theme.image.favicon) : null;
    })(),
    // twitter_site: theme.contact.twitter ? "@" + theme.contact.twitter : null,
    title
  }) %>
  <% if (config.feed) { %>
    <%- feed_tag() %>
  <% } %>
  <!-- JSON-LD 结构化数据 -->
  <%
    let json_ld_data = null;
    if (is_post()) {
      const jsonLdDescription = page.description || strip_html(truncate(page.content, { length: 150 }));
      json_ld_data = {
        "@context": "https://schema.org",
        "@type": "BlogPosting",
        "headline": page.title,
        "description": jsonLdDescription,
        "image": page.cover ? full_url_for(page.path + page.cover) : (theme.image.avatar ? full_url_for(theme.image.avatar) : ""),
        "author": {
          "@type": "Person",
          "name": page.author || config.author
        },
        "publisher": {
          "@type": "Organization",
          "name": config.title,
          "logo": {
            "@type": "ImageObject",
            "url": theme.image.avatar ? full_url_for(theme.image.avatar) : ""
          }
        },
        "datePublished": page.date.toISOString(),
        "dateModified": (page.updated ? page.updated.toISOString() : page.date.toISOString()),
        "mainEntityOfPage": {
          "@type": "WebPage",
          "@id": full_url_for(page.path)
        }
      };
    } else if (is_home()) {
      json_ld_data = {
        "@context": "https://schema.org",
        "@type": "Blog",
        "name": config.title,
        "description": config.description,
        "url": config.url,
        "author": {
          "@type": "Person",
          "name": config.author
        }
      };
    }
  %>
  <% if (json_ld_data) { %>
    <script type="application/ld+json">
    <%- JSON.stringify(json_ld_data) %>
    </script>
  <% } %>
  <!-- 关键 CSS 预加载 -->
  <%- css_theme_cdn_preload('css/kr-core.min.css') %>
  <%- css_theme_cdn_preload('css/kr-theme/light.min.css') %>
  <%- css_theme_cdn_preload('css/kr-theme/dark.min.css') %>
  <!-- 样式表文件 -->
  <%- css_theme_cdn('css/kr-core.min.css', {id: "kratos-css", media:"all"}) %>
  <%- css_theme_cdn('css/kr-theme/light.min.css', {id: "theme-light-css", media:"all"}) %>
  <%- css_theme_cdn('css/kr-theme/dark.min.css', {id: "theme-dark-css", media:"(prefers-color-scheme: dark)"}) %>
  <% if (config.syntax_highlighter && theme.syntax_highlighter.theme) { %>
    <%- css_theme_cdn_async(`css/highlight/${config.syntax_highlighter}/${theme.syntax_highlighter.theme}.min.css`, {id: "highlight-css"}) %>
  <% } %>
  <%- js_theme_cdn('js/kr-theme.min.js') %>
  <%- css_npm_cdn_async('font-awesome', 'css/font-awesome.min.css', {id:"fontawe-css"}) %>
  <% if (theme.pjax || theme.search.provider === "local") { %>
    <%- css_npm_cdn_async('nprogress', 'nprogress.css', {id:"nprogress-css"}) %>
  <% } %>
  <% if (page.type !== '404' && theme.viewerjs) { %>
    <%- css_npm_cdn_async('viewerjs', 'dist/viewer.min.css') %>
  <% } %>
  <!-- 预先加载的JS文件 -->
  <% if (theme.share.enable || theme.donate.enable) { %>
    <%- js_npm_cdn('qr-code-styling', 'lib/qr-code-styling.js') %>
  <% } %>
  <!-- 自定义站点横幅和背景 -->
  <style>
    <% if (theme.image.banner) { %>
      .kratos-cover.kratos-cover-2 {
        background-image: url('<%- theme.image.banner.light %>');
      }
      html[data-theme="dark"] .kratos-cover.kratos-cover-2 {
        background-image: url('<%- theme.image.banner.dark %>');
      }
    <% } %>
    <% if (theme.image.background) { %>
      @media(min-width:768px) {
        body.custom-background {
          background-image: url('<%- theme.image.background.light %>');
        }
        html[data-theme="dark"] body.custom-background {
          background-image: url('<%- theme.image.background.dark %>');
        }
      }
    <% } %>
    <% if (theme.image.about) { %>
      .widget-kratos-about .photo-background {
        background-image: url('<%- theme.image.about.light %>');
      }
      html[data-theme="dark"] .widget-kratos-about .photo-background {
        background-image: url('<%- theme.image.about.dark %>');
      }
    <% } %>
  </style>

  <!-- 额外的追加注入项 -->
  <% if (theme.additional_injections.head) { %>
    <%- theme.additional_injections.head %>
  <% } %>
</head>
