コンテンツにスキップ

API

本文書では,minitype が提供する主要な API を説明します.

文書組版のエントリポイントです.

export const minitype: (
groups: Group[],
style?: Partial<DocumentStyle>,
options?: MiniTypeOptions,
) => {
save(path: string): Promise<string[]>;
toPdf(): Promise<Uint8Array>;
toImages(): Promise<Uint8Array[]>;
getLayout(): Promise<BlockLabel[]>;
getPageCount(): Promise<number>;
getDiagnostics(): Promise<Diagnostic[]>;
};
プロパティ説明
groupsグループの配列(文書の内容).
styleドキュメントスタイル(省略可能).
optionsオプション設定(MiniTypeOptions).
interface MiniTypeOptions {
outline?: boolean;
metadata?: PdfMetadata;
ppi?: number;
blockTransformers?: BlockTransformer[];
disableDefaultTransformers?: boolean;
fontDir?: string;
fonts?: BrowserFontData[];
browserFiles?: { path: string; data: ArrayBuffer }[];
}
プロパティ説明
outlinePDF のアウトライン(しおり)を生成するか.true の場合生成する.
metadataPDF メタデータ.
ppiPNG 出力時の解像度(既定値 350).
blockTransformersカスタムブロック変換プラグイン.
disableDefaultTransformersデフォルトの Transformer を無効にするか(既定値 false).true の場合無効にする.
fontDirフォントファイルのディレクトリ(Node.js,Bun のみ対応).
fontsブラウザ向けフォントデータ.
browserFilesブラウザ向けファイルデータ.

PDF または PNG をファイルシステムに保存します(Node.js のみ). 複数ページの PNG は $name-$index.png の形式で保存されます. 保存されたファイルパスの配列を返します.

組版結果の PDF を Uint8Array として返します.

組版結果を PNG の Uint8Array 配列として返します. 配列の要素が各ページに対応します.

組版結果のレイアウト情報をページ出現順で返します. 戻り値である BlockLabel には以下のプロパティが含まれます.

type BlockLabel =
| ({
type: BlockLabelType;
index: string;
} & BaseLabel)
| HeadingBlockLabel
| ListBlockLabel;
type BlockLabelType =
| "paragraph"
| "code"
| "image"
| "shape"
| "caption"
| "math"
| "table"
| "footnote"
| "newpage"
| "clearpage"
| "newcolumn"
| "box"
| "flexbox";
type BaseLabel = {
pageIndex: number | null;
x: number | null;
y: number | null;
width: number | null;
height: number | null;
id: string;
label?: string;
content: string | null;
inlines?: InlineOrExtender[];
};
type HeadingBlockLabel = {
type: "heading";
index: HeadingIndex;
level: HeadingLevel;
unnumbered?: boolean;
} & BaseLabel;
type ListBlockLabel = {
type: "list";
index: number;
order: number[];
} & BaseLabel;
プロパティ型説明
typestringブロックの種類.
indexstring | number | HeadingIndexブロックの通し番号.
pageIndexnumber0-based のページ番号.
x, ynumberページ上の座標(mm).
width, heightnumberサイズ(mm) .
idstringブロック ID.
labelstringブロックのラベル.
contentstringテキスト内容またはファイルパス(画像).
inlinesInlineOrExtender[]ブロックの内容
levelnumber見出しレベル(見出しのみ).
unnumberedboolean見出しに番号を付与しないか.true の場合に付与しない.
ordernumberリスト番号(リストのみ).

組版結果の総ページ数を返します.

組版中に検出された診断情報を返します. 戻り値である Diagnostic は以下の型です.

type Diagnostic =
| MissingLabelDiagnostic
| MissingCitationDiagnostic
| OverfullImageDiagnostic
| OverfullLineDiagnostic;
type BaseDiagnostic<
Type extends string,
Severity extends DiagnosticSeverity,
> = {
type: Type;
severity: Severity;
message: string;
blockId?: string;
blockLabel?: string;
blockType?: string;
position?: DiagnosticPosition;
hint?: string;
};
type DiagnosticSeverity = "error" | "warning" | "info";
type DiagnosticPosition = {
pageIndex?: number | null;
x?: number | null;
y?: number | null;
width?: number | null;
height?: number | null;
};
type MissingCitationDiagnostic = BaseDiagnostic<
"missing-citation",
"warning"
> & {
missingKey: string;
availableKeys: string[];
};
type MissingLabelDiagnostic = BaseDiagnostic<"missing-label", "warning"> & {
missingLabelOrId: string;
};
type OverfullImageDiagnostic = BaseDiagnostic<"overfull-image", "warning"> & {
expectedInlineSize: number;
actualInlineSize: number;
};
type OverfullLineDiagnostic = BaseDiagnostic<"overfull-line", "warning"> & {
expectedInlineSize: number;
actualInlineSize: number;
lineIndex: number;
};
型説明
MissingLabelDiagnostic未定義のラベルが参照された.
MissingCitationDiagnostic未定義の文献キーが引用された.
OverfullImageDiagnostic図版がサイズを超過した.
OverfullLineDiagnostic行がサイズを超過した.

グループの配列を受け取り,文書構造を検証します. AI エージェントが生成した JSON を minitype に渡す前の検証等に利用できます.

export const validateDocument: (groups: unknown) => ValidationResult;
type ValidationResult =
| { ok: true; data: Group[] }
| { ok: false; errors: ValidationError[] };
type ValidationError = {
path: string;
message: string;
};

validateDocument が返したエラー配列を,AI エージェント向けの修正プロンプト文字列に変換します.

export const formatValidationErrors: (errors: ValidationError[]) => string;

文書構造の JSON Schema を返します.

export const getDocumentJsonSchema: () => Record<string, unknown>;