mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-16 15:19:06 +00:00
Update public wiki (#2868)
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import MarkdownViewer from '@seafile/seafile-editor/dist/viewer/markdown-viewer';
|
||||
import { gettext } from '../utils/constants';
|
||||
import { gettext, repoID, slug, serviceURL, isPublicWiki } from '../utils/constants';
|
||||
import Loading from './loading';
|
||||
import { Utils } from '../utils/utils';
|
||||
|
||||
const propTypes = {
|
||||
children: PropTypes.object,
|
||||
@@ -10,7 +11,8 @@ const propTypes = {
|
||||
markdownContent: PropTypes.string.isRequired,
|
||||
latestContributor: PropTypes.string.isRequired,
|
||||
lastModified: PropTypes.string.isRequired,
|
||||
onLinkClick: PropTypes.func.isRequired
|
||||
onLinkClick: PropTypes.func.isRequired,
|
||||
isWiki: PropTypes.bool
|
||||
};
|
||||
|
||||
const contentClass = 'wiki-page-content';
|
||||
@@ -102,6 +104,76 @@ class WikiMarkdownViewer extends React.Component {
|
||||
this.setState({activeTitleIndex: activeTitleIndex});
|
||||
}
|
||||
|
||||
changeInlineNode = (item) => {
|
||||
if (item.object == 'inline') {
|
||||
let url;
|
||||
|
||||
// change image url
|
||||
if (item.type == 'image' && isPublicWiki) {
|
||||
url = item.data.src;
|
||||
const re = new RegExp(serviceURL + '/lib/' + repoID +'/file.*raw=1');
|
||||
// different repo
|
||||
if (!re.test(url)) {
|
||||
return;
|
||||
}
|
||||
// get image path
|
||||
let index = url.indexOf('/file');
|
||||
let index2 = url.indexOf('?');
|
||||
const imagePath = url.substring(index + 5, index2);
|
||||
// replace url
|
||||
item.data.src = serviceURL + '/view-image-via-public-wiki/?slug=' + slug + '&path=' + imagePath;
|
||||
}
|
||||
|
||||
else if (item.type == 'link') {
|
||||
url = item.data.href;
|
||||
// change file url
|
||||
if (Utils.isInternalMarkdownLink(url, repoID)) {
|
||||
let path = Utils.getPathFromInternalMarkdownLink(url, repoID);
|
||||
// replace url
|
||||
item.data.href = serviceURL + '/wikis/' + slug + path;
|
||||
}
|
||||
// change dir url
|
||||
else if (Utils.isInternalDirLink(url, repoID)) {
|
||||
let path = Utils.getPathFromInternalDirLink(url, repoID, slug);
|
||||
// replace url
|
||||
item.data.href = serviceURL + '/wikis/' + slug + path;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
modifyValueBeforeRender = (value) => {
|
||||
let nodes = value.document.nodes;
|
||||
let newNodes = Utils.changeMarkdownNodes(nodes, this.changeInlineNode);
|
||||
value.document.nodes = newNodes;
|
||||
return value;
|
||||
}
|
||||
|
||||
renderMarkdown = () => {
|
||||
if (this.props.isWiki) {
|
||||
return (
|
||||
<MarkdownViewer
|
||||
showTOC={true}
|
||||
markdownContent={this.props.markdownContent}
|
||||
activeTitleIndex={this.state.activeTitleIndex}
|
||||
onContentRendered={this.onContentRendered}
|
||||
modifyValueBeforeRender={this.modifyValueBeforeRender}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<MarkdownViewer
|
||||
showTOC={true}
|
||||
markdownContent={this.props.markdownContent}
|
||||
activeTitleIndex={this.state.activeTitleIndex}
|
||||
onContentRendered={this.onContentRendered}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
render() {
|
||||
if (this.props.isFileLoading) {
|
||||
return <Loading />
|
||||
@@ -110,12 +182,7 @@ class WikiMarkdownViewer extends React.Component {
|
||||
<div ref={this.markdownContainer} className="wiki-page-container" onScroll={this.onScrollHandler.bind(this)}>
|
||||
<div className={contentClass}>
|
||||
{this.props.children}
|
||||
<MarkdownViewer
|
||||
showTOC={true}
|
||||
markdownContent={this.props.markdownContent}
|
||||
activeTitleIndex={this.state.activeTitleIndex}
|
||||
onContentRendered={this.onContentRendered}
|
||||
/>
|
||||
{this.renderMarkdown()}
|
||||
<p id="wiki-page-last-modified">{gettext('Last modified by')} {this.props.latestContributor}, <span>{this.props.lastModified}</span></p>
|
||||
</div>
|
||||
</div>
|
||||
@@ -123,6 +190,11 @@ class WikiMarkdownViewer extends React.Component {
|
||||
}
|
||||
}
|
||||
|
||||
const defaultProps = {
|
||||
isWiki: false,
|
||||
}
|
||||
|
||||
WikiMarkdownViewer.propTypes = propTypes;
|
||||
MarkdownViewer.defaultProps = defaultProps;
|
||||
|
||||
export default WikiMarkdownViewer;
|
||||
|
Reference in New Issue
Block a user