API
本文書では,minitype が提供する主要な API を説明します.
minitype(groups, style, options)
Section titled “minitype(groups, style, options)”文書組版のエントリポイントです.
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). |
MiniTypeOptions
Section titled “MiniTypeOptions”interface MiniTypeOptions { outline?: boolean; metadata?: PdfMetadata; ppi?: number; blockTransformers?: BlockTransformer[]; disableDefaultTransformers?: boolean; fontDir?: string; fonts?: BrowserFontData[]; browserFiles?: { path: string; data: ArrayBuffer }[];}| プロパティ | 説明 |
|---|---|
outline | PDF のアウトライン(しおり)を生成するか.true の場合生成する. |
metadata | PDF メタデータ. |
ppi | PNG 出力時の解像度(既定値 350). |
blockTransformers | カスタムブロック変換プラグイン. |
disableDefaultTransformers | デフォルトの Transformer を無効にするか(既定値 false).true の場合無効にする. |
fontDir | フォントファイルのディレクトリ(Node.js,Bun のみ対応). |
fonts | ブラウザ向けフォントデータ. |
browserFiles | ブラウザ向けファイルデータ. |
戻り値のメソッド
Section titled “戻り値のメソッド”save(path)
Section titled “save(path)”PDF または PNG をファイルシステムに保存します(Node.js のみ).
複数ページの PNG は $name-$index.png の形式で保存されます.
保存されたファイルパスの配列を返します.
toPdf()
Section titled “toPdf()”組版結果の PDF を Uint8Array として返します.
toImages()
Section titled “toImages()”組版結果を PNG の Uint8Array 配列として返します.
配列の要素が各ページに対応します.
getLayout()
Section titled “getLayout()”組版結果のレイアウト情報をページ出現順で返します.
戻り値である 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;| プロパティ | 型 | 説明 |
|---|---|---|
type | string | ブロックの種類. |
index | string | number | HeadingIndex | ブロックの通し番号. |
pageIndex | number | 0-based のページ番号. |
x, y | number | ページ上の座標(mm). |
width, height | number | サイズ(mm) . |
id | string | ブロック ID. |
label | string | ブロックのラベル. |
content | string | テキスト内容またはファイルパス(画像). |
inlines | InlineOrExtender[] | ブロックの内容 |
level | number | 見出しレベル(見出しのみ). |
unnumbered | boolean | 見出しに番号を付与しないか.true の場合に付与しない. |
order | number | リスト番号(リストのみ). |
getPageCount(): Promise<number>
Section titled “getPageCount(): Promise<number>”組版結果の総ページ数を返します.
getDiagnostics(): Promise<Diagnostic[]>
Section titled “getDiagnostics(): Promise<Diagnostic[]>”組版中に検出された診断情報を返します.
戻り値である 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 | 行がサイズを超過した. |
バリデーション
Section titled “バリデーション”validateDocument(groups)
Section titled “validateDocument(groups)”グループの配列を受け取り,文書構造を検証します. 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;};formatValidationErrors(errors)
Section titled “formatValidationErrors(errors)”validateDocument が返したエラー配列を,AI エージェント向けの修正プロンプト文字列に変換します.
export const formatValidationErrors: (errors: ValidationError[]) => string;getDocumentJsonSchema()
Section titled “getDocumentJsonSchema()”文書構造の JSON Schema を返します.
export const getDocumentJsonSchema: () => Record<string, unknown>;