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

update-developer-site-links.js 2.6 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
  1. #!/usr/bin/env node
  2. const fs = require('fs')
  3. const path = require('path')
  4. const walk = require('walk-sync')
  5. const frontmatter = require('../../lib/read-frontmatter')
  6. const { loadPages, loadPageMap } = require('../../lib/pages')
  7. const patterns = require('../../lib/patterns')
  8. const loadRedirects = require('../../lib/redirects/precompile')
  9. const allVersions = Object.keys(require('../../lib/all-versions'))
  10. // get all content and data files
  11. const files = ['content', 'data'].map(dir => {
  12. return walk(path.join(process.cwd(), dir), { includeBasePath: true, directories: false })
  13. .filter(file => file.endsWith('.md') && !file.endsWith('README.md'))
  14. }).flat()
  15. // match [foo](/v3) and [bar](/v4) Markdown links
  16. const linkRegex = /\(\/v[34].*?\)/g
  17. main()
  18. async function main () {
  19. // we need to load the pages so we can get the redirects
  20. const englishPages = (await loadPages()).filter(p => p.languageCode === 'en')
  21. const englishPageMap = await loadPageMap(englishPages)
  22. const redirects = await loadRedirects(englishPages, englishPageMap)
  23. for (const file of files) {
  24. const { data, content } = frontmatter(fs.readFileSync(file, 'utf8'))
  25. const links = content.match(linkRegex)
  26. if (!links) continue
  27. // remove parentheses: (/v3) -> /v3
  28. // also remove trailing slash before closing parens if there is one
  29. const devLinks = links
  30. .map(link => link.replace('(', '').replace(/\/?\)/, ''))
  31. let newContent = content
  32. for (const devLink of devLinks) {
  33. const [link, fragment] = devLink.split(/\/?#/)
  34. let redirect = redirects[link]
  35. if (!redirect) {
  36. console.log(`no redirect found for ${devLink} in ${file}`)
  37. continue
  38. }
  39. // do some cleanup
  40. redirect = redirect
  41. // remove language code segment
  42. .replace(patterns.getLanguageCode, '')
  43. // remove version segment
  44. .replace(new RegExp(`/(${allVersions.join('|')})`), '')
  45. // re-add the fragment after removing any fragment added via the redirect
  46. // otherwise /v3/git/refs/#create-a-reference will become /rest/reference/git#refs#create-a-reference
  47. // we want to preserve the #create-a-reference fragment, not #refs
  48. const newLink = fragment
  49. ? redirect.replace(/#.+?$/, '') + '#' + fragment
  50. : redirect
  51. // first replace the old link with the new link
  52. // then remove any trailing slashes
  53. newContent = newContent
  54. .replace(new RegExp(`${devLink}/?(?=\\))`), newLink)
  55. }
  56. fs.writeFileSync(file, frontmatter.stringify(newContent, data, { lineWidth: 10000 }))
  57. }
  58. console.log('Done!')
  59. }
Tip!

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

Comments

Loading...