From 08579289d7e6a4b785cc2f4691afee1af5925e08 Mon Sep 17 00:00:00 2001 From: Michael An <1822852997@qq.com> Date: Thu, 20 Jun 2019 14:40:11 +0800 Subject: [PATCH 1/3] wiki relative image path --- frontend/src/components/wiki-markdown-viewer.js | 16 +++++++++------- frontend/src/pages/wiki/main-panel.js | 1 + frontend/src/utils/utils.js | 16 ++++++++++++++++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/frontend/src/components/wiki-markdown-viewer.js b/frontend/src/components/wiki-markdown-viewer.js index 1dfca1aa27..b9f16887e0 100644 --- a/frontend/src/components/wiki-markdown-viewer.js +++ b/frontend/src/components/wiki-markdown-viewer.js @@ -127,21 +127,23 @@ class WikiMarkdownViewer extends React.Component { changeInlineNode = (item) => { if (item.object == 'inline') { - let url; + let url, imagePath; // 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)) { + if (re.test(url)) { + // get image path + let index = url.indexOf('/file'); + let index2 = url.indexOf('?'); + imagePath = url.substring(index + 5, index2); + } else if (/^\.\.\/*/.test(url) || /^\.\/*/.test(url)) { + imagePath = Utils.pathNormalize(this.props.path, url); + } else { 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; } diff --git a/frontend/src/pages/wiki/main-panel.js b/frontend/src/pages/wiki/main-panel.js index ab9c2127c8..d53a83c9db 100644 --- a/frontend/src/pages/wiki/main-panel.js +++ b/frontend/src/pages/wiki/main-panel.js @@ -116,6 +116,7 @@ class MainPanel extends Component { latestContributor={this.props.latestContributor} onLinkClick={this.props.onLinkClick} isWiki={true} + path={this.props.path} /> )} {(!this.props.isDataLoading && !this.props.isViewFile) && ( diff --git a/frontend/src/utils/utils.js b/frontend/src/utils/utils.js index 9a80b81266..360f4e7bb6 100644 --- a/frontend/src/utils/utils.js +++ b/frontend/src/utils/utils.js @@ -880,6 +880,22 @@ export const Utils = { return password; }, + pathNormalize: function(filePath, relativePath) { + let newPath = relativePath.split('/'); + let path = filePath.slice(1, filePath.lastIndexOf('/')).split('/'); + for (let i = 0, len = newPath.length; i < len; i++) { + if (newPath[i] === '..') { + path.pop(); + newPath.splice(i, 1); + i--; + } else if (newPath[i] === '.') { + newPath.splice(i, 1); + i--; + } + } + return path.concat(newPath).join('/'); + }, + getEventData: function(event, data) { if (event.target.dataset) { return event.target.dataset[data]; From eaca5e05847ebfe2e8868f9b34b31fe896c2efe7 Mon Sep 17 00:00:00 2001 From: Michael An <1822852997@qq.com> Date: Thu, 20 Jun 2019 18:03:26 +0800 Subject: [PATCH 2/3] update --- .../src/components/wiki-markdown-viewer.js | 4 +++- frontend/src/utils/utils.js | 18 ++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/frontend/src/components/wiki-markdown-viewer.js b/frontend/src/components/wiki-markdown-viewer.js index b9f16887e0..a6bf1b8948 100644 --- a/frontend/src/components/wiki-markdown-viewer.js +++ b/frontend/src/components/wiki-markdown-viewer.js @@ -140,7 +140,9 @@ class WikiMarkdownViewer extends React.Component { let index2 = url.indexOf('?'); imagePath = url.substring(index + 5, index2); } else if (/^\.\.\/*/.test(url) || /^\.\/*/.test(url)) { - imagePath = Utils.pathNormalize(this.props.path, url); + const path = this.props.path; + const originalPath = path.slice(0, path.lastIndexOf('/')) + '/' + url; + imagePath = Utils.pathNormalize(originalPath); } else { return; } diff --git a/frontend/src/utils/utils.js b/frontend/src/utils/utils.js index 360f4e7bb6..7346ec0598 100644 --- a/frontend/src/utils/utils.js +++ b/frontend/src/utils/utils.js @@ -880,20 +880,18 @@ export const Utils = { return password; }, - pathNormalize: function(filePath, relativePath) { - let newPath = relativePath.split('/'); - let path = filePath.slice(1, filePath.lastIndexOf('/')).split('/'); - for (let i = 0, len = newPath.length; i < len; i++) { - if (newPath[i] === '..') { - path.pop(); - newPath.splice(i, 1); - i--; - } else if (newPath[i] === '.') { + pathNormalize: function(originalPath) { + let newPath = originalPath.split('/'); + for (let i = 0; i < newPath.length; i++) { + if (newPath[i] === '.' || newPath[i] === '') { newPath.splice(i, 1); i--; + } else if (newPath[i] === '..') { + newPath.splice(i - 1, 2); + i = i - 2; } } - return path.concat(newPath).join('/'); + return newPath.join('/'); }, getEventData: function(event, data) { From a08a1e97e31d02ab93aa033901d4ba5bd4f1c1e8 Mon Sep 17 00:00:00 2001 From: Michael An <1822852997@qq.com> Date: Thu, 20 Jun 2019 18:25:10 +0800 Subject: [PATCH 3/3] update --- frontend/src/utils/utils.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/frontend/src/utils/utils.js b/frontend/src/utils/utils.js index 7346ec0598..04479817e3 100644 --- a/frontend/src/utils/utils.js +++ b/frontend/src/utils/utils.js @@ -881,14 +881,15 @@ export const Utils = { }, pathNormalize: function(originalPath) { - let newPath = originalPath.split('/'); - for (let i = 0; i < newPath.length; i++) { - if (newPath[i] === '.' || newPath[i] === '') { - newPath.splice(i, 1); - i--; - } else if (newPath[i] === '..') { - newPath.splice(i - 1, 2); - i = i - 2; + let oldPath = originalPath.split('/'); + let newPath = []; + for (let i = 0; i < oldPath.length; i++) { + if (oldPath[i] === '.' || oldPath[i] === '') { + continue; + } else if (oldPath[i] === '..') { + newPath.pop(); + } else { + newPath.push(oldPath[i]); } } return newPath.join('/');