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

reconcile-filenames-with-ids.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  1. #!/usr/bin/env node
  2. const fs = require('fs')
  3. const path = require('path')
  4. const walk = require('walk-sync')
  5. const slugger = new (require('github-slugger'))()
  6. const entities = new (require('html-entities').XmlEntities)()
  7. const frontmatter = require('../lib/read-frontmatter')
  8. const { execSync } = require('child_process')
  9. const addRedirectToFrontmatter = require('../lib/redirects/add-redirect-to-frontmatter')
  10. const contentDir = path.join(process.cwd(), 'content')
  11. const contentFiles = walk(contentDir, { includeBasePath: true, directories: false })
  12. .filter(file => {
  13. return file.endsWith('.md') &&
  14. !file.endsWith('index.md') &&
  15. !file.includes('README')
  16. })
  17. // [start-readme]
  18. //
  19. // An automated test checks for discrepancies between filenames and [autogenerated heading IDs](https://www.npmjs.com/package/remark-autolink-headings).
  20. // If the test fails, a human needs to run this script to update the filenames.
  21. //
  22. // **This script is not currently supported on Windows.**
  23. //
  24. // [end-readme]
  25. // TODO fix path separators in the redirect
  26. if (process.platform.startsWith('win')) {
  27. console.log('This script cannot be run on Windows at this time! Exiting...')
  28. process.exit()
  29. }
  30. contentFiles.forEach(oldFullPath => {
  31. const { data, content } = frontmatter(fs.readFileSync(oldFullPath, 'utf8'))
  32. // skip pages with frontmatter flag
  33. if (data.allowTitleToDifferFromFilename) return
  34. // slugify the title of each article
  35. // where title = Foo bar
  36. // and slug = foo-bar
  37. slugger.reset()
  38. const slug = slugger.slug(entities.decode(data.title))
  39. // get the basename of each file
  40. // where file = content/foo-bar.md
  41. // and basename = foo-bar
  42. const basename = path.basename(oldFullPath, '.md')
  43. // if slug and basename match, return early
  44. if (basename === slug) return
  45. // otherwise rename the file using the slug
  46. const newFullPath = oldFullPath.replace(basename, slug)
  47. const oldContentPath = path.relative(process.cwd(), oldFullPath)
  48. const newContentPath = path.relative(process.cwd(), newFullPath)
  49. const gitStatusOfFile = execSync(`git status --porcelain ${oldContentPath}`).toString()
  50. // if file is untracked, do a regular mv; otherwise do a git mv
  51. if (gitStatusOfFile.includes('??')) {
  52. execSync(`mv ${oldContentPath} ${newContentPath}`)
  53. } else {
  54. execSync(`git mv ${oldContentPath} ${newContentPath}`)
  55. }
  56. // then add the old path to the redirect_from frontmatter
  57. // TODO fix path separators on Windows (e.g. \github\extending-github\about-webhooks)
  58. const redirect = path.join('/', path.relative(contentDir, oldFullPath).replace(/.md$/, ''))
  59. data.redirect_from = addRedirectToFrontmatter(data.redirect_from, redirect)
  60. // update the file
  61. fs.writeFileSync(newFullPath, frontmatter.stringify(content, data))
  62. })
Tip!

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

Comments

Loading...