UNPKG

next-validate-link

Version:

An utility to validate links in markdown file

204 lines 5.7 kB
import { RouteConfig } from "@react-router/dev/routes"; import { RootContent } from "mdast"; import { PluggableList } from "unified"; //#region src/presets/react-router.d.ts interface ReactRouterScanOptions extends ScanOptions { preset: "react-router"; routerConfig: RouteConfig; } //#endregion //#region src/scan.d.ts type PopulateParams = Record<string, { value?: string[] | string | Record<string, string[] | string>; hashes?: string[]; queries?: Record<string, string>[]; }[]>; interface ScanOptions { /** * path of pages (e.g. `/docs/page.tsx`) * * App Router format only **/ pages?: string[]; cwd?: string; populate?: PopulateParams; meta?: Record<string, UrlMeta>; extensions?: string[]; } type ScanPresetOptions = (ScanOptions & { /** * @default next */ preset?: "next" | "astro" | "nuxt" | "waku" | "tanstack-start"; }) | ReactRouterScanOptions; interface ScanResult { urls: Map<string, UrlMeta>; fallbackUrls: { url: RegExp; meta: UrlMeta; }[]; } interface UrlMeta { hashes?: string[]; queries?: Record<string, string>[]; } declare function scanURLs(options?: ScanPresetOptions): Promise<ScanResult>; //#endregion //#region src/sample.d.ts type PathToUrl = (path: string) => string | undefined; declare function readFileFromPath(file: string, pathToUrl?: PathToUrl): Promise<FileObject>; declare function readFiles(patterns: string | readonly string[], options?: Partial<{ pathToUrl?: PathToUrl; }>): Promise<FileObject[]>; //#endregion //#region src/validate/markdown.d.ts interface MarkdownConfig { /** * scan href from attributes in MDX components */ components?: Record<string, { attributes: string[]; }>; remarkPlugins?: PluggableList; /** * control how URLs are scanned from Markdown content, override all default behaviours. */ onNode?: (node: RootContent) => { hrefs: string[]; }; } //#endregion //#region src/utils/external-link.d.ts interface ExternalLinkConfig { validate?: (url: URL) => Promise<ExternalLinkResult>; } type ExternalLinkResult = { success: true; } | { success: false; message?: string; }; //#endregion //#region src/validate.d.ts interface ValidateResult { file: string; /**] * @deprecated use `errors` instead */ detected: DetectedError[]; errors: ValidateError[]; } interface ValidateError { url: string; line: number; column: number; reason: ErrorReason | Error; } type ErrorReason = "not-found" | "invalid-fragment" | "invalid-query"; interface ResolutionConfig { /** * Base URL to resolve relative URLs */ baseUrl?: string; /** * Base directory to resolve relative file paths */ baseDir?: string; /** * Generate URL from file paths, used for relative file path detection. * * Default to searching in input files. */ pathToUrl?: PathToUrl; } interface DetectorConfig { /** * Available URLs (including hashes and query parameters) */ scanned: ScanResult; /** * don't validate the fragment/hash of URLs * * @defaultValue false */ ignoreFragment?: boolean; /** * don't validate the query of URLs * * @defaultValue false */ ignoreQuery?: boolean; /** * Check external urls * * @defaultValue false */ checkExternal?: boolean | ExternalLinkConfig; /** * Check relative paths (e.g. `[My File](./my-file.md)`) * * - `exists`: ensure the file exists. * - `as-url`: resolve & check the public URL of referenced file, requires one of these to be defined: * - `pathToUrl` option. * - `file.url` in input file objects. * - `false` (default): ignore. */ checkRelativePaths?: "exists" | "as-url" | false; /** * Check relative URLs (e.g. `[My File](./my-page)`) * * - `true` (default): resolve & check the relative URL, requires one of these to be defined: * - `file.url` in input file objects. * - `pathToUrl` option. * - `baseUrl` option. * - `false`: ignore. */ checkRelativeUrls?: boolean; /** * Allowed hrefs, can be: * - a list of hrefs * - a function that returns `true` for allowed href */ whitelist?: string[] | ((url: string) => boolean); /** * Determinate the type of pathname */ determinatePathname?: (pathname: string) => Awaitable<PathnameType>; } interface ValidateConfig extends ResolutionConfig, DetectorConfig { markdown?: MarkdownConfig; } type PathnameType = "url" | "relative-file-path" | "relative-url"; type Awaitable<T> = T | Promise<T>; interface FileObject { path: string; content: string; data?: object; /** * URL of page, required for relative url detection */ url?: string; } /** * Validate markdown files * * @param files - file paths or file objects * @param config - configurations */ declare function validateFiles(files: (string | FileObject)[], config: ValidateConfig): Promise<ValidateResult[]>; interface Detector { detect: (href: string, resolution: ResolutionConfig) => Promise<{ type: "error"; reason: Error | ErrorReason; } | undefined>; } type DetectedError = [url: string, line: number, column: number, reason: ErrorReason | Error]; //#endregion //#region src/print.d.ts /** * Print validation errors */ declare function printErrors(results: ValidateResult[], throwError?: boolean): void; //#endregion export { DetectedError, Detector, DetectorConfig, ErrorReason, type ExternalLinkConfig, type ExternalLinkResult, FileObject, PathToUrl, PopulateParams, ResolutionConfig, ScanOptions, ScanPresetOptions, ScanResult, UrlMeta, ValidateConfig, ValidateError, ValidateResult, printErrors, readFileFromPath, readFiles, scanURLs, validateFiles }; //# sourceMappingURL=index.d.mts.map