add readme candidate order

This commit is contained in:
monoid 2023-01-06 22:29:48 +09:00
parent 2f91fb771e
commit ea4339b2dc
3 changed files with 22 additions and 6 deletions

View File

@ -143,9 +143,16 @@ function isImageFile(path: string) {
return /\.(jpg|jpeg|png|gif|webp|svg|bmp|ico|tiff)$/i.test(path); 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); const candiate = path.filter(fn);
if (candiate.length > 0) { if (candiate.length > 0) {
if (opt.order) {
candiate.sort(opt.order);
}
return candiate[0]; return candiate[0];
} }
return null; return null;
@ -157,16 +164,24 @@ export default function DirLists(props: PageProps<DirOrFileProps>) {
if (data.type === "dir") { if (data.type === "dir") {
cover = searchFiles(data.files, (f) => isImageFile(f.name)); cover = searchFiles(data.files, (f) => isImageFile(f.name));
index = searchFiles(data.files, (f) => f.name === "index.html"); index = searchFiles(data.files, (f) => f.name === "index.html");
const contentFilenameCandidate = new Set([ const contentFilenameCandidate = [
"SUMMARY.md", "SUMMARY.md",
"README.md", "README.md",
"readme.md", "readme.md",
"README.txt", "README.txt",
"readme.txt", "readme.txt",
]); ];
const contentFilenameCandidateSet = new Set(contentFilenameCandidate);
content = searchFiles( content = searchFiles(
data.files, 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 ( return (
@ -205,8 +220,7 @@ export default function DirLists(props: PageProps<DirOrFileProps>) {
> >
<RenderView <RenderView
src={`/dir/${encodePath(join(data.path, content.name))}`} src={`/dir/${encodePath(join(data.path, content.name))}`}
> />
</RenderView>
</div> </div>
) )
: null} : null}

1
test_data/e/readme.txt Normal file
View File

@ -0,0 +1 @@
Readme

1
test_data/g/readme.txt Normal file
View File

@ -0,0 +1 @@
Readme