1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-06 17:33:18 +00:00
Files
seahub/frontend/src/shared-file-view-markdown.js

92 lines
2.6 KiB
JavaScript
Raw Normal View History

2019-01-04 18:30:03 +08:00
import React from 'react';
import { createRoot } from 'react-dom/client';
2022-05-25 15:22:43 +08:00
import { MarkdownViewer } from '@seafile/seafile-editor';
2019-01-04 18:30:03 +08: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 18:30:03 +08:00
import Loading from './components/loading';
import toaster from './components/toast';
2019-01-04 18:30:03 +08:00
const { repoID, sharedToken, rawPath, err } = window.shared.pageOptions;
2019-01-04 18:30:03 +08:00
class SharedFileViewMarkdown extends React.Component {
render() {
return <SharedFileView content={<FileContent />} fileType="md" />;
}
}
class FileContent extends React.Component {
2019-01-04 18:30:03 +08:00
constructor(props) {
super(props);
this.state = {
markdownContent: '',
loading: !err
2019-01-04 18:30:03 +08: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 18:30:03 +08:00
}
2019-01-24 15:41:01 +08:00
changeImageURL = (innerNode) => {
if (innerNode.type == 'image') {
let imageUrl = innerNode.data.src;
2024-07-18 11:58:42 +08:00
const re = new RegExp(serviceURL + '/lib/' + repoID + '/file.*raw=1');
// different repo
2019-01-24 15:41:01 +08:00
if (!re.test(imageUrl)) {
return;
}
// get image path
2020-03-24 20:12:29 +08: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 15:41:01 +08:00
// change image url
2022-01-18 21:29:45 +08:00
// the image path has been encoded when inserting the image
2022-01-18 17:28:10 +08:00
innerNode.data.src = serviceURL + '/view-image-via-share-link/?token=' + sharedToken + '&path=' + imagePath;
2019-01-24 15:41:01 +08:00
}
return innerNode;
};
2019-01-24 15:41:01 +08:00
modifyValueBeforeRender = (value) => {
2020-07-07 18:27:16 +08:00
return Utils.changeMarkdownNodes(value, this.changeImageURL);
};
2019-01-24 15:41:01 +08:00
2019-01-04 18:30:03 +08:00
render() {
if (err) {
return <SharedFileViewTip />;
}
2019-01-04 18:30:03 +08:00
if (this.state.loading) {
return <Loading />;
2019-01-04 18:30:03 +08:00
}
2019-01-04 18:30:03 +08:00
return (
2023-12-07 15:14:34 +08: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 18:30:03 +08:00
</div>
);
}
}
const root = createRoot(document.getElementById('wrapper'));
root.render(<SharedFileViewMarkdown />);