UNPKG

@sveltek/markdown

Version:

Svelte Markdown Preprocessor.

371 lines (370 loc) 9.77 kB
import * as Unified from "unified"; import { PluggableList, Plugin } from "unified"; import * as VFile from "vfile"; import * as Mdast from "mdast"; import * as Hast from "hast"; import { PreprocessorGroup, Processed } from "svelte/compiler"; export * from "unist-util-visit"; //#region src/config/types/frontmatter.d.ts type Frontmatter = Record<string, unknown> & { /** * Specifies frontmatter global data to be applied to all markdown files. * * @default undefined */ defaults?: Record<string, unknown>; /** * Specifies support for parsing Svelte `special` elements such as `svelte:head` etc. in markdown files. * * Can be enabled at the **top-level** (via config) or at the **file-level** (via frontmatter). * * If you don't plan to use them in every markdown file, it is recommended to enable the option only on those pages where you really need it. * * @default undefined */ specialElements?: boolean; /** * Specifies at the **file-level** whether plugins will be used or not. * * Useful if you want to completely disable plugins in a specific markdown file. * * @default undefined */ plugins?: { remark?: false; rehype?: false; }; /** * Specifies layout name. * * To disable the layout, simply set it to `layout: false`. * * Also, it is possible to specify at the **file-level** whether layout plugins will be used or not. * * Useful if you want to completely disable layout plugins in a specific markdown file. * * @default undefined */ layout?: string | false | { name: string; plugins?: { remark?: false; rehype?: false; }; }; /** * Specifies entry name. * * Also, it is possible to specify at the **file-level** whether entry plugins will be used or not. * * Useful if you want to completely disable entry plugins in a specific markdown file. * * @default undefined */ entry?: string | false | { name: string; plugins?: { remark?: false; rehype?: false; }; }; }; interface FrontmatterOptions { /** * Specifies frontmatter global data to be applied to all markdown files. * * @default undefined */ defaults?: Record<string, unknown>; /** * Specifies the **start/end** symbols for the frontmatter content block. * * It only works in combination with the default parser. * * @default '-' */ marker?: string; /** * Specifies a custom parser for frontmatter. * * Allows adaptation to other formats such as `TOML` or `JSON`. * * @default undefined */ parser?: (value: string) => Record<string, unknown> | void; } //#endregion //#region src/unplugins/types.d.ts type PluginList = PluggableList; interface Plugins { remark?: PluginList; rehype?: PluginList; } //#endregion //#region src/config/types/layouts.d.ts interface Layout { /** * Specifies the layout name. */ name: string; /** * Specifies the path to the layout file. */ path: string; /** * Specifies the **layout-specific** plugins that will only be applied to markdown files that use this layout. * * Also, these plugins will run after the **top-level** plugins. * * @default undefined */ plugins?: { /** * Specifies custom `remark` plugins at the **layout-level**. * * Parses source into a Markdown AST (MDAST). * * @default undefined */ remark?: PluginList; /** * Specifies custom `rehype` plugins at the **layout-level**. * * Parses source into a HTML AST (HAST). * * @default undefined */ rehype?: PluginList; }; } type Layouts = Layout[]; //#endregion //#region src/config/types/entries.d.ts interface Entry { /** * Specifies the entry name. */ name: string; /** * Specifies the **entry-specific** plugins that will only be applied to this particular markdown file. * * Also, these plugins will run after **top-level** and **layout-level** plugins. * * @default undefined */ plugins?: { /** * Specifies custom `remark` plugins at the **entry-level**. * * Parses source into a Markdown AST (MDAST). * * @default undefined */ remark?: PluginList; /** * Specifies custom `rehype` plugins at the **entry-level**. * * Parses source into a HTML AST (HAST). * * @default undefined */ rehype?: PluginList; }; } type Entries = Entry[]; //#endregion //#region src/config/types/components.d.ts interface GlobalComponent { /** * Specifies the component name. */ name: string; /** * Specifies the component path. */ path: string; /** * Specifies the component import form. * * @default 'default' */ form?: 'default' | 'named'; } type Components = GlobalComponent[]; //#endregion //#region src/config/types/options.d.ts interface SvelteMarkdownOptions { /** * Specifies custom file extensions. * * @default ['.md'] */ extensions?: string[]; /** * Specifies a custom list of preprocessors that will be applied to a Svelte file. * * @default undefined */ preprocessors?: PreprocessorGroup[]; /** * Defines frontmatter custom options. * * By default, frontmatter only supports the `YAML` format, but allows additional customization via parser. * * @default undefined */ frontmatter?: FrontmatterOptions; /** * Specifies the **top-level** plugins that will be used for all markdown files. * * @default undefined */ plugins?: { /** * Specifies custom `remark` plugins at the **top-level**. * * Parses source into a Markdown AST (MDAST). * * @default undefined */ remark?: PluginList; /** * Specifies custom `rehype` plugins at the **top-level**. * * Parses source into a HTML AST (HAST). * * @default undefined */ rehype?: PluginList; }; /** * Specifies a custom layout array. * * Layout component serves as a wrapper for the markdown files, which means the page content is displayed via the component's children prop. * * Can be enabled at the **top-level** (via config) or at the **file-level** (via frontmatter). * * @default undefined */ layouts?: Layouts; /** * Specifies a custom entry array. * * Entry serves as a special configuration for markdown files, which means it is similar to layout but without the need to create a custom component file. * * Allows unique and straightforward customization for an individual markdown file. An entry can be a page or a component. * * Can be enabled at the **top-level** (via config) or at the **file-level** (via frontmatter). * * @default undefined */ entries?: Entries; /** * Defines global components that can be used in all markdown files without manual setup. * * Especially useful for some generic components like buttons, links, images, etc. * * @default undefined */ components?: Components; } //#endregion //#region src/config/index.d.ts /** * Defines configuration via custom `options` object that contains all available settings. * * @example * * ```ts * import { defineConfig } from '@sveltek/markdown' * * export const markdownConfig = defineConfig({ * frontmatter: { * defaults: { * layout: 'default', * author: 'Sveltek', * }, * }, * layouts: [ * { * name: 'default', * path: 'lib/content/layouts/default/layout.svelte', * }, * ], * }) * ``` * * @see [Repository](https://github.com/sveltek/markdown) */ declare function defineConfig(options: SvelteMarkdownOptions): SvelteMarkdownOptions; //#endregion //#region src/utils/escape.d.ts /** * Escapes certain Svelte special characters in a string, replacing them with their corresponding HTML entity codes. * * Ensures that the string can safely be used in templates or code. * * @example * * ```ts * import { escapeSvelte } from '@sveltek/markdown' * * escapeSvelte(value) * ``` */ declare function escapeSvelte(value: string): string; //#endregion //#region src/preprocess/types/file.d.ts interface ASTScript { start: number; end: number; content: string; } interface FileData { preprocessors?: PreprocessorGroup[]; plugins?: Plugins; dependencies?: string[]; frontmatter?: Frontmatter; components?: string[]; layout?: Layout; } //#endregion //#region src/preprocess/types/preprocess.d.ts interface PreprocessOptions extends SvelteMarkdownOptions { filename?: string; /** * @experimental This option is experimental and may change at any time, so use it with caution. * * @default true */ module?: boolean; } //#endregion //#region src/preprocess/index.d.ts declare function preprocess(source: string, options?: PreprocessOptions): Promise<Processed>; //#endregion //#region src/preprocessor/index.d.ts /** * Svelte Markdown Preprocessor. * * @example * * ```ts * // svelte.config.js * import adapter from '@sveltejs/adapter-static' * import { svelteMarkdown } from '@sveltek/markdown' * * const config = { * kit: { adapter: adapter() }, * preprocess: [svelteMarkdown()], * extensions: ['.svelte', '.md'], * } * * export default config * ``` * * @see [Repository](https://github.com/sveltek/markdown) */ declare function svelteMarkdown(options?: SvelteMarkdownOptions): PreprocessorGroup; //#endregion export { ASTScript, Components, Entries, Entry, FileData, Frontmatter, FrontmatterOptions, GlobalComponent, type Hast, Layout, Layouts, type Mdast, type Plugin, PluginList, Plugins, PreprocessOptions, SvelteMarkdownOptions, type Unified, type VFile, defineConfig, escapeSvelte, preprocess, svelteMarkdown };