Skip to content

Commit 0f1c683

Browse files
authored
Merge pull request #2064 from RedisInsight/fe/bugfix/RI-4494
#RI-4494 - fix url parsing for docker builds
2 parents c71f203 + 7cfee7d commit 0f1c683

File tree

5 files changed

+36
-33
lines changed

5 files changed

+36
-33
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { visit } from 'unist-util-visit'
2-
import { prepareTutorialDataFileUrlFromMd } from 'uiSrc/utils/pathUtil'
2+
import { getFileUrlFromMd } from 'uiSrc/utils/pathUtil'
33

44
export const remarkImage = (path: string): (tree: Node) => void => (tree: any) => {
55
// Find img node in syntax tree
66
visit(tree, 'image', (node) => {
7-
node.url = prepareTutorialDataFileUrlFromMd(node.url, path)
7+
node.url = getFileUrlFromMd(node.url, path)
88
})
99
}

redisinsight/ui/src/pages/workbench/components/enablement-area/EnablementArea/utils/transform/remarkRedisUpload.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { visit } from 'unist-util-visit'
2-
import { prepareTutorialDataFileUrlFromMd } from 'uiSrc/utils/pathUtil'
2+
import { getFileUrlFromMd } from 'uiSrc/utils/pathUtil'
33

44
export const remarkRedisUpload = (path: string): (tree: Node) => void => (tree: any) => {
55
// Find code node in syntax tree
@@ -10,7 +10,7 @@ export const remarkRedisUpload = (path: string): (tree: Node) => void => (tree:
1010
const value: string = `${lang} ${meta}`
1111
const [, filePath, label] = value.match(/^redis-upload:\[(.*)] (.*)/i) || []
1212

13-
const { pathname } = new URL(prepareTutorialDataFileUrlFromMd(filePath, path))
13+
const { pathname } = new URL(getFileUrlFromMd(filePath, path))
1414
const decodedPath = decodeURI(pathname)
1515

1616
if (path && label) {

redisinsight/ui/src/services/resourcesService.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ const resourcesService = axios.create({
1313
baseURL: RESOURCES_BASE_URL,
1414
})
1515

16+
export const getOriginUrl = () => (IS_ABSOLUTE_PATH.test(RESOURCES_BASE_URL)
17+
? RESOURCES_BASE_URL
18+
: (window?.location?.origin || RESOURCES_BASE_URL))
19+
1620
export const getPathToResource = (url: string = ''): string => (IS_ABSOLUTE_PATH.test(url)
1721
? url
1822
: new URL(url, resourcesService.defaults.baseURL).toString())

redisinsight/ui/src/utils/pathUtil.ts

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RESOURCES_BASE_URL } from 'uiSrc/services/resourcesService'
1+
import { getOriginUrl } from 'uiSrc/services/resourcesService'
22
import { IS_ABSOLUTE_PATH } from 'uiSrc/constants/regex'
33

44
enum TutorialsPaths {
@@ -7,37 +7,36 @@ enum TutorialsPaths {
77
Tutorials = 'tutorials',
88
}
99

10-
export const prepareTutorialDataFileUrlFromMd = (nodeUrl: string, mdPath: string): string => {
11-
// process external link
12-
if (IS_ABSOLUTE_PATH.test(nodeUrl)) {
13-
return nodeUrl
10+
export const getRootStaticPath = (mdPath: string) => {
11+
const paths = mdPath?.split('/') || []
12+
const tutorialFolder = paths[1]
13+
14+
if (tutorialFolder === TutorialsPaths.CustomTutorials) return paths.slice(0, 3).join('/')
15+
if (tutorialFolder === TutorialsPaths.Guide || tutorialFolder === TutorialsPaths.Tutorials) {
16+
return paths.slice(0, 2).join('/')
1417
}
1518

16-
// process absolute path
19+
return mdPath
20+
}
21+
22+
const processAbsolutePath = (nodeUrl: string, mdPath: string) => {
23+
// todo: quick fix. find the root cause why path has both '/' and '\'
24+
const normalizedMdPath = mdPath.replaceAll('\\', '/')
25+
const tutorialRootPath = getRootStaticPath(normalizedMdPath)
26+
27+
return new URL(tutorialRootPath + nodeUrl, getOriginUrl()).toString()
28+
}
29+
30+
export const getFileUrlFromMd = (nodeUrl: string, mdPath: string): string => {
31+
// process external link
32+
if (IS_ABSOLUTE_PATH.test(nodeUrl)) return nodeUrl
33+
1734
if (nodeUrl.startsWith('/') || nodeUrl.startsWith('\\')) {
18-
// todo: quick fix. find the root cause why path has both '/' and '\'
19-
const normalizedMdPath = mdPath.replaceAll('\\', '/')
20-
21-
const paths = normalizedMdPath?.split('/') || []
22-
let tutorialRootPath
23-
switch (paths[1]) {
24-
case TutorialsPaths.CustomTutorials:
25-
tutorialRootPath = paths.slice(0, 3).join('/')
26-
break
27-
case TutorialsPaths.Guide:
28-
case TutorialsPaths.Tutorials:
29-
tutorialRootPath = paths.slice(0, 2).join('/')
30-
break
31-
default:
32-
tutorialRootPath = normalizedMdPath
33-
break
34-
}
35-
36-
return new URL(tutorialRootPath + nodeUrl, RESOURCES_BASE_URL).toString()
35+
return processAbsolutePath(nodeUrl, mdPath)
3736
}
3837

3938
// process relative path
40-
const pathUrl = new URL(mdPath, RESOURCES_BASE_URL)
39+
const pathUrl = new URL(mdPath, getOriginUrl())
4140
return new URL(nodeUrl, pathUrl).toString()
4241
}
4342

redisinsight/ui/src/utils/tests/pathUtil.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { RESOURCES_BASE_URL } from 'uiSrc/services/resourcesService'
2-
import { prepareTutorialDataFileUrlFromMd } from '../pathUtil'
2+
import { getFileUrlFromMd } from '../pathUtil'
33

44
jest.mock('unist-util-visit')
55
const TUTORIAL_PATH = 'static/custom-tutorials/tutorial-id'
@@ -31,10 +31,10 @@ const testCases = [
3131
result: 'https://somesite.test/image.png',
3232
}
3333
]
34-
describe('prepareTutorialDataFileUrlFromMd', () => {
34+
describe('getFileUrlFromMd', () => {
3535
testCases.forEach((tc) => {
3636
it(`should return ${tc.result} for url:${tc.url}, path: ${tc.path} `, () => {
37-
const url = prepareTutorialDataFileUrlFromMd(tc.url, tc.path)
37+
const url = getFileUrlFromMd(tc.url, tc.path)
3838
expect(url).toEqual(tc.result)
3939
})
4040
})

0 commit comments

Comments
 (0)