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

permalinks.js 2.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
  1. const path = require('path')
  2. const patterns = require('../patterns')
  3. const supportedVersions = new Set(Object.keys(require('../all-versions')))
  4. const getOldPathsFromPermalink = require('./get-old-paths-from-permalink')
  5. const { getVersionStringFromPath } = require('../path-utils')
  6. const { getNewVersionedPath } = require('../old-versions-utils')
  7. const removeFPTFromPath = require('../remove-fpt-from-path')
  8. module.exports = function generateRedirectsForPermalinks (permalinks, redirectFrontmatter) {
  9. // account for Array or String frontmatter entries
  10. const redirectFrontmatterOldPaths = redirectFrontmatter
  11. ? Array.from([redirectFrontmatter]).flat()
  12. : []
  13. const redirects = {}
  14. // for every permalink...
  15. permalinks.forEach(permalink => {
  16. // get an array of possible old paths, e.g., /desktop/guides/ from current permalink /desktop/
  17. const possibleOldPaths = getOldPathsFromPermalink(permalink.href, permalink.languageCode, permalink.pageVersion)
  18. // for each old path, add a redirect to the current permalink
  19. possibleOldPaths.forEach(oldPath => {
  20. redirects[oldPath] = permalink.href
  21. })
  22. // for every redirect frontmatter old path...
  23. redirectFrontmatterOldPaths.forEach(frontmatterOldPath => {
  24. // remove trailing slashes (sometimes present in frontmatter)
  25. frontmatterOldPath = frontmatterOldPath.replace(patterns.trailingSlash, '$1')
  26. // support hardcoded versions in redirect frontmatter
  27. if (supportedVersions.has(frontmatterOldPath.split('/')[1])) {
  28. redirects[frontmatterOldPath] = permalink.href
  29. redirects[`/en${frontmatterOldPath}`] = permalink.href
  30. }
  31. // get the old path for the current permalink version
  32. let versionedFrontmatterOldPath = path.posix.join('/', permalink.languageCode, getNewVersionedPath(frontmatterOldPath))
  33. const versionFromPath = getVersionStringFromPath(versionedFrontmatterOldPath)
  34. versionedFrontmatterOldPath = removeFPTFromPath(versionedFrontmatterOldPath.replace(versionFromPath, permalink.pageVersion))
  35. // add it to the redirects object
  36. redirects[versionedFrontmatterOldPath] = permalink.href
  37. // then get an array of possible alternative old paths from the current versioned old path
  38. const possibleOldPathsForVersionedOldPaths = getOldPathsFromPermalink(versionedFrontmatterOldPath, permalink.languageCode, permalink.pageVersion)
  39. // and add each one to the redirects object
  40. possibleOldPathsForVersionedOldPaths.forEach(oldPath => {
  41. redirects[oldPath] = permalink.href
  42. })
  43. })
  44. })
  45. // filter for unique entries only
  46. Object.entries(redirects).forEach(([oldPath, newPath]) => {
  47. if (oldPath === newPath) delete redirects[oldPath]
  48. })
  49. return redirects
  50. }
Tip!

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

Comments

Loading...