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-legacy-asset-paths.js 1.8 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
  1. const visit = require('unist-util-visit')
  2. const fs = require('fs')
  3. const { legacyAssetVersions } = require('../../enterprise-server-releases')
  4. const allVersions = require('../../all-versions')
  5. const matcher = node => (
  6. node.type === 'element' &&
  7. node.tagName === 'img' &&
  8. node.properties.src &&
  9. node.properties.src.startsWith('/assets/images')
  10. )
  11. // This module rewrites asset paths for specific Enterprise versions that
  12. // were migrated from AWS S3 image storage. Only images that were unique
  13. // were stored in a new /assets/enterprise path. Once these versions are
  14. // deprecated, we will have one source of truth for image assets and we
  15. // can remove this module.
  16. // Source example: /assets/images/foo.png
  17. // Rewritten: /assets/enterprise/2.20/assets/images/foo.png
  18. module.exports = function checkForLegacyAssetPaths ({ currentVersion, relativePath }) {
  19. // Bail if we don't have a relativePath in this context
  20. if (!relativePath) return
  21. // skip if this is the homepage
  22. if (relativePath === 'index.md') return
  23. // skip for any versions that aren't enterprise-server
  24. if (!allVersions[currentVersion].plan === 'enterprise-server') return
  25. const enterpriseRelease = allVersions[currentVersion].currentRelease
  26. if (!legacyAssetVersions.includes(enterpriseRelease)) return
  27. return async (tree) => {
  28. const promises = []
  29. visit(tree, matcher, visitor)
  30. await Promise.all(promises)
  31. function visitor (node) {
  32. const legacyAssetPath = `/assets/images/enterprise/legacy-format/${enterpriseRelease}${node.properties.src}`
  33. const p = fs.promises.access((`${process.cwd()}${legacyAssetPath}`), fs.constants.F_OK)
  34. // rewrite the nodes src
  35. .then(() => { node.properties.src = `${legacyAssetPath}` })
  36. .catch(() => node)
  37. promises.push(p)
  38. }
  39. }
  40. }
Tip!

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

Comments

Loading...