@netronk/mdx-remote
Version:
The remote MDX files adapter for Netronk Gen
167 lines (158 loc) • 4.51 kB
TypeScript
import { VirtualFile, PageData, Source, MetaData, Page, FileInfo } from '@netronk/gen-core/source';
import { AdvancedIndex } from '@netronk/gen-core/search/server';
type Resolver = {
type: 'local';
file: string;
} | {
type: 'github';
/**
* GitHub access token passed as option
*/
accessToken?: string;
blobUrl: string;
};
interface FileData {
resolver: Resolver;
/**
* Cached file content when `keepContent` is enabled on resolvers
*/
content?: string;
}
interface GetFilesOptions {
/**
* relative path to the content directory
*
* @defaultValue './content'
*/
directory?: string;
/**
* Which files to include
*
* When not specified, all files will be scanned
*/
include?: string | string[];
/**
* Keep the cached content on output files
*
* @defaultValue false
*/
keepContent?: boolean;
}
type ResolvedFile = (Omit<VirtualFile, 'type'> & {
type: 'meta';
}) | (Omit<VirtualFile, 'type' | 'data'> & {
type: 'page';
data: Record<string, unknown> & {
data: FileData;
};
});
declare function getLocalFiles({ directory, include, keepContent, }: GetFilesOptions): Promise<ResolvedFile[]>;
interface GetGitHubFilesOptions extends GetFilesOptions {
/**
* repository owner
*/
owner: string;
/**
* repository name
*/
repo: string;
/**
* GitHub access token
*/
accessToken: string;
/**
* Branch name or tag name
*
* @defaultValue 'main'
*/
treeSha?: string;
}
declare function getGitHubFiles({ owner, repo, accessToken, treeSha, include, directory, keepContent, }: GetGitHubFilesOptions): Promise<ResolvedFile[]>;
/**
* Choose between GitHub and Local file system based on node environment.
*
* Use GitHub in production mode, otherwise, use file system.
*/
declare function createSourceAuto<Frontmatter extends PageData>(options: GetFilesOptions & {
/**
* Options when GitHub content source is used
*/
github: Omit<GetGitHubFilesOptions, keyof GetFilesOptions> & Partial<GetFilesOptions>;
}): Promise<Source<{
pageData: Frontmatter & {
data: FileData;
};
metaData: MetaData;
}>>;
interface FetchBlobOptions {
url: string;
/**
* GitHub access token
*/
accessToken?: string;
init?: RequestInit;
}
interface FetchBlobResponse {
content: string;
encoding: BufferEncoding;
}
/**
* Fetch content of blob, result will be converted to utf-8
*/
declare function fetchBlob({ url, accessToken, init, }: FetchBlobOptions): Promise<FetchBlobResponse>;
interface FetchTreeOptions {
owner: string;
repo: string;
/**
* GitHub access token
*/
accessToken?: string;
/**
* The SHA1 value or ref (branch or tag) name of the tree.
*/
treeSha: string;
/**
*
* Setting this parameter to any value returns the objects or subtrees referenced by the tree specified in :tree_sha
* @defaultValue `false`
*/
recursive?: boolean;
init?: RequestInit;
}
interface GitTreeResponse {
sha: string;
url: string;
truncated: boolean;
tree: ({
type: 'blob';
path: string;
sha: string;
url: string;
} | {
type: 'tree';
path: string;
sha: string;
url: string;
})[];
}
declare function fetchTree({ owner, repo, treeSha, recursive, accessToken, init, }: FetchTreeOptions): Promise<GitTreeResponse>;
declare function resolveFile<Data extends {
data: FileData;
}>(page: Page<Data>): Promise<string | undefined>;
interface BuildIndexesOptions extends Omit<GetFilesOptions, 'keepContent'> {
/**
* Base url of docs
*
* @defaultValue '/'
*/
baseUrl?: string;
slug?: (info: FileInfo) => string[];
url?: (slugs: string[], locale?: string) => string;
}
/**
* This script must be executed every time the MDX files are changed to ensure search indexes are up-to-date.
*
* We recommend using 3rd party solutions like Algolia Search to handle search indexes
*/
declare function buildSearchIndexes(options: BuildIndexesOptions): Promise<AdvancedIndex[]>;
export { type BuildIndexesOptions, type FetchTreeOptions, type FileData, type GetFilesOptions, type GetGitHubFilesOptions, type GitTreeResponse, type ResolvedFile, type Resolver, buildSearchIndexes, createSourceAuto, fetchBlob, fetchTree, getGitHubFiles, getLocalFiles, resolveFile };