20251116152149

{{/* 呼び出し例
  {{ partial "notes/sidebar-menu.html" (dict "Page" . "Site" .Site) }}
*/}}

{{ $moc := index (where .Site.RegularPages "Params.layout" "moc") 0 }}
{{ if not $moc }}
  <p class="p-4 text-neutral-500">No MOC found.</p>
  {{ return }}
{{ end }}

{{ partial "notes/moc-parser.html" (dict
    "raw"  $moc.RawContent
    "Page" .Page
    "Site" .Site
) }}
{{- $raw := .raw -}}
{{- $site := .Site -}}
{{- $current := .Page -}}

{{/* ----------------------------------------------
     1. ## セクションごとに抽出
------------------------------------------------ */}}
{{ $sections := findRE `(?ms)^##\s+(.+?)\n(.*?)(?=\n##|\z)` $raw }}

<ul class="space-y-4">

  {{ range $sections }}
    {{ $sectionTitle := index . 1 }}
    {{ $sectionBody  := index . 2 }}

    <li>
      <details open>
        <summary class="cursor-pointer text-neutral-700 font-medium py-1">
          {{ $sectionTitle }}
        </summary>

        {{/* ----------------------------------------------
             2. セクション内のリンク - [text](target)
        ------------------------------------------------ */}}
        {{ $links := findRE `- $begin:math:display$\(\[\^$end:math:display$]+)\]$begin:math:text$\(\[\^\)\]\+\)$end:math:text$` $sectionBody }}

        <ul class="mt-1 ml-4 space-y-1">
          {{ range $links }}
            {{ $text := index . 1 }}
            {{ $target := index . 2 }}

            {{/* .md を除去して BaseFileName と比較 */}}
            {{ $base := replaceRE `\.md$` "" $target }}

            {{/* RegularPages からプレーン検索 */}}
            {{ $page := index (where $site.RegularPages "File.BaseFileName" $base) 0 }}

            {{ if $page }}
              <li>
                <a href="{{ $page.RelPermalink }}"
                   class="block py-1 pl-2 rounded
                     {{ if eq $current.RelPermalink $page.RelPermalink }}
                       font-semibold text-neutral-900 bg-neutral-100
                     {{ else }}
                       text-neutral-600 hover:text-neutral-900
                     {{ end }}">
                  {{ $text }}
                </a>
              </li>
            {{ else }}
              <li class="text-xs text-red-500">Not found: {{ $target }}</li>
            {{ end }}

          {{ end }}
        </ul>
      </details>
    </li>

  {{ end }}

</ul>
{{ $matches := findRESubmatch $re .Page.RawContent }}

{{ range $matches }}

{{ $linkURL := index . 2 }}

{{ $page := index (where site.RegularPages "File.BaseFileName" $linkURL) 0 }}

{{ $page.RelPermalink }}

{{ with $page }}

$isOpen =

{{ or $isOpen (eq .RelPermalink $currentPath) }}

{{ end }}

{{ end }}