2019-01-04 18:30:03 +08:00
|
|
|
import React from 'react';
|
2025-02-14 14:04:25 +08:00
|
|
|
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';
|
2020-12-26 17:45:48 +08:00
|
|
|
import { serviceURL, mediaUrl } from './utils/constants';
|
2019-03-28 10:59:43 +08:00
|
|
|
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';
|
2019-07-16 10:01:09 +08:00
|
|
|
import toaster from './components/toast';
|
2019-01-04 18:30:03 +08:00
|
|
|
|
2019-03-28 10:59:43 +08:00
|
|
|
const { repoID, sharedToken, rawPath, err } = window.shared.pageOptions;
|
2019-01-04 18:30:03 +08:00
|
|
|
|
|
|
|
class SharedFileViewMarkdown extends React.Component {
|
2019-03-28 10:59:43 +08:00
|
|
|
render() {
|
2022-05-27 12:37:10 +08:00
|
|
|
return <SharedFileView content={<FileContent />} fileType="md" />;
|
2019-03-28 10:59:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class FileContent extends React.Component {
|
2019-01-04 18:30:03 +08:00
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
markdownContent: '',
|
2019-03-28 10:59:43 +08:00
|
|
|
loading: !err
|
2019-01-04 18:30:03 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
seafileAPI.getFileContent(rawPath).then((res) => {
|
|
|
|
this.setState({
|
|
|
|
markdownContent: res.data,
|
|
|
|
loading: false
|
|
|
|
});
|
2019-07-16 10:01:09 +08:00
|
|
|
}).catch(error => {
|
|
|
|
let errMessage = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errMessage);
|
2019-07-19 23:50:22 +08:00
|
|
|
});
|
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');
|
2019-11-05 17:46:06 +08:00
|
|
|
|
|
|
|
// 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;
|
2023-09-13 08:40:50 +08:00
|
|
|
};
|
2019-01-24 15:41:01 +08:00
|
|
|
|
|
|
|
modifyValueBeforeRender = (value) => {
|
2020-07-07 18:27:16 +08:00
|
|
|
return Utils.changeMarkdownNodes(value, this.changeImageURL);
|
2023-09-13 08:40:50 +08:00
|
|
|
};
|
2019-01-24 15:41:01 +08:00
|
|
|
|
2019-01-04 18:30:03 +08:00
|
|
|
render() {
|
2019-03-28 10:59:43 +08:00
|
|
|
if (err) {
|
|
|
|
return <SharedFileViewTip />;
|
|
|
|
}
|
|
|
|
|
2019-01-04 18:30:03 +08:00
|
|
|
if (this.state.loading) {
|
2019-01-31 17:37:02 +08:00
|
|
|
return <Loading />;
|
2019-01-04 18:30:03 +08:00
|
|
|
}
|
2019-03-28 10:59:43 +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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-14 14:04:25 +08:00
|
|
|
const root = createRoot(document.getElementById('wrapper'));
|
|
|
|
root.render(<SharedFileViewMarkdown />);
|