#!/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