Skip to content

Commit

Permalink
feat(plugin-notes-data): 支持 数组形式的插件配置项
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhanbo committed Jan 4, 2024
1 parent 1fe16bd commit 66f1f03
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
2 changes: 1 addition & 1 deletion plugins/plugin-notes-data/src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getDirname, path } from '@vuepress/utils'
import type { NotesDataOptions } from '../shared/index.js'
import { prepareNotesData, watchNotesData } from './prepareNotesData.js'

export function notesDataPlugin(options: NotesDataOptions): Plugin {
export function notesDataPlugin(options: NotesDataOptions | NotesDataOptions[]): Plugin {
return (app: App) => {
return {
name: '@vuepress-plume/plugin-notes-data',
Expand Down
48 changes: 28 additions & 20 deletions plugins/plugin-notes-data/src/node/prepareNotesData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createFilter } from 'create-filter'
import type {
NotesData,
NotesDataOptions,
NotesItem,
NotesItemOptions,
NotesSidebar,
NotesSidebarItem,
} from '../shared/index.js'
Expand All @@ -32,10 +32,11 @@ interface NotePage {
link: string
}

export async function prepareNotesData(app: App, { include, exclude, notes, dir, link }: NotesDataOptions) {
function resolvedNotesData(app: App, options: NotesDataOptions, result: NotesData) {
const { include, exclude, notes, dir: _dir, link } = options
if (!notes || notes.length === 0)
return
dir = normalizePath(dir)
const dir = normalizePath(_dir)
const filter = createFilter(ensureArray(include), ensureArray(exclude), {
resolve: false,
})
Expand All @@ -47,23 +48,27 @@ export async function prepareNotesData(app: App, { include, exclude, notes, dir,
&& page.filePathRelative.startsWith(dir)
&& filter(page.filePathRelative),
)
.map((page) => {
return {
relativePath: page.filePathRelative?.replace(DIR_PATTERN, '') || '',
title: page.title,
link: page.path,
}
})

const notesData: NotesData = {}
.map(page => ({
relativePath: page.filePathRelative?.replace(DIR_PATTERN, '') || '',
title: page.title,
link: page.path,
}))
notes.forEach((note) => {
notesData[normalizePath(path.join('/', link, note.link))] = initSidebar(
result[normalizePath(path.join('/', link, note.link))] = initSidebar(
note,
notesPageList.filter(page =>
page.relativePath.startsWith(note.dir.trim().replace(/^\/|\/$/g, '')),
),
)
})
}

export async function prepareNotesData(app: App, options: NotesDataOptions | NotesDataOptions[]) {
const notesData: NotesData = {}
const allOptions = ensureArray<NotesDataOptions>(options)

allOptions.forEach(option => resolvedNotesData(app, option, notesData))

let content = `
export const notesData = ${JSON.stringify(notesData, null, 2)}
`
Expand All @@ -73,22 +78,25 @@ export const notesData = ${JSON.stringify(notesData, null, 2)}
await app.writeTemp('internal/notesData.js', content)
}

export function watchNotesData(app: App, watchers: any[], options: NotesDataOptions): void {
if (!options.notes || options.notes.length === 0 || !options.dir)
return
const dir = path.join('pages', options.dir, '**/*')
export function watchNotesData(app: App, watchers: any[], options: NotesDataOptions | NotesDataOptions[]): void {
const allOptions = ensureArray<NotesDataOptions>(options)
const [firstLink, ...links] = allOptions.map(option => option.link)

const dir = path.join('pages', firstLink, '**/*')
const watcher = chokidar.watch(dir, {
cwd: app.dir.temp(),
ignoreInitial: true,
})

links.length && watcher.add(links.map(link => path.join('pages', link, '**/*')))

watcher.on('add', () => prepareNotesData(app, options))
watcher.on('change', () => prepareNotesData(app, options))
watcher.on('unlink', () => prepareNotesData(app, options))
watchers.push(watcher)
}

function initSidebar(note: NotesItem, pages: NotePage[]): NotesSidebarItem[] {
function initSidebar(note: NotesItemOptions, pages: NotePage[]): NotesSidebarItem[] {
if (!note.sidebar)
return []
if (note.sidebar === 'auto')
Expand All @@ -97,7 +105,7 @@ function initSidebar(note: NotesItem, pages: NotePage[]): NotesSidebarItem[] {
}

function initSidebarByAuto(
note: NotesItem,
note: NotesItemOptions,
pages: NotePage[],
): NotesSidebarItem[] {
pages = pages.sort((prev, next) => {
Expand Down Expand Up @@ -135,7 +143,7 @@ function initSidebarByAuto(
}

function initSidebarByConfig(
{ text, dir, sidebar }: NotesItem,
{ text, dir, sidebar }: NotesItemOptions,
pages: NotePage[],
): NotesSidebarItem[] {
return (sidebar as NotesSidebar).map((item) => {
Expand Down
4 changes: 3 additions & 1 deletion plugins/plugin-notes-data/src/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ export interface NotesDataOptions {
link: string
include?: string | string[]
exclude?: string | string[]
notes: NotesItem[]
notes: NotesItemOptions[]
}

export type NotesItemOptions = (Omit<NotesItem, 'text'> & { text?: string })

export interface NotesItem {
dir: string
link: string
Expand Down

0 comments on commit 66f1f03

Please sign in to comment.