From 7d28f911679d3ee3b0ab41c744862e788327ac0e Mon Sep 17 00:00:00 2001 From: spencerwooo Date: Wed, 30 Jun 2021 12:53:17 +0100 Subject: [PATCH] render readme files --- components/FileListing.tsx | 18 ++++++++++++++++++ components/previews/MarkdownPreview.tsx | 16 +++++++++------- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/components/FileListing.tsx b/components/FileListing.tsx index b6d8369..24e20a6 100644 --- a/components/FileListing.tsx +++ b/components/FileListing.tsx @@ -123,9 +123,16 @@ const FileListing: FunctionComponent<{ query?: ParsedUrlQuery }> = ({ query }) = if ('folder' in resp) { const { children } = resp + + // Image preview rendering preparations const imagesInFolder: ImageDecorator[] = [] const imageIndexDict: { [key: string]: number } = {} let imageIndex = 0 + + // README rendering preparations + let renderReadme = false + let readmeFile = null + children.forEach((c: any) => { if (fileIsImage(c.name)) { imagesInFolder.push({ @@ -136,6 +143,11 @@ const FileListing: FunctionComponent<{ query?: ParsedUrlQuery }> = ({ query }) = imageIndexDict[c.id] = imageIndex imageIndex += 1 } + + if (c.name.toLowerCase() === 'readme.md') { + renderReadme = true + readmeFile = c + } }) return ( @@ -202,6 +214,12 @@ const FileListing: FunctionComponent<{ query?: ParsedUrlQuery }> = ({ query }) = ))} + + {renderReadme && ( +
+ +
+ )} ) } diff --git a/components/previews/MarkdownPreview.tsx b/components/previews/MarkdownPreview.tsx index f865193..1228676 100644 --- a/components/previews/MarkdownPreview.tsx +++ b/components/previews/MarkdownPreview.tsx @@ -16,7 +16,7 @@ import DownloadBtn from '../DownloadBtn' const fetcher = (url: string) => axios.get(url).then(res => res.data) -const MarkdownPreview: FunctionComponent<{ file: any }> = ({ file }) => { +const MarkdownPreview: FunctionComponent<{ file: any; standalone?: boolean }> = ({ file, standalone = true }) => { const { data, error } = useSWR(file['@microsoft.graph.downloadUrl'], fetcher) useEffect(() => { if (typeof window !== 'undefined') { @@ -26,14 +26,14 @@ const MarkdownPreview: FunctionComponent<{ file: any }> = ({ file }) => { if (error) { return ( -
+
) } if (!data) { return ( -
+
) @@ -41,14 +41,16 @@ const MarkdownPreview: FunctionComponent<{ file: any }> = ({ file }) => { return ( <> -
+
{data}
-
- -
+ {standalone && ( +
+ +
+ )} ) }