import { entries, fromEntries, isArray, isFunction, isPlainObject, normalizePath, } from '@vuepress/helper' import type { App, Page } from 'vuepress/core' import type { RedirectPluginFrontmatter, RedirectPluginOptions, } from './types/index.js' export const getRedirectMap = ( app: App, options: RedirectPluginOptions, ): Record => { const config = isFunction(options.config) ? options.config(app) : isPlainObject(options.config) ? options.config : {} return { ...fromEntries( ( app.pages as Page, RedirectPluginFrontmatter>[] ).flatMap(({ frontmatter, path }) => isArray(frontmatter.redirectFrom) ? frontmatter.redirectFrom.map((from) => [ normalizePath(from, true), path, ]) : frontmatter.redirectFrom ? [[normalizePath(frontmatter.redirectFrom, true), path]] : [], ), ), ...fromEntries( entries(config).map(([from, to]) => [ normalizePath(from, true), normalizePath(to), ]), ), } }