1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-07-18 17:22:05 +00:00
seahub/frontend/src/shared-file-view-markdown.js

91 lines
2.5 KiB
JavaScript
Raw Normal View History

2019-01-04 10:30:03 +00:00
import React from 'react';
import ReactDom from 'react-dom';
2022-05-25 07:22:43 +00:00
import { MarkdownViewer } from '@seafile/seafile-editor';
2019-01-04 10:30:03 +00:00
import { seafileAPI } from './utils/seafile-api';
import { Utils } from './utils/utils';
import { serviceURL, mediaUrl } from './utils/constants';
import SharedFileView from './components/shared-file-view/shared-file-view';
import SharedFileViewTip from './components/shared-file-view/shared-file-view-tip';
2019-01-04 10:30:03 +00:00
import Loading from './components/loading';
import toaster from './components/toast';
2019-01-04 10:30:03 +00:00
const { repoID, sharedToken, rawPath, err } = window.shared.pageOptions;
2019-01-04 10:30:03 +00:00
class SharedFileViewMarkdown extends React.Component {
render() {
return <SharedFileView content={<FileContent />} fileType="md" />;
}
}
class FileContent extends React.Component {
2019-01-04 10:30:03 +00:00
constructor(props) {
super(props);
this.state = {
markdownContent: '',
loading: !err
2019-01-04 10:30:03 +00:00
};
}
componentDidMount() {
seafileAPI.getFileContent(rawPath).then((res) => {
this.setState({
markdownContent: res.data,
loading: false
});
}).catch(error => {
let errMessage = Utils.getErrorMsg(error);
toaster.danger(errMessage);
});
2019-01-04 10:30:03 +00:00
}
2019-01-24 07:41:01 +00:00
changeImageURL = (innerNode) => {
if (innerNode.type == 'image') {
let imageUrl = innerNode.data.src;
2024-07-18 03:58:42 +00:00
const re = new RegExp(serviceURL + '/lib/' + repoID + '/file.*raw=1');
// different repo
2019-01-24 07:41:01 +00:00
if (!re.test(imageUrl)) {
return;
}
// get image path
2020-03-24 12:12:29 +00:00
let imagePath = imageUrl.substring(serviceURL.length);
let index = imagePath.indexOf('/file');
let index2 = imagePath.indexOf('?');
imagePath = imagePath.substring(index + 5, index2);
2019-01-24 07:41:01 +00:00
// change image url
2022-01-18 13:29:45 +00:00
// the image path has been encoded when inserting the image
2022-01-18 09:28:10 +00:00
innerNode.data.src = serviceURL + '/view-image-via-share-link/?token=' + sharedToken + '&path=' + imagePath;
2019-01-24 07:41:01 +00:00
}
return innerNode;
};
2019-01-24 07:41:01 +00:00
modifyValueBeforeRender = (value) => {
2020-07-07 10:27:16 +00:00
return Utils.changeMarkdownNodes(value, this.changeImageURL);
};
2019-01-24 07:41:01 +00:00
2019-01-04 10:30:03 +00:00
render() {
if (err) {
return <SharedFileViewTip />;
}
2019-01-04 10:30:03 +00:00
if (this.state.loading) {
return <Loading />;
2019-01-04 10:30:03 +00:00
}
2019-01-04 10:30:03 +00:00
return (
2023-12-07 07:14:34 +00:00
<div className="shared-file-view-body md-view">
<MarkdownViewer
value={this.state.markdownContent}
isShowOutline={true}
mathJaxSource={mediaUrl + 'js/mathjax/tex-svg.js'}
beforeRenderCallback={this.modifyValueBeforeRender}
/>
2019-01-04 10:30:03 +00:00
</div>
);
}
}
ReactDom.render(<SharedFileViewMarkdown />, document.getElementById('wrapper'));