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

rewrite-local-links.js 3.7 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
  1. const assert = require('assert')
  2. const path = require('path')
  3. const externalRedirects = Object.keys(require('./redirects/external-sites'))
  4. const { getPathWithoutLanguage, getVersionStringFromPath } = require('./path-utils')
  5. const { getNewVersionedPath } = require('./old-versions-utils')
  6. const patterns = require('./patterns')
  7. const { deprecated, latest } = require('./enterprise-server-releases')
  8. const nonEnterpriseDefaultVersion = require('./non-enterprise-default-version')
  9. const allVersions = require('./all-versions')
  10. const supportedVersions = Object.keys(allVersions)
  11. const supportedPlans = Object.values(allVersions).map(v => v.plan)
  12. const removeFPTFromPath = require('./remove-fpt-from-path')
  13. // Content authors write links like `/some/article/path`, but they need to be
  14. // rewritten on the fly to match the current language and page version
  15. module.exports = function rewriteLocalLinks ($, version, languageCode) {
  16. assert(languageCode, 'languageCode is required')
  17. $('a[href^="/"]').each((i, el) => {
  18. getNewHref($(el), languageCode, version)
  19. })
  20. }
  21. function getNewHref (link, languageCode, version) {
  22. const href = link.attr('href')
  23. // Exceptions to link rewriting
  24. if (href.startsWith('/assets')) return
  25. if (href.startsWith('/public')) return
  26. if (externalRedirects.includes(href)) return
  27. let newHref = href
  28. // If the link has a hardcoded plan or version in it, do not update other than adding a language code
  29. // Examples:
  30. // /enterprise-server@2.20/rest/reference/oauth-authorizations
  31. // /enterprise-server/rest/reference/oauth-authorizations (this redirects to the latest version)
  32. // /enterprise-server@latest/rest/reference/oauth-authorizations (this redirects to the latest version)
  33. const firstLinkSegment = href.split('/')[1]
  34. if ([...supportedPlans, ...supportedVersions, 'enterprise-server@latest'].includes(firstLinkSegment)) {
  35. newHref = path.join('/', languageCode, href)
  36. }
  37. // If the link includes a deprecated version, do not update other than adding a language code
  38. // Example: /enterprise/11.10.340/admin/articles/upgrading-to-the-latest-release
  39. const oldEnterpriseVersionNumber = href.match(patterns.getEnterpriseVersionNumber)
  40. if (oldEnterpriseVersionNumber && deprecated.includes(oldEnterpriseVersionNumber[1])) {
  41. newHref = path.join('/', languageCode, href)
  42. }
  43. if (newHref === href) {
  44. // start clean with no language (TOC pages already include the lang codes via lib/liquid-tags/link.js)
  45. const hrefWithoutLang = getPathWithoutLanguage(href)
  46. // normalize any legacy links so they conform to new link structure
  47. newHref = path.posix.join('/', languageCode, getNewVersionedPath(hrefWithoutLang))
  48. // get the current version from the link
  49. const versionFromHref = getVersionStringFromPath(newHref)
  50. // ------ BEGIN ONE-OFF OVERRIDES ------//
  51. // dotcom-only links always point to dotcom
  52. if (link.hasClass('dotcom-only')) {
  53. version = nonEnterpriseDefaultVersion
  54. }
  55. // desktop links always point to dotcom
  56. if (patterns.desktop.test(hrefWithoutLang)) {
  57. version = nonEnterpriseDefaultVersion
  58. }
  59. // admin links on dotcom always point to Enterprise
  60. if (patterns.adminProduct.test(hrefWithoutLang) && version === nonEnterpriseDefaultVersion) {
  61. version = `enterprise-server@${latest}`
  62. }
  63. // insights links on dotcom always point to Enterprise
  64. if (patterns.insightsProduct.test(hrefWithoutLang) && version === nonEnterpriseDefaultVersion) {
  65. version = `enterprise-server@${latest}`
  66. }
  67. // ------ END ONE-OFF OVERRIDES ------//
  68. // update the version in the link
  69. newHref = removeFPTFromPath(newHref.replace(versionFromHref, version))
  70. }
  71. newHref = newHref.replace(patterns.trailingSlash, '$1')
  72. if (href !== newHref) link.attr('href', newHref)
  73. }
Tip!

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

Comments

Loading...