From ea4339b2dc20e630c859e1a87182f18b701ddc2e Mon Sep 17 00:00:00 2001 From: monoid Date: Fri, 6 Jan 2023 22:29:48 +0900 Subject: [PATCH] add readme candidate order --- routes/dir/[...path].tsx | 26 ++++++++++++++++++++------ test_data/e/readme.txt | 1 + test_data/g/readme.txt | 1 + 3 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 test_data/e/readme.txt create mode 100644 test_data/g/readme.txt diff --git a/routes/dir/[...path].tsx b/routes/dir/[...path].tsx index fad4f6d..bbe0b01 100644 --- a/routes/dir/[...path].tsx +++ b/routes/dir/[...path].tsx @@ -143,9 +143,16 @@ function isImageFile(path: string) { return /\.(jpg|jpeg|png|gif|webp|svg|bmp|ico|tiff)$/i.test(path); } -function searchFiles(path: EntryInfo[], fn: (path: EntryInfo) => boolean) { +function searchFiles( + path: EntryInfo[], + fn: (path: EntryInfo) => boolean, + opt: { order?: (a: EntryInfo, b: EntryInfo) => number } = {}, +) { const candiate = path.filter(fn); if (candiate.length > 0) { + if (opt.order) { + candiate.sort(opt.order); + } return candiate[0]; } return null; @@ -157,16 +164,24 @@ export default function DirLists(props: PageProps) { if (data.type === "dir") { cover = searchFiles(data.files, (f) => isImageFile(f.name)); index = searchFiles(data.files, (f) => f.name === "index.html"); - const contentFilenameCandidate = new Set([ + const contentFilenameCandidate = [ "SUMMARY.md", "README.md", "readme.md", "README.txt", "readme.txt", - ]); + ]; + const contentFilenameCandidateSet = new Set(contentFilenameCandidate); content = searchFiles( data.files, - (f) => contentFilenameCandidate.has(f.name), + (f) => contentFilenameCandidateSet.has(f.name), + { + order: (a, b) => { + const aIndex = contentFilenameCandidate.indexOf(a.name); + const bIndex = contentFilenameCandidate.indexOf(b.name); + return (aIndex - bIndex); + }, + }, ); } return ( @@ -205,8 +220,7 @@ export default function DirLists(props: PageProps) { > - + /> ) : null} diff --git a/test_data/e/readme.txt b/test_data/e/readme.txt new file mode 100644 index 0000000..adea80c --- /dev/null +++ b/test_data/e/readme.txt @@ -0,0 +1 @@ +Readme \ No newline at end of file diff --git a/test_data/g/readme.txt b/test_data/g/readme.txt new file mode 100644 index 0000000..adea80c --- /dev/null +++ b/test_data/g/readme.txt @@ -0,0 +1 @@ +Readme \ No newline at end of file