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

get-link-data.js 1.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
  1. const path = require('path')
  2. const findPage = require('./find-page')
  3. const nonEnterpriseDefaultVersion = require('./non-enterprise-default-version')
  4. const removeFPTFromPath = require('./remove-fpt-from-path')
  5. const renderContent = require('./render-content')
  6. // rawLinks is an array of paths: [ '/foo' ]
  7. // we need to convert it to an array of localized objects: [ { href: '/en/foo', title: 'Foo', intro: 'Description here' } ]
  8. module.exports = async (rawLinks, context, option = { title: true, intro: true }) => {
  9. if (!rawLinks) return
  10. if (typeof rawLinks === 'string') {
  11. return await processLink(rawLinks, context, option)
  12. }
  13. const links = []
  14. for (const link of rawLinks) {
  15. const linkObj = await processLink(link, context, option)
  16. if (!linkObj) {
  17. continue
  18. } else {
  19. links.push(linkObj)
  20. }
  21. }
  22. return links
  23. }
  24. const processLink = async (link, context, option) => {
  25. const opts = { textOnly: true, encodeEntities: true }
  26. // Parse the link in case it includes Liquid conditionals
  27. const linkPath = await renderContent((link.href || link), context, opts)
  28. if (!linkPath) return null
  29. const version = context.currentVersion === 'homepage' ? nonEnterpriseDefaultVersion : context.currentVersion
  30. const href = removeFPTFromPath(path.join('/', context.currentLanguage, version, linkPath))
  31. const linkedPage = findPage(href, context.pages, context.redirects)
  32. if (!linkedPage) return null
  33. const result = { href, page: linkedPage }
  34. if (option.title) {
  35. result.title = await linkedPage.renderTitle(context, opts)
  36. }
  37. if (option.intro) {
  38. result.intro = await linkedPage.renderProp('intro', context, opts)
  39. }
  40. return result
  41. }
Tip!

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

Comments

Loading...