1
0
mirror of synced 2025-12-22 03:16:52 -05:00
Files
docs/script/content-migrations/remove-map-topics.js
Kevin Heis 42e785b0a8 Migrate CommonJS to ESM (#20301)
* First run of script

* Get the app running --- ish

* Get NextJS working

* Remove `node:`

* Get more tests passing in unit directory

* Update FailBot test to use nock

* Update test.yml

* Update Dockerfile

* tests/content fixes

* Update page.js

* Update build-changelog.js

* updating tests/routing

* Update orphan-tests.js

* updating tests/rendering

* Update .eslintrc.js

* Update .eslintrc.js

* Install jest/globals

* "linting" tests

* staging update to server.mjs

* Change '.github/allowed-actions.js' to a ESM export

* Lint

* Fixes for the main package.json

* Move Jest to be last in the npm test command so we can pass args

* Just use 'npm run lint' in the npm test command

* update algolia label script

* update openapi script

* update require on openapi

* Update enterprise-algolia-label.js

* forgot JSON.parse

* Update lunr-search-index.js

* Always explicitly include process.cwd() for JSON file reads pathed from project root

* update graphql/update-files.js script

* Update other npm scripts using jest to pass ESM NODE_OPTIONS

* Update check-for-enterprise-issues-by-label.js for ESM

* Update create-enterprise-issue.js for ESM

* Import jest global for browser tests

* Convert 'script/deploy' to ESM

Co-authored-by: Grace Park <gracepark@github.com>
Co-authored-by: James M. Greene <jamesmgreene@github.com>
2021-07-14 13:49:18 -07:00

105 lines
4.0 KiB
JavaScript
Executable File

#!/usr/bin/env node
import fs from 'fs'
import path from 'path'
import walk from 'walk-sync'
import stripHtmlComments from 'strip-html-comments'
import languages from '../../lib/languages.js'
import frontmatter from '../../lib/read-frontmatter.js'
import addRedirectToFrontmatter from '../helpers/add-redirect-to-frontmatter.js'
const relativeRefRegex = /\/[a-zA-Z0-9-]+/g
const linkString = /{% [^}]*?link.*? \/(.*?) ?%}/m
const linksArray = new RegExp(linkString.source, 'gm')
const walkOpts = {
includeBasePath: true,
directories: false
}
// We only want category TOC files, not product TOCs.
const categoryFileRegex = /content\/[^/]+?\/[^/]+?\/index.md/
const fullDirectoryPaths = Object.values(languages).map(langObj => path.join(process.cwd(), langObj.dir, 'content'))
const categoryIndexFiles = fullDirectoryPaths.map(fullDirectoryPath => walk(fullDirectoryPath, walkOpts)).flat()
.filter(file => categoryFileRegex.test(file))
categoryIndexFiles.forEach(categoryIndexFile => {
let categoryIndexContent = fs.readFileSync(categoryIndexFile, 'utf8')
if (categoryIndexFile.endsWith('github/getting-started-with-github/index.md')) {
categoryIndexContent = stripHtmlComments(categoryIndexContent.replace(/\n<!--/g, '<!--'))
}
// find array of TOC link strings
const rawItems = categoryIndexContent.match(linksArray)
if (!rawItems || !rawItems[0].includes('topic_link_in_list')) return
const pageToc = {}
let currentTopic = ''
// Create an object of topics and articles
rawItems.forEach(tocItem => {
const relativePath = tocItem.match(relativeRefRegex).pop().replace('/', '')
if (tocItem.includes('topic_link_in_list')) {
currentTopic = relativePath
pageToc[relativePath] = []
} else {
const tmpArray = pageToc[currentTopic]
tmpArray.push(relativePath)
pageToc[currentTopic] = tmpArray
}
})
for (const topic in pageToc) {
const oldTopicDirectory = path.dirname(categoryIndexFile)
const newTopicDirectory = path.join(oldTopicDirectory, topic)
const oldTopicFile = path.join(oldTopicDirectory, `${topic}.md`)
// Some translated category TOCs may be outdated and contain incorrect links
if (!fs.existsSync(oldTopicFile)) continue
if (!fs.existsSync(newTopicDirectory)) fs.mkdirSync(newTopicDirectory)
const { data, content } = frontmatter(fs.readFileSync(oldTopicFile, 'utf8'))
delete data.mapTopic
let topicContent = content
const articles = pageToc[topic]
articles.forEach(article => {
// Update the new map topic index file content
topicContent = topicContent + `{% link_with_intro /${article} %}\n`
// Update the category index file content
categoryIndexContent = categoryIndexContent.replace(`{% link_in_list /${article}`, `{% link_in_list /${topic}/${article}`)
// Early return if the article doesn't exist (some translated category TOCs may be outdated and contain incorrect links)
if (!fs.existsSync(`${oldTopicDirectory}/${article}.md`)) return
// Move the file under the new map topic directory
const newArticlePath = `${newTopicDirectory}/${article}.md`
fs.renameSync(`${oldTopicDirectory}/${article}.md`, newArticlePath)
// Read the article file so we can add a redirect from its old path
const articleContents = frontmatter(fs.readFileSync(newArticlePath, 'utf8'))
if (!articleContents.data.redirect_from) articleContents.data.redirect_from = []
addRedirectToFrontmatter(articleContents.data.redirect_from, `${oldTopicDirectory.replace(/^.*?\/content\//, '/')}/${article}`)
// Write the article with updated frontmatter
fs.writeFileSync(newArticlePath, frontmatter.stringify(articleContents.content.trim(), articleContents.data, { lineWidth: 10000 }))
})
// Write the map topic index file
fs.writeFileSync(`${newTopicDirectory}/index.md`, frontmatter.stringify(topicContent.trim(), data, { lineWidth: 10000 }))
// Write the category index file
fs.writeFileSync(categoryIndexFile, categoryIndexContent)
// Delete the old map topic
fs.unlinkSync(oldTopicFile)
}
})