Files
Kamran Ahmed ee4b7305a2 Adds ByteByteGo guides and links (#106)
This PR adds all the guides from [Visual
Guides](https://bytebytego.com/guides/) section on bytebytego to the
repository with proper links.

- [x] Markdown files for guides and categories are placed inside
`data/guides` and `data/categories`
- [x] Guide links in readme are auto-generated using
`scripts/readme.ts`. Everytime you run the script `npm run
update-readme`, it reads the categories and guides from the above
mentioned folders, generate production links for guides and categories
and populate the table of content in the readme. This ensures that any
future guides and categories will automatically get added to the readme.
- [x] Sorting inside the readme matches the actual category and guides
sorting on production
2025-03-31 22:16:44 -07:00
..

import fs from 'fs'
import path from 'path'
import matter from 'gray-matter'

interface Category {
  id: string
  title: string
  sort: number
}

interface Guide {
  id: string
  title: string
  createdAt: string
  categories: string[]
}

const CATEGORIES_DIR = path.join(process.cwd(), 'data/categories')
const GUIDES_DIR = path.join(process.cwd(), 'data/guides')
const README_PATH = path.join(process.cwd(), 'README.md')

function getCategories(): Category[] {
  const files = fs.readdirSync(CATEGORIES_DIR)
  return files
    .map(file => {
      const content = fs.readFileSync(path.join(CATEGORIES_DIR, file), 'utf8')
      const { data } = matter(content)
      return {
        id: file.replace('.md', ''),
        title: data.title,
        sort: data.sort
      }
    })
    .sort((a, b) => a.sort - b.sort)
}

function getGuides(): Guide[] {
  const files = fs.readdirSync(GUIDES_DIR)
  return files
    .map(file => {
      const content = fs.readFileSync(path.join(GUIDES_DIR, file), 'utf8')
      const { data } = matter(content)
      return {
        id: file.replace('.md', ''),
        title: data.title,
        createdAt: data.createdAt,
        categories: data.categories || []
      }
    })
    .sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime())
}

function generateMarkdownList() {
  const categories = getCategories()
  const guides = getGuides()
  
  let markdown = ''
  
  categories.forEach(category => {
    markdown += `* [${category.title}](https://bytebytego.com/guides/${category.id})\n`
    
    const categoryGuides = guides.filter(guide => guide.categories.includes(category.id))
    if (categoryGuides.length > 0) {
      categoryGuides.forEach(guide => {
        markdown += `  * [${guide.title}](https://bytebytego.com/guides/${guide.id})\n`
      })
    }
  })
  
  return markdown
}

function updateReadmeToc() {
  const readmeContent = fs.readFileSync(README_PATH, 'utf8')
  const tocRegex = /<!-- TOC -->\n([\s\S]*?)\n<!-- \/TOC -->/
  const newToc = `<!-- TOC -->\n\n${generateMarkdownList()}\n\n<!-- /TOC -->`
  const updatedContent = readmeContent.replace(tocRegex, newToc)
  fs.writeFileSync(README_PATH, updatedContent)
  console.log('TOC updated successfully!')
}

updateReadmeToc()