@mdfriday/foundry
Version:
The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.
349 lines • 9.72 kB
TypeScript
import { Content } from "../../domain/markdown/vo/content";
export interface FrontMatterData {
params: Record<string, any>;
format: 'yaml' | 'toml' | 'json' | 'org';
}
export interface Shortcode {
/** Shortcode name */
name: string;
/** Parameters */
params: string[] | Record<string, any> | null;
/** Position in source */
pos: number;
/** Length in source */
length: number;
rawContent: string;
/** Placeholder for shortcode in content */
placeholder: string;
/** Whether it's inline */
inline: boolean;
/** Whether it's closed */
closed: boolean;
/** Inner content for paired shortcodes */
inner?: string | any[];
ordinal: number;
indentation?: string;
doMarkup: boolean;
}
export interface ParsedContentResult {
frontMatter?: FrontMatterData | undefined;
content: Content;
shortcodes: Shortcode[];
summary?: string;
wordCount: number;
readingTime: number;
}
/**
* Shortcode renderer function type for external use
*/
export type ShortcodeRendererFunc = (shortcode: Shortcode) => Promise<string> | string;
/**
* Simplified content result interface for external use
*/
export interface ContentResult {
/** Front matter data */
frontMatter?: Record<string, any>;
/** Final rendered HTML content */
renderedContent: string;
/** Content summary */
summary?: string;
/** Word count */
wordCount: number;
/** Reading time in minutes */
readingTime: number;
}
export interface RenderableDocument {
frontMatter(): Record<string, any>;
toc(): TocFragments;
render(options: ParseAndRenderOptions): Promise<ContentResult>;
}
/**
* Parse and render options
*/
export interface ParseAndRenderOptions {
/** Shortcode renderer function */
shortcodeRenderer?: ShortcodeRendererFunc;
/** Maximum summary length */
maxSummaryLength?: number;
/** Words per minute for reading time calculation */
wordsPerMinute?: number;
}
export interface Markdown extends Highlighter {
render(rctx: RenderContext, dctx: DocumentContext): Promise<Result>;
parseContent(source: Uint8Array): Promise<ParsedContentResult>;
parseAndRenderContent(source: Uint8Array, options?: ParseAndRenderOptions): Promise<ContentResult>;
prepareRender(source: Uint8Array): Promise<RenderableDocument>;
}
/**
* Result represents the minimum returned from Convert.
*/
export interface Result extends RenderingResult, ParsingResult {
}
/**
* ParsingResult interface
*/
export interface ParsingResult {
headers(): Header[];
tableOfContents(): TocFragments;
}
/**
* RenderingResult interface
*/
export interface RenderingResult {
bytes(): Uint8Array;
}
/**
* Highlighter interface for code highlighting
*/
export interface Highlighter extends CodeBlockRenderer, IsDefaultCodeBlockRendererProvider {
highlight(code: string, lang: string, opts?: any): Promise<string>;
highlightCodeBlock(ctx: CodeblockContext, opts?: any): Promise<HighlightResult>;
}
/**
* HighlightResult interface
*/
export interface HighlightResult {
wrapped(): string;
inner(): string;
}
/**
* AttributesOptionsSliceProvider interface
*/
export interface AttributesOptionsSliceProvider {
attributesSlice(): Attribute[];
optionsSlice(): Attribute[];
}
/**
* Attribute interface
*/
export interface Attribute {
name(): string;
value(): any;
valueString(): string;
}
/**
* RenderContext holds contextual information about the content to render.
*/
export interface RenderContext {
/** Context for the current Page render */
ctx: any;
/** Content to render */
src: Uint8Array;
/** Whether to render TableOfContents */
renderTOC: boolean;
/** Provides hook renderers on demand */
getRenderer: GetRendererFunc;
}
/**
* DocumentContext holds contextual information about the document to convert.
*/
export interface DocumentContext {
/** May be null. Usually a page.Page */
document: any | null;
documentID: string;
documentName: string;
filename: string;
}
/**
* Renderer types enum
*/
export declare enum RendererType {
LinkRendererType = 1,
ImageRendererType = 2,
HeadingRendererType = 3,
CodeBlockRendererType = 4
}
/**
* GetRendererFunc type
*/
export type GetRendererFunc = (t: RendererType, id?: any) => any | null;
/**
* Default renderer function
*/
export declare const DefaultRendererFunc: GetRendererFunc;
/**
* ContextData interface
*/
export interface ContextData {
renderContext(): RenderContext;
documentContext(): DocumentContext;
}
/**
* TableOfContentsProvider interface
*/
export interface TableOfContentsProvider {
tableOfContents(): TocFragments;
}
/**
* TocFragments interface
*/
export interface TocFragments {
toHTML(startLevel: number, stopLevel: number, ordered: boolean): string;
}
/**
* LinkContext is the context passed to a link render hook.
*/
export interface LinkContext {
/** The Page being rendered */
page(): any;
/** The link URL */
destination(): string;
/** The link title attribute */
title(): string;
/** The rendered (HTML) text */
text(): RenderedString;
/** The plain variant of Text */
plainText(): string;
}
/**
* LinkRenderer interface
*/
export interface LinkRenderer {
renderLink(cctx: any, w: FlexiWriter, ctx: LinkContext): Promise<void>;
}
/**
* AttributesProvider interface
*/
export interface AttributesProvider {
/** Attributes passed in from Markdown (e.g. { attrName1=attrValue1 attrName2="attr Value 2" }) */
attributes(): Record<string, any>;
}
/**
* HeadingContext contains accessors to all attributes that a HeadingRenderer
* can use to render a heading.
*/
export interface HeadingContext extends AttributesProvider {
/** Page is the page containing the heading */
page(): any;
/** Level is the level of the header (i.e. 1 for top-level, 2 for sub-level, etc.) */
level(): number;
/** Anchor is the HTML id assigned to the heading */
anchor(): string;
/** Text is the rendered (HTML) heading text, excluding the heading marker */
text(): RenderedString;
/** PlainText is the unrendered version of Text */
plainText(): string;
}
/**
* HeadingRenderer describes a uniquely identifiable rendering hook.
*/
export interface HeadingRenderer {
/** RenderHeading writes the rendered content to w using the data in ctx */
renderHeading(cctx: any, w: FlexiWriter, ctx: HeadingContext): Promise<void>;
}
/**
* IsDefaultCodeBlockRendererProvider interface
*/
export interface IsDefaultCodeBlockRendererProvider {
isDefaultCodeBlockRenderer(): boolean;
}
/**
* ElementPositionResolver provides a way to resolve the start Position
* of a markdown element in the original source document.
* This may be both slow and approximate, so should only be
* used for error logging.
*/
export interface ElementPositionResolver {
resolvePosition(ctx: any): Position;
}
/**
* Position interface for text positioning
*/
export interface Position {
line: number;
column: number;
offset: number;
}
/**
* CodeblockContext is the context passed to a code block render hook.
*/
export interface CodeblockContext extends AttributesProvider, AttributesOptionsSliceProvider, Positioner {
/** Chroma highlighting processing options. This will only be filled if Type is a known Chroma Lexer */
options(): Record<string, any>;
/** The type of code block. This will be the programming language, e.g. bash, when doing code highlighting */
type(): string;
/** The text between the code fences */
inner(): string;
/** Zero-based ordinal for all code blocks in the current document */
ordinal(): number;
/** The owning Page */
page(): any;
}
/**
* Positioner interface
*/
export interface Positioner {
position(): Position;
}
/**
* CodeBlockRenderer interface
*/
export interface CodeBlockRenderer {
renderCodeblock(cctx: any, w: FlexiWriter, ctx: CodeblockContext): Promise<void>;
}
/**
* FlexiWriter interface - flexible writer for different output types
*/
export interface FlexiWriter {
write(data: Uint8Array): Promise<void>;
writeString(str: string): Promise<void>;
writeByte(b: number): Promise<void>;
}
/**
* RenderedString type - represents rendered HTML string
*/
export type RenderedString = string;
/**
* Header interface
*/
export interface Header {
name(): string;
level(): number;
links(): Link[];
paragraphs(): Paragraph[];
listParagraphs(): Paragraph[];
}
/**
* Link interface
*/
export interface Link {
text(): string;
url(): string;
}
/**
* Paragraph interface
*/
export interface Paragraph {
text(): string;
}
/**
* Markdown renderer interface - allows for external markdown renderers
*/
export interface MarkdownRenderer {
render(source: string): Promise<string>;
parse(source: string): Promise<ParsingResult>;
}
/**
* Markdown configuration interface
*/
export interface MarkdownConfig {
renderer?: MarkdownRenderer;
extensions?: MarkdownExtension[];
options?: Record<string, any>;
}
/**
* Markdown extension interface
*/
export interface MarkdownExtension {
name: string;
extend(md: any): void;
}
/**
* Basic markdown rendering function type - for environments where we only have access to render functions
*/
export type BasicMarkdownRenderFunc = (source: string) => Promise<string> | string;
/**
* Basic markdown parsing function type - for environments where we only have access to parse functions
*/
export type BasicMarkdownParseFunc = (source: string) => Promise<any> | any;
//# sourceMappingURL=type.d.ts.map