Complete reference for chalk library methods.
Core Methods
chalk.createLibrary(documentDefinition: DocumentDefinition): Library
Creates a new Chalk library with the specified document definition.
Parameters:
documentDefinition(DocumentDefinition): The document structure definition
Returns: Library instance with .add() method for chaining
Example:
const library = chalk.createLibrary({
class: 'blog-post',
bodyPolicy: ['paragraph', 'image'],
headerAttributes: [
{ identifier: 'title', type: 'string', required: true }
]
});
chalk.compile(source: string, library: Library): Result<Document, string>
Compiles Chalk source code into a structured document.
Parameters:
source(string): The Chalk source code to compilelibrary(Library): The library containing element definitions
Returns: Result object with ok, data, and error properties
Example:
const result = chalk.compile(chalkSource, library);
if (result.ok) {
console.log(result.data);
} else {
console.error(result.error);
}
chalk.compileFile(filePath: string, library: Library): Result<Document, string>
Compiles a Chalk file into a structured document.
Parameters:
filePath(string): Path to the .chalk filelibrary(Library): The library containing element definitions
Returns: Result object with ok, data, and error properties
Example:
const result = chalk.compileFile('./document.chalk', library);
Library Methods
Library.add(...definitions): Library
Adds element definitions, attribute types, and enums to a library. Supports method chaining.
Parameters:
...definitions: ElementDefinition, AttributeTypeDefinition, or EnumDefinition objects
Returns: The library instance for chaining
Example:
const library = chalk.createLibrary(docDef)
.add(element1, element2, customType, enumType);
Result Type
All methods return a Result<T, E> object:
interface Result<T, E> {
ok: boolean;
data?: T; // Present when ok: true
error?: E; // Present when ok: false
}
Usage:
const result = chalk.compile(source, library);
if (result.ok) {
// Success - use result.data
processDocument(result.data);
} else {
// Error - handle result.error
console.error('Compilation failed:', result.error);
}
Next Steps
- API Documentation - Back to API documentation index
- Key Concepts - Understanding Chalk fundamentals
- Defining Libraries - Creating custom libraries
- Primitive Reference - Built-in elements and types
- Language Guide - Learn the Chalk language
- Home - Back to documentation home