Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

path-utils.js 3.4 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  1. const slash = require('slash')
  2. const path = require('path')
  3. const patterns = require('./patterns')
  4. const { latest } = require('./enterprise-server-releases')
  5. const { productIds } = require('./all-products')
  6. const allVersions = require('./all-versions')
  7. const supportedVersions = new Set(Object.keys(allVersions))
  8. const nonEnterpriseDefaultVersion = require('./non-enterprise-default-version')
  9. // Add the language to the given HREF
  10. // /articles/foo -> /en/articles/foo
  11. function getPathWithLanguage (href, languageCode) {
  12. return slash(path.posix.join('/', languageCode, getPathWithoutLanguage(href)))
  13. .replace(patterns.trailingSlash, '$1')
  14. }
  15. // Remove the language from the given HREF
  16. // /en/articles/foo -> /articles/foo
  17. function getPathWithoutLanguage (href) {
  18. return slash(href.replace(patterns.hasLanguageCode, '/'))
  19. }
  20. // Remove the version segment from the path
  21. function getPathWithoutVersion (href) {
  22. const versionFromPath = getVersionStringFromPath(href)
  23. // If the derived version is not found in the list of all versions, just return the HREF
  24. return allVersions[versionFromPath]
  25. ? href.replace(`/${getVersionStringFromPath(href)}`, '')
  26. : href
  27. }
  28. // Return the version segment in a path
  29. function getVersionStringFromPath (href) {
  30. href = getPathWithoutLanguage(href)
  31. // Return immediately if this is a link to the homepage
  32. if (href === '/') {
  33. return 'homepage'
  34. }
  35. // Get the first segment
  36. const versionFromPath = href.split('/')[1]
  37. // If the first segment is a supported product, assume this is FPT
  38. if (productIds.includes(versionFromPath)) {
  39. return nonEnterpriseDefaultVersion
  40. }
  41. // Otherwise, check if it's a supported version
  42. if (supportedVersions.has(versionFromPath)) {
  43. return versionFromPath
  44. }
  45. // If the version segment is the latest enterprise-server release, return the latest release
  46. if (versionFromPath === 'enterprise-server@latest') {
  47. return `enterprise-server@${latest}`
  48. }
  49. // If it's just a plan with no @release (e.g., `enterprise-server`), return the latest release
  50. const planObject = Object.values(allVersions).find(v => v.plan === versionFromPath)
  51. if (planObject) {
  52. return allVersions[planObject.latestVersion].version
  53. }
  54. // Otherwise, return the first segment as-is, which may not be a real supported version,
  55. // but additional checks are done on this segment in getVersionedPathWithoutLanguage
  56. return versionFromPath
  57. }
  58. // Return the corresponding object for the version segment in a path
  59. function getVersionObjectFromPath (href) {
  60. const versionFromPath = getVersionStringFromPath(href)
  61. return allVersions[versionFromPath]
  62. }
  63. // Return the product segment from the path
  64. function getProductStringFromPath (href) {
  65. href = getPathWithoutLanguage(href)
  66. if (href === '/') return 'homepage'
  67. const pathParts = href.split('/')
  68. if (pathParts.includes('early-access')) return 'early-access'
  69. return productIds.includes(pathParts[2])
  70. ? pathParts[2]
  71. : pathParts[1]
  72. }
  73. function getCategoryStringFromPath (href) {
  74. href = getPathWithoutLanguage(href)
  75. if (href === '/') return null
  76. const pathParts = href.split('/')
  77. if (pathParts.includes('early-access')) return null
  78. const productIndex = productIds.includes(pathParts[2])
  79. ? 2
  80. : 1
  81. return pathParts[productIndex + 1]
  82. }
  83. module.exports = {
  84. getPathWithLanguage,
  85. getPathWithoutLanguage,
  86. getPathWithoutVersion,
  87. getVersionStringFromPath,
  88. getVersionObjectFromPath,
  89. getProductStringFromPath,
  90. getCategoryStringFromPath
  91. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...