-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add markdown viewer * fix "</body>" showing as markdown content for a few seconds in markdown viewer * fix indentation * replace markdown library 'uiw/react-markdown-preview' with 'marked' * update marked * add comment to explain removal of anchor links * fix formatting with pre-commit --------- Co-authored-by: Pamela Fox <[email protected]>
- Loading branch information
1 parent
4ce59be
commit 0a76219
Showing
6 changed files
with
164 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
app/frontend/src/components/MarkdownViewer/MarkdownViewer.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
.downloadButton { | ||
position: relative; | ||
float: right; | ||
} | ||
|
||
.markdownViewer { | ||
border-radius: 8px; | ||
box-shadow: | ||
#0000000d 0 0 0 1px, | ||
#0000001a 0 2px 3px; | ||
background-color: white; | ||
margin: 20px 0; | ||
} | ||
|
||
.loading { | ||
padding: 100px; | ||
height: 100vh; | ||
background-color: white; | ||
} | ||
|
||
.error { | ||
height: 100vh; | ||
background-color: white; | ||
} | ||
|
||
.markdown { | ||
padding: 30px; | ||
} | ||
|
||
table { | ||
border-collapse: collapse; | ||
} | ||
|
||
th, | ||
td { | ||
border: 1px solid #ddd; | ||
padding: 8px; | ||
} | ||
|
||
tr:nth-child(even) { | ||
background-color: #f6f8fa; | ||
} | ||
|
||
code { | ||
display: block; | ||
font-family: monospace; | ||
padding: 10px; | ||
background-color: #f6f8fa; | ||
} |
78 changes: 78 additions & 0 deletions
78
app/frontend/src/components/MarkdownViewer/MarkdownViewer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import { marked } from "marked"; | ||
import styles from "./MarkdownViewer.module.css"; | ||
import { Spinner, SpinnerSize, MessageBar, MessageBarType, Link, IconButton } from "@fluentui/react"; | ||
|
||
interface MarkdownViewerProps { | ||
src: string; | ||
} | ||
|
||
export const MarkdownViewer: React.FC<MarkdownViewerProps> = ({ src }) => { | ||
const [content, setContent] = useState<string>(""); | ||
const [isLoading, setIsLoading] = useState<boolean>(true); | ||
const [error, setError] = useState<Error | null>(null); | ||
|
||
/** | ||
* Anchor links are not handled well by 'marked' and result in HTTP 404 errors as the URL they point to does not exist. | ||
* This function removes them from the resulted HTML. | ||
*/ | ||
const removeAnchorLinks = (html: string) => { | ||
const ancorLinksRegex = /<a\s+(?:[^>]*?\s+)?href=['"](#[^"']*?)['"][^>]*?>/g; | ||
return html.replace(ancorLinksRegex, ""); | ||
}; | ||
|
||
useEffect(() => { | ||
const fetchMarkdown = async () => { | ||
try { | ||
const response = await fetch(src); | ||
|
||
if (!response.ok) { | ||
throw new Error("Failed loading markdown file."); | ||
} | ||
|
||
const markdownText = await response.text(); | ||
const parsedHtml = await marked.parse(markdownText); | ||
const cleanedHtml = removeAnchorLinks(parsedHtml); | ||
setContent(cleanedHtml); | ||
} catch (error: any) { | ||
setError(error); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
fetchMarkdown(); | ||
}, [src]); | ||
|
||
return ( | ||
<div> | ||
{isLoading ? ( | ||
<div className={`${styles.loading} ${styles.markdownViewer}`}> | ||
<Spinner size={SpinnerSize.large} label="Loading file" /> | ||
</div> | ||
) : error ? ( | ||
<div className={`${styles.error} ${styles.markdownViewer}`}> | ||
<MessageBar messageBarType={MessageBarType.error} isMultiline={false}> | ||
{error.message} | ||
<Link href={src} download> | ||
Download the file | ||
</Link> | ||
</MessageBar> | ||
</div> | ||
) : ( | ||
<div> | ||
<IconButton | ||
className={styles.downloadButton} | ||
style={{ color: "black" }} | ||
iconProps={{ iconName: "Save" }} | ||
title="Save" | ||
ariaLabel="Save" | ||
href={src} | ||
download | ||
/> | ||
<div className={`${styles.markdown} ${styles.markdownViewer}`} dangerouslySetInnerHTML={{ __html: content }} /> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./MarkdownViewer"; |