Skip to content

Commit

Permalink
refactor: optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
QC2168 committed Dec 17, 2024
1 parent 909b9fb commit 2aedd32
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
13 changes: 3 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,11 @@ import type { DefaultTheme } from 'vitepress/theme';
import type { Plugin, ViteDevServer } from 'vite';
import type { SidebarPluginOptionType, UserConfig } from './types';

import { DEFAULT_IGNORE_FOLDER, log, removePrefix, getTitleFromFile, getTitleFromFileByYaml } from './utils';
import { DEFAULT_IGNORE_FOLDER, log, removePrefix, extractTitleFn } from './utils';

let option: SidebarPluginOptionType;

function extractTitleFn({ titleFromFile = false, titleFromFileByYaml = false }): ((file: string) => string | undefined) | undefined {
if (titleFromFile) {
return getTitleFromFile;
} else if (titleFromFileByYaml) {
return getTitleFromFileByYaml;
}
return undefined;
}

function createSideBarItems(
targetPath: string,
...reset: string[]
Expand Down Expand Up @@ -183,7 +176,7 @@ export default function VitePluginVitePressAutoSidebar(
const docsPath = join(process.cwd(), path);
// create sidebar data and insert
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion, @typescript-eslint/prefer-destructuring
const {themeConfig} = (config as UserConfig).vitepress.site;
const { themeConfig } = (config as UserConfig).vitepress.site;
themeConfig.sidebar =
createSidebarMulti(docsPath);
log('injected sidebar data successfully');
Expand Down
19 changes: 15 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import { existsSync, readFileSync } from 'fs';
import fm from 'front-matter';

export const DEFAULT_IGNORE_FOLDER = ['scripts', 'components', 'assets', '.vitepress'];
export function log (...info: string[]): void {
export function log(...info: string[]): void {
console.log(c.bold(c.cyan('[auto-sidebar]')), ...info);
}

// remove the file prefix
export function removePrefix (str: string, identifier: string | RegExp): string {
export function removePrefix(str: string, identifier: string | RegExp): string {
return str.replace(identifier, '');
}

// 尝试从一个md文件中读取标题,读取到第一个 ‘# 标题内容’ 的时候返回这一行
export function getTitleFromFile (realFileName: string): string | undefined {
export function getTitleFromFile(realFileName: string): string | undefined {
if (!existsSync(realFileName)) {
return undefined;
}
Expand All @@ -37,7 +37,7 @@ export function getTitleFromFile (realFileName: string): string | undefined {
}

// obtain title form yaml frontmatter
export function getTitleFromFileByYaml (realFileName: string): string | undefined {
export function getTitleFromFileByYaml(realFileName: string): string | undefined {
if (!existsSync(realFileName)) {
return undefined;
}
Expand All @@ -51,3 +51,14 @@ export function getTitleFromFileByYaml (realFileName: string): string | undefine
const content = fm<Record<string, string>>(data);
return content.attributes?.title || undefined;
}

// obtain title from file
export function extractTitleFn({ titleFromFile = false, titleFromFileByYaml = false }): ((filePath: string) => string | undefined) | undefined {
if (titleFromFile) {
return getTitleFromFile;
}
if (titleFromFileByYaml) {
return getTitleFromFileByYaml;
}
return undefined;
}

0 comments on commit 2aedd32

Please sign in to comment.