<commit_before>import * as fs from 'fs';
import * as path from 'path';

import { BindOption } from 'typedoc';
import { Component } from 'typedoc/dist/lib/converter/components';
import { RendererComponent } from 'typedoc/dist/lib/output/components';
import { RendererEvent } from 'typedoc/dist/lib/output/events';

import { SidebarItem, SidebarOptions } from './types';

@Component({ name: 'sidebar' })
export class SidebarComponent extends RendererComponent {
  @BindOption('sidebar')
  sidebar!: SidebarOptions;
  @BindOption('siteDir')
  siteDir!: string;
  @BindOption('out')
  out!: string;

  initialize() {
    this.listenTo(this.application.renderer, {
      [RendererEvent.BEGIN]: this.onRendererBegin,
    });
  }

  onRendererBegin(renderer: RendererEvent) {
    const sidebarPath = path.resolve(this.siteDir, this.sidebar.sidebarFile);
    const navigation = this.application.renderer.theme?.getNavigation(
      renderer.project,
    );

    // map the navigation object to a Docuaurus sidebar format
    const sidebarItems = navigation?.children
      ? navigation.children.map((navigationItem) => {
          if (navigationItem.title === 'Modules' || navigationItem.isLabel) {
            const sidebarCategoryItems = navigationItem.children
              ? navigationItem.children.map((navItem) => {
                  const url = this.getUrlKey(this.out, navItem.url);
                  if (navItem.children && navItem.children.length > 0) {
                    const sidebarCategoryChildren = navItem.children.map(
                      (childGroup) =>
                        this.sidebarCategory(
                          childGroup.title,
                          childGroup.children
                            ? childGroup.children.map((childItem) =>
                                this.getUrlKey(this.out, childItem.url),
                              )
                            : [],
                        ),
                    );
                    return this.sidebarCategory(navItem.title, [
                      url,
                      ...sidebarCategoryChildren,
                    ]);
                  }
                  return url;
                })
              : [];
            return this.sidebarCategory(
              navigationItem.title,
              sidebarCategoryItems,
            );
          }
          return this.getUrlKey(this.out, navigationItem.url);
        })
      : [];

    // write result to disk
    fs.writeFileSync(
      sidebarPath,
      `module.exports = ${JSON.stringify(sidebarItems, null, 2)};`,
    );

    this.application.logger.success(
      `TypeDoc sidebar written to ${sidebarPath}`,
    );
  }

  /**
   * returns a sidebar category node
   */
  sidebarCategory(title: string, items: SidebarItem[]) {
    return {
      type: 'category',
      label: title,
      items,
    };
  }

  /**
   * returns the url key for relevant doc
   */
  getUrlKey(out: string, url: string) {
    const urlKey = url.replace('.md', '');
    return out ? out + '/' + urlKey : urlKey;<commit_msg>Check if sidebar path exists<commit_after>import * as fs from 'fs';
import * as path from 'path';

import { BindOption } from 'typedoc';
import { Component } from 'typedoc/dist/lib/converter/components';
import { RendererComponent } from 'typedoc/dist/lib/output/components';
import { RendererEvent } from 'typedoc/dist/lib/output/events';

import { SidebarItem, SidebarOptions } from './types';

@Component({ name: 'sidebar' })
export class SidebarComponent extends RendererComponent {
  @BindOption('sidebar')
  sidebar!: SidebarOptions;
  @BindOption('siteDir')
  siteDir!: string;
  @BindOption('out')
  out!: string;

  initialize() {
    this.listenTo(this.application.renderer, {
      [RendererEvent.BEGIN]: this.onRendererBegin,
    });
  }

  onRendererBegin(renderer: RendererEvent) {
    const sidebarPath = path.resolve(this.siteDir, this.sidebar.sidebarFile);
    const navigation = this.application.renderer.theme?.getNavigation(
      renderer.project,
    );

    // map the navigation object to a Docuaurus sidebar format
    const sidebarItems = navigation?.children
      ? navigation.children.map((navigationItem) => {
          if (navigationItem.title === 'Modules' || navigationItem.isLabel) {
            const sidebarCategoryItems = navigationItem.children
              ? navigationItem.children.map((navItem) => {
                  const url = this.getUrlKey(this.out, navItem.url);
                  if (navItem.children && navItem.children.length > 0) {
                    const sidebarCategoryChildren = navItem.children.map(
                      (childGroup) =>
                        this.sidebarCategory(
                          childGroup.title,
                          childGroup.children
                            ? childGroup.children.map((childItem) =>
                                this.getUrlKey(this.out, childItem.url),
                              )
                            : [],
                        ),
                    );
                    return this.sidebarCategory(navItem.title, [
                      url,
                      ...sidebarCategoryChildren,
                    ]);
                  }
                  return url;
                })
              : [];
            return this.sidebarCategory(
              navigationItem.title,
              sidebarCategoryItems,
            );
          }
          return this.getUrlKey(this.out, navigationItem.url);
        })
      : [];

    // write result to disk
    if (!fs.existsSync(path.dirname(sidebarPath))) {
      fs.mkdirSync(path.dirname(sidebarPath));
    }
    fs.writeFileSync(
      sidebarPath,
      `module.exports = ${JSON.stringify(sidebarItems, null, 2)};`,
    );

    this.application.logger.success(
      `TypeDoc sidebar written to ${sidebarPath}`,
    );
  }

  /**
   * returns a sidebar category node
   */
  sidebarCategory(title: string, items: SidebarItem[]) {
    return {
      type: 'category',
      label: title,
      items,
    };
  }

  /**
   * returns the url key for relevant doc
   */
  getUrlKey(out: string, url: string) {
    const urlKey = url.replace('.md', '');
    return out ? out + '/' + urlKey : urlKey;
{'fraction_non_alphanumeric': 0.1146049481245012, 'fraction_numerical': 0.0006384676775738228, 'mean_word_length': 1.8611872146118722, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '365091', 'n_tokens_mistral': 1687, 'n_tokens_neox': 1619, 'n_words': 334}