Skip to content

Commit

Permalink
Common markdown component with common styling
Browse files Browse the repository at this point in the history
  • Loading branch information
forman committed Nov 29, 2024
1 parent 79b63a3 commit 44e3e52
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/components/DevRefPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const DevRefPage = ({ open, onClose }: DevRefPageProps) => {
return (
<MarkdownPage
title={i18n.get("Developer Reference")}
text={text}
text={text || ""}
open={open}
onClose={onClose}
/>
Expand Down
6 changes: 3 additions & 3 deletions src/components/LegalAgreementDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import { useContext, useEffect, useState } from "react";
import { Store } from "redux";
import { ReactReduxContext } from "react-redux";
import Markdown from "react-markdown";
import Button from "@mui/material/Button";
import Dialog from "@mui/material/Dialog";
import DialogActions from "@mui/material/DialogActions";
Expand All @@ -36,8 +35,9 @@ import CircularProgress from "@mui/material/CircularProgress";
import CheckIcon from "@mui/icons-material/Check";

import i18n from "@/i18n";
import { ControlState } from "@/states/controlState";
import { makeStyles } from "@/util/styles";
import { ControlState } from "@/states/controlState";
import Markdown from "@/components/Markdown";

const styles = makeStyles({
icon: (theme) => ({
Expand Down Expand Up @@ -108,7 +108,7 @@ export default function LegalAgreementDialog({
{markdownText === null ? (
<CircularProgress />
) : (
<Markdown children={markdownText} linkTarget="_blank" />
<Markdown text={markdownText} />
)}
</DialogContentText>
</DialogContent>
Expand Down
67 changes: 67 additions & 0 deletions src/components/Markdown.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019-2024 by the xcube development team and contributors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

import { useMemo } from "react";
import { useTheme } from "@mui/material";
import OriginalMarkdown from "react-markdown";

interface MarkdownProps {
text?: string;
}

export default function Markdown({ text }: MarkdownProps) {
const theme = useTheme();
const components = useMemo(
() => ({
a: (props: Record<string, unknown>) => {
const { node: _, ...rest } = props;
return (
<a
{...rest}
style={{
color: theme.palette.mode === "dark" ? "#90caf9" : "#1e90ff",
}}
/>
);
},
code: (props: Record<string, unknown>) => {
const { node: _, ...rest } = props;
return <code {...rest} style={{ color: "grey" }} />;
},
}),
[theme],
);

if (!text) {
return null;
}

return (
<OriginalMarkdown
children={text}
components={components}
linkTarget="_blank"
/>
);
}
4 changes: 2 additions & 2 deletions src/components/MarkdownPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/

import React from "react";
import Markdown from "react-markdown";
import Dialog from "@mui/material/Dialog";
import AppBar from "@mui/material/AppBar";
import Toolbar from "@mui/material/Toolbar";
Expand All @@ -36,6 +35,7 @@ import { TransitionProps } from "@mui/material/transitions";

import { makeStyles } from "@/util/styles";
import { styled } from "@mui/system";
import Markdown from "@/components/Markdown";

const styles = makeStyles({
dialog: (theme) => ({
Expand Down Expand Up @@ -104,7 +104,7 @@ const MarkdownPage: React.FC<MarkdownPageProps> = ({
</AppBar>
<DialogContent sx={styles.dialog}>
<StyledDiv>
<Markdown children={text} linkTarget="_blank" />
<Markdown text={text} />
</StyledDiv>
</DialogContent>
</Dialog>
Expand Down
30 changes: 3 additions & 27 deletions src/components/MarkdownPopover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
* SOFTWARE.
*/

import Markdown from "react-markdown";
import Popover from "@mui/material/Popover";
import Paper from "@mui/material/Paper";
import { useTheme } from "@mui/material";

import Markdown from "@/components/Markdown";

interface MarkdownPopoverProps {
anchorEl: HTMLElement | null;
Expand All @@ -40,29 +40,9 @@ export default function MarkdownPopover({
open,
onClose,
}: MarkdownPopoverProps) {
const theme = useTheme();
if (!markdownText) {
return null;
}

const components = {
a: (props: Record<string, unknown>) => {
const { node: _, ...rest } = props;
return (
<a
{...rest}
style={{
color: theme.palette.mode === "dark" ? "#90caf9" : "#1e90ff",
}}
/>
);
},
code: (props: Record<string, unknown>) => {
const { node: _, ...rest } = props;
return <code {...rest} style={{ color: "green" }} />;
},
};

return (
<Popover anchorEl={anchorEl} open={open} onClose={onClose}>
<Paper
Expand All @@ -74,11 +54,7 @@ export default function MarkdownPopover({
paddingRight: 2,
}}
>
<Markdown
children={markdownText}
components={components}
linkTarget="_blank"
/>
<Markdown text={markdownText} />
</Paper>
</Popover>
);
Expand Down
1 change: 1 addition & 0 deletions src/resources/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"allowRefresh": true,
"allowSharing": true,
"allowUserVariables": true,
"allowViewModePython": true,
"allow3D": true
}
}

0 comments on commit 44e3e52

Please sign in to comment.