---
// ガイドのページ（/guide/{slug}。2026-11-15）
// LOG とは別の、検索から来る人向けに版が変わるたび書き直す記事。LOG 番号は付けず、記録の一覧には並べない。
// 見た目は記事ページ（src/styles/record.css）を使う。日付は「最終更新」を前に出す
import Base from '../../layouts/Base.astro';
import { ymd } from '../../lib/format';
import { abs, AUTHOR, PUBLISHER, SITE, SITE_NAME, textOf } from '../../lib/seo';
import { allCards } from '../../lib/records';
import { allGuides } from '../../lib/guides';
import { getCollection, render } from 'astro:content';
import { existsSync } from 'node:fs';
import { linkifyHtml, linkLogsHtml } from '../../lib/links.mjs';
import { roomyTables } from '../../lib/tables.mjs';
import '../../styles/record.css';
import GuideCard from '../../components/GuideCard.astro';

export async function getStaticPaths() {
  return (await getCollection('guides')).map((e) => ({ params: { slug: e.data.slug }, props: { entry: e } }));
}
const { entry } = Astro.props;
const d = entry.data;
const esc = (s) => s.replace(/&/g, '&amp;').replace(/</g, '&lt;');
const rendered = await render(entry);
const heads = rendered.headings.filter((h) => h.depth === 2);
const toc = heads.length ? `<nav class="toc"><ul>${heads.map((t) => `<li><a href="#${t.slug}">${esc(t.text)}</a></li>`).join('')}</ul></nav>` : '';
let body = entry.rendered?.html || '';
const at = toc ? body.search(/<h2[\s>]/) : -1;
body = !toc ? body : at >= 0 ? body.slice(0, at) + toc + body.slice(at) : toc + body;
const LOGS = Object.fromEntries((await allCards()).map((c) => [c.day, c.slug]));
body = roomyTables(linkifyHtml(linkLogsHtml(body, LOGS, d.slug, false, true, false)));

const path = `/guide/${d.slug}`;
const url = SITE + path;
const published = d.published.toISOString();
const updated = d.updated.toISOString();
const description = d.meta_description || textOf(entry.body, 120);
// SNS・検索の画像：HTML のサムネイルは書き出した /og/…jpg、画像のサムネイルは同じ名前の .jpg（webp を読めない SNS があるため）
const ogFromThumb = !d.thumb ? null : /\.html$/.test(d.thumb) ? d.thumb.replace(/^\/thumbs\//, '/og/').replace(/\.html$/, '.jpg') : d.thumb.replace(/\.webp$/, '.jpg');
const ogImage = ogFromThumb && existsSync(`public${ogFromThumb}`) ? ogFromThumb : undefined;
const others = (await allGuides()).filter((g) => g.slug !== d.slug);
const jsonLd = [
  {
    '@context': 'https://schema.org', '@type': 'Article',
    headline: d.title, description, url, mainEntityOfPage: url, inLanguage: 'ja',
    datePublished: published, dateModified: updated,
    image: ogImage ? [abs(ogImage)] : undefined,
    articleSection: 'ガイド', keywords: d.tags?.length ? d.tags.join(', ') : undefined,
    author: AUTHOR, publisher: PUBLISHER,
    isPartOf: { '@type': 'WebSite', name: SITE_NAME, url: SITE + '/' },
  },
  {
    '@context': 'https://schema.org', '@type': 'BreadcrumbList',
    itemListElement: [
      { '@type': 'ListItem', position: 1, name: SITE_NAME, item: SITE + '/' },
      { '@type': 'ListItem', position: 2, name: 'ガイド', item: `${SITE}/guide` },
      { '@type': 'ListItem', position: 3, name: d.title, item: url },
    ],
  },
];
---
<Base title={`${d.title} | ${SITE_NAME}`} description={description} ogImage={ogImage} path={path}
  ogType="article" published={published} modified={updated} jsonLd={jsonLd}>
  <article class="rec guide">
    <nav class="crumbs" aria-label="現在地">
      <a href="/">{SITE_NAME}</a><span aria-hidden="true">›</span><a href="/guide">ガイド</a>
    </nav>
    {d.thumb && (/\.html$/.test(d.thumb) ? <div class="hero"><iframe src={d.thumb} sandbox="allow-scripts" title={d.title} scrolling="no" tabindex="-1"></iframe></div> : <img class="hero-img" src={d.thumb} alt={d.title} width="1920" height="1080" />)}
    <div class="meta"><a class="tag" href="/guide">ガイド</a><span class="date">最終更新 {ymd(updated)}</span>{updated !== published && <span class="date">公開 {ymd(published)}</span>}</div>
    <h1 class="title">{d.title}</h1>
    <div class="richText" set:html={body} />
  </article>
  {others.length > 0 && (
    <section class="related">
      <div class="panel">
        <h2 class="sec-h">ほかのガイド | Guide</h2>
        <div class="grid3">{others.map((g) => <GuideCard item={g} />)}</div>
        <div class="rel-more"><a class="btn-dark" href="/guide">ガイドをすべて見る<i class="ms">arrow_forward</i></a></div>
      </div>
    </section>
  )}
</Base>
<style is:global>
  .guide .meta .date + .date::before { content: "・"; margin-right: 4px; }
</style>
