render readme files

This commit is contained in:
spencerwooo
2021-06-30 12:53:17 +01:00
parent 6fc114f16c
commit 7d28f91167
2 changed files with 27 additions and 7 deletions
+18
View File
@@ -123,9 +123,16 @@ const FileListing: FunctionComponent<{ query?: ParsedUrlQuery }> = ({ query }) =
if ('folder' in resp) { if ('folder' in resp) {
const { children } = resp const { children } = resp
// Image preview rendering preparations
const imagesInFolder: ImageDecorator[] = [] const imagesInFolder: ImageDecorator[] = []
const imageIndexDict: { [key: string]: number } = {} const imageIndexDict: { [key: string]: number } = {}
let imageIndex = 0 let imageIndex = 0
// README rendering preparations
let renderReadme = false
let readmeFile = null
children.forEach((c: any) => { children.forEach((c: any) => {
if (fileIsImage(c.name)) { if (fileIsImage(c.name)) {
imagesInFolder.push({ imagesInFolder.push({
@@ -136,6 +143,11 @@ const FileListing: FunctionComponent<{ query?: ParsedUrlQuery }> = ({ query }) =
imageIndexDict[c.id] = imageIndex imageIndexDict[c.id] = imageIndex
imageIndex += 1 imageIndex += 1
} }
if (c.name.toLowerCase() === 'readme.md') {
renderReadme = true
readmeFile = c
}
}) })
return ( return (
@@ -202,6 +214,12 @@ const FileListing: FunctionComponent<{ query?: ParsedUrlQuery }> = ({ query }) =
<FileListItem fileContent={c} /> <FileListItem fileContent={c} />
</div> </div>
))} ))}
{renderReadme && (
<div className="border-t">
<MarkdownPreview file={readmeFile} standalone={false} />
</div>
)}
</div> </div>
) )
} }
+9 -7
View File
@@ -16,7 +16,7 @@ import DownloadBtn from '../DownloadBtn'
const fetcher = (url: string) => axios.get(url).then(res => res.data) 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) const { data, error } = useSWR(file['@microsoft.graph.downloadUrl'], fetcher)
useEffect(() => { useEffect(() => {
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
@@ -26,14 +26,14 @@ const MarkdownPreview: FunctionComponent<{ file: any }> = ({ file }) => {
if (error) { if (error) {
return ( return (
<div className="shadow bg-white rounded p-3"> <div className={`${standalone ? 'shadow bg-white rounded p-3' : ''}`}>
<FourOhFour errorMsg={error.message} /> <FourOhFour errorMsg={error.message} />
</div> </div>
) )
} }
if (!data) { if (!data) {
return ( return (
<div className="shadow bg-white rounded p-3"> <div className={standalone ? 'shadow bg-white rounded p-3' : ''}>
<Loading loadingText="Loading file content..." /> <Loading loadingText="Loading file content..." />
</div> </div>
) )
@@ -41,14 +41,16 @@ const MarkdownPreview: FunctionComponent<{ file: any }> = ({ file }) => {
return ( return (
<> <>
<div className="markdown-body shadow bg-white rounded p-3"> <div className={standalone ? 'markdown-body shadow bg-white rounded p-3' : 'markdown-body p-3'}>
<ReactMarkdown remarkPlugins={[gfm, remarkMath]} rehypePlugins={[rehypeKatex]}> <ReactMarkdown remarkPlugins={[gfm, remarkMath]} rehypePlugins={[rehypeKatex]}>
{data} {data}
</ReactMarkdown> </ReactMarkdown>
</div> </div>
<div className="mt-4"> {standalone && (
<DownloadBtn downloadUrl={file['@microsoft.graph.downloadUrl']} /> <div className="mt-4">
</div> <DownloadBtn downloadUrl={file['@microsoft.graph.downloadUrl']} />
</div>
)}
</> </>
) )
} }