github.com/igoogolx/clash@v1.19.8/docs/.vitepress/locales/side_bar.ts (about)

     1  import directoryTree from 'directory-tree'
     2  import fs from 'fs'
     3  import metadataParser from 'markdown-yaml-metadata-parser'
     4  
     5  function getMetadataFromDoc(path: string): { sidebarTitle?: string, sidebarOrder?: number } {
     6    const fileContents = fs.readFileSync(path, 'utf8')
     7  
     8    return metadataParser(fileContents).metadata
     9  }
    10  
    11  export function generateSidebarChapter(locale:string, chapterDirName: Map<string,string>): any[] {
    12    if (chapterDirName.size < 1) {
    13      console.error(chapterDirName)
    14      throw new Error(`Could not genereate sidebar: chapterDirName is empty`)
    15    }
    16  
    17    var chapterPath = ''
    18    var sidebar: any[] = []
    19  
    20    for (const chapterDirKey of chapterDirName.keys()) {
    21      if (locale !== 'en_US') {
    22        chapterPath = `./${locale}/${chapterDirKey}`
    23      } else {
    24        chapterPath = `./${chapterDirKey}`
    25      }
    26  
    27      const tree = directoryTree(chapterPath)
    28  
    29      if (!tree || !tree.children) {
    30        console.error(tree)
    31        throw new Error(`Could not genereate sidebar: invalid chapter at ${chapterPath}`)
    32      }
    33  
    34      let items: { sidebarOrder: number, text: string, link: string }[] = []
    35  
    36      // Look into files in the chapter
    37      for (const doc of tree.children) {
    38        // make sure it's a .md file
    39        if (doc.children || !doc.name.endsWith('.md'))
    40          continue
    41  
    42        const { sidebarOrder, sidebarTitle } = getMetadataFromDoc(doc.path)
    43  
    44        if (!sidebarOrder)
    45          throw new Error('Cannot find sidebarOrder in doc metadata: ' + doc.path)
    46  
    47        if (!sidebarTitle)
    48          throw new Error('Cannot find sidebarTitle in doc metadata: ' + doc.path)
    49  
    50        if (chapterDirKey === 'introduction' && doc.name === '_dummy-index.md') {
    51          // Override index page link
    52          items.push({
    53            sidebarOrder,
    54            text: sidebarTitle,
    55            link: '/' + (locale === 'en_US' ? '' : locale + '/') 
    56          })
    57        } else {
    58          items.push({
    59            sidebarOrder,
    60            text: sidebarTitle,
    61            link: "/" + doc.path
    62          })
    63        }
    64      }
    65  
    66      items = items.sort((a, b) => a.sidebarOrder - b.sidebarOrder)
    67  
    68      // remove dash and capitalize first character of each word as chapter title
    69      const text = chapterDirName.get(chapterDirKey) || chapterDirKey.split('-').join(' ').replace(/\b\w/g, l => l.toUpperCase())
    70      sidebar.push({
    71        text,
    72        collapsed: false,
    73        items,
    74      })
    75    }
    76  
    77    return sidebar
    78  }