|
| 1 | +/* eslint-disable react/no-children-prop */ |
| 2 | +/* |
| 3 | + * Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com). |
| 4 | + * |
| 5 | + * WSO2 LLC. licenses this file to you under the Apache License, |
| 6 | + * Version 2.0 (the "License"); you may not use this file except |
| 7 | + * in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import React, { |
| 21 | + useState, useEffect, useContext, Suspense, lazy, |
| 22 | +} from 'react'; |
| 23 | +import { styled } from '@mui/material/styles'; |
| 24 | +import PropTypes from 'prop-types'; |
| 25 | +import { injectIntl } from 'react-intl'; |
| 26 | +import API from 'AppData/api'; |
| 27 | +import Settings from 'Settings'; |
| 28 | +import remarkGfm from 'remark-gfm'; |
| 29 | +import CircularProgress from '@mui/material/CircularProgress'; |
| 30 | +import { vscDarkPlus, vs } from 'react-syntax-highlighter/dist/esm/styles/prism'; |
| 31 | +import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; |
| 32 | +import { ApiContext } from '../ApiContext'; |
| 33 | + |
| 34 | +const PREFIX = 'OverviewMarkdown'; |
| 35 | + |
| 36 | +const classes = { |
| 37 | + root: `${PREFIX}-root`, |
| 38 | +}; |
| 39 | + |
| 40 | +const Root = styled('div')(( |
| 41 | + { |
| 42 | + theme, |
| 43 | + }, |
| 44 | +) => ({ |
| 45 | + [`& .${classes.root}`]: { |
| 46 | + paddingTop: theme.spacing(2), |
| 47 | + paddingBottom: theme.spacing(2), |
| 48 | + }, |
| 49 | +})); |
| 50 | + |
| 51 | +const ReactMarkdown = lazy(() => import('react-markdown' /* webpackChunkName: "MDReactMarkdown" */)); |
| 52 | + |
| 53 | +function OverviewMarkdown({ apiId }) { |
| 54 | + const { api } = useContext(ApiContext); |
| 55 | + const [code, setCode] = useState(''); |
| 56 | + const restAPI = new API(); |
| 57 | + const { skipHtml, syntaxHighlighterProps = {}, syntaxHighlighterDarkTheme } = Settings.app.markdown; |
| 58 | + |
| 59 | + const loadContentForDoc = () => { |
| 60 | + 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); |
| 69 | + }) |
| 70 | + .catch((error) => { |
| 71 | + if (process.env.NODE_ENV !== 'production') { |
| 72 | + console.log(error); |
| 73 | + } |
| 74 | + }); |
| 75 | + }; |
| 76 | + |
| 77 | + useEffect(() => { |
| 78 | + loadContentForDoc(); |
| 79 | + }, [apiId]); |
| 80 | + |
| 81 | + return ( |
| 82 | + <Root> |
| 83 | + <div className='markdown-content-wrapper'> |
| 84 | + <Suspense fallback={<CircularProgress />}> |
| 85 | + <ReactMarkdown |
| 86 | + skipHtml={skipHtml} |
| 87 | + children={code} |
| 88 | + remarkPlugins={[remarkGfm]} |
| 89 | + components={{ |
| 90 | + code({ |
| 91 | + node, inline, className, children, ...propsInner |
| 92 | + }) { |
| 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 | + ); |
| 108 | + }, |
| 109 | + }} |
| 110 | + /> |
| 111 | + </Suspense> |
| 112 | + </div> |
| 113 | + </Root> |
| 114 | + ); |
| 115 | +} |
| 116 | + |
| 117 | +OverviewMarkdown.propTypes = { |
| 118 | + classes: PropTypes.shape({}).isRequired, |
| 119 | + apiId: PropTypes.string.isRequired, |
| 120 | +}; |
| 121 | + |
| 122 | +export default injectIntl((OverviewMarkdown)); |
0 commit comments