Skip to content

Commit 665b4eb

Browse files
committed
Refactor OverviewMarkdown component to improve content loading and rendering logic
1 parent 45cebc6 commit 665b4eb

File tree

1 file changed

+31
-41
lines changed
  • portals/devportal/src/main/webapp/source/src/app/components/Apis/Details/Documents

1 file changed

+31
-41
lines changed

portals/devportal/src/main/webapp/source/src/app/components/Apis/Details/Documents/OverviewMarkdown.jsx

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -37,85 +37,75 @@ const classes = {
3737
root: `${PREFIX}-root`,
3838
};
3939

40-
const Root = styled('div')((
41-
{
42-
theme,
43-
},
44-
) => ({
40+
const Root = styled('div')(({ theme }) => ({
4541
[`& .${classes.root}`]: {
46-
paddingTop: theme.spacing(2),
4742
paddingBottom: theme.spacing(2),
43+
paddingTop: theme.spacing(2),
4844
},
4945
}));
5046

5147
const ReactMarkdown = lazy(() => import('react-markdown' /* webpackChunkName: "MDReactMarkdown" */));
5248

5349
function OverviewMarkdown({ apiId }) {
5450
const { api } = useContext(ApiContext);
55-
const [code, setCode] = useState('');
51+
const [content, setContent] = useState('');
5652
const restAPI = new API();
5753
const { skipHtml, syntaxHighlighterProps = {}, syntaxHighlighterDarkTheme } = Settings.app.markdown;
5854

59-
const loadContentForDoc = () => {
55+
useEffect(() => {
6056
restAPI.getMarkdownContentOfAPI(apiId)
61-
.then((dataDoc) => {
62-
let { text } = dataDoc;
63-
Object.keys(api).forEach((fieldName) => {
64-
// eslint-disable-next-line no-useless-escape
65-
const regex = new RegExp('\_\_\_' + fieldName + '\_\_\_', 'g');
66-
text = text.replace(regex, api[fieldName]);
67-
});
68-
setCode(text);
57+
.then(({ text }) => {
58+
const updatedText = Object.entries(api).reduce((acc, [fieldName, value]) => {
59+
return acc.replace(new RegExp(`___${fieldName}___`, 'g'), value);
60+
}, text);
61+
setContent(updatedText);
6962
})
7063
.catch((error) => {
7164
if (process.env.NODE_ENV !== 'production') {
72-
console.log(error);
65+
console.error(error);
7366
}
7467
});
75-
};
76-
77-
useEffect(() => {
78-
loadContentForDoc();
7968
}, [apiId]);
8069

8170
return (
8271
<Root>
8372
<div className='markdown-content-wrapper'>
8473
<Suspense fallback={<CircularProgress />}>
8574
<ReactMarkdown
86-
skipHtml={skipHtml}
87-
children={code}
8875
remarkPlugins={[remarkGfm]}
76+
skipHtml={skipHtml}
8977
components={{
9078
code({
91-
node, inline, className, children, ...propsInner
79+
inline, className = '', children, ...propsInner
9280
}) {
93-
const match = /language-(\w+)/.exec(className || '');
94-
return !inline && match ? (
95-
<SyntaxHighlighter
96-
children={String(children).replace(/\n$/, '')}
97-
style={syntaxHighlighterDarkTheme ? vscDarkPlus : vs}
98-
language={match[1]}
99-
PreTag='div'
100-
{...propsInner}
101-
{...syntaxHighlighterProps}
102-
/>
103-
) : (
104-
<code className={className} {...propsInner}>
105-
{children}
106-
</code>
107-
);
81+
const match = /language-(\w+)/.exec(className);
82+
const codeContent = String(children).replace(/\n$/, '');
83+
if (inline || !match) {
84+
return (
85+
<code className={className} {...propsInner}>{children}</code>
86+
);
87+
}
88+
89+
const syntaxProps = {
90+
style: syntaxHighlighterDarkTheme ? vscDarkPlus : vs,
91+
language: match[1],
92+
PreTag: 'div',
93+
...propsInner,
94+
...syntaxHighlighterProps,
95+
};
96+
return <SyntaxHighlighter {...syntaxProps}>{codeContent}</SyntaxHighlighter>;
10897
},
10998
}}
110-
/>
99+
>
100+
{content}
101+
</ReactMarkdown>
111102
</Suspense>
112103
</div>
113104
</Root>
114105
);
115106
}
116107

117108
OverviewMarkdown.propTypes = {
118-
classes: PropTypes.shape({}).isRequired,
119109
apiId: PropTypes.string.isRequired,
120110
};
121111

0 commit comments

Comments
 (0)