1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-08-17 14:37:58 +00:00
seahub/frontend/src/components/seafile-markdown-viewer/index.js

134 lines
4.4 KiB
JavaScript
Raw Normal View History

2024-03-20 23:45:10 +00:00
import React, { Suspense } from 'react';
2019-01-15 11:58:25 +00:00
import PropTypes from 'prop-types';
2024-01-30 01:45:46 +00:00
import { MarkdownViewer } from '@seafile/seafile-editor';
2024-03-20 23:45:10 +00:00
import { I18nextProvider } from 'react-i18next';
import i18n from '../../_i18n/i18n-seafile-editor';
import { gettext, mediaUrl, serviceURL, sharedToken, slug } from '../../utils/constants';
import { Utils } from '../../utils/utils';
import Loading from '../loading';
import './style.css';
2019-01-15 11:58:25 +00:00
const propTypes = {
isWiki: PropTypes.bool,
path: PropTypes.string,
repoID: PropTypes.string,
isTOCShow: PropTypes.bool,
2019-01-15 11:58:25 +00:00
children: PropTypes.object,
isFileLoading: PropTypes.bool.isRequired,
containerClassName: PropTypes.string,
2019-01-15 11:58:25 +00:00
markdownContent: PropTypes.string.isRequired,
latestContributor: PropTypes.string.isRequired,
lastModified: PropTypes.string.isRequired,
2019-01-24 07:41:01 +00:00
onLinkClick: PropTypes.func.isRequired,
2019-01-15 11:58:25 +00:00
};
class SeafileMarkdownViewer extends React.Component {
2019-01-15 11:58:25 +00:00
constructor(props) {
super(props);
2023-12-08 03:20:12 +00:00
this.scrollRef = React.createRef();
2019-01-15 11:58:25 +00:00
}
2024-01-30 01:45:46 +00:00
onLinkClick = (link) => {
2019-01-15 11:58:25 +00:00
this.props.onLinkClick(link);
};
2019-01-15 11:58:25 +00:00
2019-01-24 07:41:01 +00:00
changeInlineNode = (item) => {
const { repoID } = this.props;
2021-04-27 03:22:55 +00:00
let url, imagePath;
// isPublicWiki: in the old version, only public wiki need replace image url
if (item.type == 'image') { // change image url
2021-04-27 03:22:55 +00:00
url = item.data.src;
const re = new RegExp(serviceURL + '/lib/' + repoID +'/file.*raw=1');
// different repo
if (re.test(url)) {
// get image path
let index = url.indexOf('/file');
let index2 = url.indexOf('?');
imagePath = url.substring(index + 5, index2);
} else if (/^\.\.\/*/.test(url) || /^\.\/*/.test(url)) {
const path = this.props.path;
const originalPath = path.slice(0, path.lastIndexOf('/')) + '/' + url;
imagePath = Utils.pathNormalize(originalPath);
} else {
return;
}
2021-04-27 03:22:55 +00:00
item.data.src = serviceURL + '/view-image-via-public-wiki/?slug=' + slug + '&path=' + imagePath;
2021-04-27 03:28:12 +00:00
} else if (item.type == 'link') { // change link url
2023-12-08 03:20:12 +00:00
url = item.url;
2021-04-27 03:22:55 +00:00
if (Utils.isInternalFileLink(url, repoID)) { // change file url
if (Utils.isInternalMarkdownLink(url, repoID)) {
let path = Utils.getPathFromInternalMarkdownLink(url, repoID);
2019-01-24 07:41:01 +00:00
// replace url
2023-12-08 03:20:12 +00:00
item.url = serviceURL + '/published/' + slug + path;
2021-04-27 03:22:55 +00:00
} else {
2023-12-08 03:20:12 +00:00
item.url = url.replace(/(.*)lib\/([-0-9a-f]{36})\/file(.*)/g, (match, p1, p2, p3) => {
2021-04-27 03:22:55 +00:00
return `${p1}d/${sharedToken}/files/?p=${p3}&dl=1`;
});
}
2021-04-27 03:22:55 +00:00
} else if (Utils.isInternalDirLink(url, repoID)) { // change dir url
let path = Utils.getPathFromInternalDirLink(url, repoID);
// replace url
2023-12-08 03:20:12 +00:00
item.url = serviceURL + '/published/' + slug + path;
2019-01-24 07:41:01 +00:00
}
}
return item;
};
2019-01-24 07:41:01 +00:00
modifyValueBeforeRender = (value) => {
let newNodes = Utils.changeMarkdownNodes(value, this.changeInlineNode);
return newNodes;
};
2019-01-24 07:41:01 +00:00
renderMarkdown = () => {
2023-12-08 03:20:12 +00:00
const { isTOCShow = true, isWiki, markdownContent } = this.props;
const props = {
isShowOutline: isTOCShow,
mathJaxSource: `${mediaUrl}js/mathjax/tex-svg.js`,
value: markdownContent,
scrollRef: this.scrollRef,
2024-01-30 01:45:46 +00:00
onLinkClick: this.onLinkClick,
2023-12-08 03:20:12 +00:00
...(isWiki && {beforeRenderCallback: this.modifyValueBeforeRender})
};
2019-01-24 07:41:01 +00:00
2024-03-20 23:45:10 +00:00
return (
<I18nextProvider i18n={ i18n }>
<Suspense fallback={<Loading />}>
<MarkdownViewer {...props} />
</Suspense>
</I18nextProvider>
);
};
2019-01-24 07:41:01 +00:00
2019-01-15 11:58:25 +00:00
render() {
if (this.props.isFileLoading) {
return <Loading />;
2019-01-15 11:58:25 +00:00
}
const { isWiki, containerClassName = '' } = this.props;
const containerClass = `wiki-page-container ${containerClassName}`;
// In dir-column-file width is 100%;
// In wiki-viewer width isn't 100%
const contentClassName = `wiki-page-content ${!isWiki ? + 'w-100' : ''}`;
2019-01-15 11:58:25 +00:00
return (
<div ref={this.scrollRef} className={containerClass}>
2019-05-29 03:48:00 +00:00
<div className={contentClassName}>
2019-01-15 11:58:25 +00:00
{this.props.children}
2019-01-24 07:41:01 +00:00
{this.renderMarkdown()}
2019-01-15 11:58:25 +00:00
<p id="wiki-page-last-modified">{gettext('Last modified by')} {this.props.latestContributor}, <span>{this.props.lastModified}</span></p>
</div>
</div>
);
}
}
2019-01-24 07:41:01 +00:00
const defaultProps = {
isWiki: false,
};
2019-01-24 07:41:01 +00:00
SeafileMarkdownViewer.propTypes = propTypes;
SeafileMarkdownViewer.defaultProps = defaultProps;
2019-01-15 11:58:25 +00:00
export default SeafileMarkdownViewer;