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

move-category-to-product.js 3.1 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
  1. #!/usr/bin/env node
  2. const fs = require('fs')
  3. const path = require('path')
  4. const mkdirp = require('mkdirp').sync
  5. const program = require('commander')
  6. const { execSync } = require('child_process')
  7. const frontmatter = require('../lib/read-frontmatter')
  8. const addRedirectToFrontmatter = require('./helpers/add-redirect-to-frontmatter')
  9. const walkFiles = require('./helpers/walk-files')
  10. const contentFiles = walkFiles('content', '.md')
  11. const contentDir = path.posix.join(process.cwd(), 'content')
  12. // [start-readme]
  13. //
  14. // Move the files from a category directory to a top-level product and add redirects.
  15. //
  16. // [end-readme]
  17. program
  18. .description('Move a category-level docs set to the product level.')
  19. .requiredOption('-c, --category <PATH>', 'Provide the path of the existing category, e.g., github/github-pages')
  20. .requiredOption('-p, --product <PATH>', 'Provide the path of the new product, e.g., pages')
  21. .parse(process.argv)
  22. const oldCategory = program.opts().category.replace('content/', '')
  23. const newProduct = program.opts().product.replace('content/', '')
  24. const [oldProductId, oldCategoryId] = oldCategory.split('/')
  25. const oldCategoryPath = path.posix.join(contentDir, oldCategory)
  26. const oldProductPath = path.posix.join(contentDir, oldProductId)
  27. if (!fs.existsSync(oldProductPath)) {
  28. console.error(`Error! Can't find ${oldProductPath}`)
  29. process.exit(1)
  30. }
  31. const oldCategoryFiles = contentFiles.filter(file => file.includes(`/${oldCategoryId}/`))
  32. if (!oldCategoryFiles.length) {
  33. console.error(`Error! Can't find ${oldCategory} files`)
  34. process.exit(1)
  35. }
  36. const newProductPath = path.posix.join(process.cwd(), 'content', newProduct)
  37. main()
  38. function main () {
  39. // Create the new product dir.
  40. mkdirp(newProductPath)
  41. // Add redirects to the frontmatter of the to-be-moved files.
  42. oldCategoryFiles.forEach(file => {
  43. const { content, data } = frontmatter(fs.readFileSync(file, 'utf8'))
  44. const redirectString = file
  45. .replace(contentDir, '')
  46. .replace('index.md', '')
  47. .replace('.md', '')
  48. data.redirect_from = addRedirectToFrontmatter(data.redirect_from, redirectString)
  49. fs.writeFileSync(file, frontmatter.stringify(content, data, { lineWidth: 10000 }))
  50. })
  51. // Move the files.
  52. execSync(`git mv ${oldCategoryPath}/* ${newProductPath}`)
  53. // Remove the category from the old product TOC.
  54. const oldProductTocPath = path.posix.join(oldProductPath, 'index.md')
  55. const productToc = frontmatter(fs.readFileSync(oldProductTocPath, 'utf8'))
  56. productToc.data.children = productToc.data.children.filter(child => child !== `/${oldCategoryId}`)
  57. fs.writeFileSync(oldProductTocPath, frontmatter.stringify(productToc.content, productToc.data, { lineWidth: 10000 }))
  58. // Add the new product to the homepage TOC.
  59. const homepage = path.posix.join(contentDir, 'index.md')
  60. const homepageToc = frontmatter(fs.readFileSync(homepage, 'utf8'))
  61. homepageToc.data.children.push(newProduct)
  62. fs.writeFileSync(homepage, frontmatter.stringify(homepageToc.content, homepageToc.data, { lineWidth: 10000 }))
  63. console.log(`Moved ${oldCategory} files to ${newProduct}, added redirects, and updated TOCs!`)
  64. }
Tip!

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

Comments

Loading...