前回のつづき
前回1階層限定のギャラリーを作成したがコンポーネントからコンポーネントを呼ぶ再帰でディレクトリ階層を調べることができることが分かり、getStaticPathsを使わないギャラリーが完成した。
ギャラリー
サイトのギャラリー
ディレクトリリスト及び画像表示コンポーネント
例によってスタイルシートは省略。
src/components/ImageList.astro
---import { getImageFiles, getDirectories } from '@utils/fileUtil.ts';import ImageList from '@components/ImageList.astro';import MyImage from "@components/MyImage.astro";import { LOCAL_IMAGE_ROOT, WEB_IMAGE_ROOT } from '@config/config';const { currentDir } = Astro.props;const localDir = currentDir + "/";const thumbnailSize = "96px";const subDir = (() => {try { return getDirectories(localDir);}catch(e) { console.log(e) return [];}})();const imageSrc: string[] = [];const imageFiles = (() => { try { return getImageFiles(localDir); } catch (e) { console.log(e); return [""]; }})();imageFiles.reverse();for (const s of imageFiles) { const imageUrl = WEB_IMAGE_ROOT + localDir.replace(LOCAL_IMAGE_ROOT, "") + s; imageSrc.push(imageUrl); console.log(imageUrl);}--- { imageSrc.map((f) => ( )) }{subDir.map((dir) => ( {dir} { } ))}LOCAL_IMAGE_ROOTは起点になるディレクトリWEB_IMAGE_ROOTはウェブ上の画像の起点になるディレクトリ
subDir.mapの中でImageListをまた呼び出している。
このループでサブディレクトリすべてを表示する。
画像はsubDirのループ前に現在のディレクトリであるlocalDirにある画像をすべて表示している。
表示ページ
対象ディレクトリ(LOCAL_IMAGE_ROOT)をcurrentDirに指定している。
---import BaseLayout from "@layouts/BaseLayout.astro";import ImageList from "@components/ImageList.astro";import { LOCAL_IMAGE_ROOT } from "@config/config";--- 画像一覧 最後に
getStaticPathsでやる方法だとこのリスト方式だとリンクの問題があるので不可能なのではないかと思った。
パスを作ること自体は出来そうではある。
