From 605068cb7a3cab8c83e788c33464e9a9b83a16e7 Mon Sep 17 00:00:00 2001 From: llj Date: Wed, 8 Apr 2020 16:51:21 +0800 Subject: [PATCH] [wiki markdown file] modified links in the file content (#4517) * only modified links of inserted files from the current library --- .../src/components/wiki-markdown-viewer.js | 22 ++++++++++++------- frontend/src/utils/utils.js | 5 +++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/frontend/src/components/wiki-markdown-viewer.js b/frontend/src/components/wiki-markdown-viewer.js index b1e30dda43..295822141c 100644 --- a/frontend/src/components/wiki-markdown-viewer.js +++ b/frontend/src/components/wiki-markdown-viewer.js @@ -1,7 +1,7 @@ import React from 'react'; import PropTypes from 'prop-types'; import MarkdownViewer from '@seafile/seafile-editor/dist/viewer/markdown-viewer'; -import { gettext, repoID, slug, serviceURL, isPublicWiki, siteRoot } from '../utils/constants'; +import { gettext, repoID, slug, serviceURL, isPublicWiki, siteRoot, sharedToken } from '../utils/constants'; import { Card, CardTitle, CardText } from 'reactstrap'; import Loading from './loading'; import { seafileAPI } from '../utils/seafile-api'; @@ -158,13 +158,19 @@ class WikiMarkdownViewer extends React.Component { 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 + '/published/' + slug + path; - } - // change dir url + // change file url + if (Utils.isInternalFileLink(url, repoID)) { + if (Utils.isInternalMarkdownLink(url, repoID)) { + let path = Utils.getPathFromInternalMarkdownLink(url, repoID); + // replace url + item.data.href = serviceURL + '/published/' + slug + path; + } else { + item.data.href = url.replace(/(.*)lib\/([-0-9a-f]{36})\/file(.*)/g, (match, p1, p2, p3) => { + return `${p1}d/${sharedToken}/files/?p=${p3}&dl=1`; + }); + } + } + // change dir url else if (Utils.isInternalDirLink(url, repoID)) { let path = Utils.getPathFromInternalDirLink(url, repoID); // replace url diff --git a/frontend/src/utils/utils.js b/frontend/src/utils/utils.js index d2e9be5576..de9af1fe66 100644 --- a/frontend/src/utils/utils.js +++ b/frontend/src/utils/utils.js @@ -714,6 +714,11 @@ export const Utils = { } }, + isInternalFileLink: function(url, repoID) { + var re = new RegExp(serviceURL + '/lib/' + repoID + '/file.*'); + return re.test(url); + }, + isInternalMarkdownLink: function(url, repoID) { var re = new RegExp(serviceURL + '/lib/' + repoID + '.*\.md$'); return re.test(url);