@gram-lang/format
v1.2.1
Published
Canonical .gram source formatter, shared by the CLI and language server
Downloads
384
Readme
@gram-lang/format
Part of the Gram monorepo.
Canonical .gram source code formatter, shared by the CLI (gram format) and Language Server (format on save).
📚 General Documentation
For full syntax specifications, command reference, and best practices, please refer to the Gram Documentation.
🛠️ Installation
npm install @gram-lang/format
# or
bun add @gram-lang/format⚡ Usage
import { formatGram, hasChanges, summarizeChanges } from '@gram-lang/format';
const unformatted = `
---
title: 'Crepes'
---
## Batter
Mix @flour{200g} and @milk{200ml}.
`;
const { content, changes } = formatGram(unformatted);
if (hasChanges(changes)) {
console.log("Summary:", summarizeChanges(changes));
console.log("Formatted content:\n", content);
}🏗️ Structure & Rules
src/index.ts: ExportsformatGram,hasChanges, andsummarizeChanges.
The package enforces canonical formatting rules across .gram files (preserving frontmatter boundary integrity):
- Lowercase ingredient IDs (
@Farine→@farine) - Remove space between
@idand{(@ing {10g}→@ing{10g}) - Trim spaces inside braces (
{ 10g }→{10g}) - Remove trailing decimal zeros (
{500.0g}→{500g}) - Normalize temperature unit spacing (
{180 °C}→{180°C}) - Normalize composite ingredient separator spacing (
<@) - Normalize arrow declaration spacing (
->&name{}) - Normalize section header spacing (
## Header) - Convert tabs to 4 spaces
- Trim trailing whitespace line by line
- Collapse consecutive blank lines to single blank lines
- Ensure single newline at end of file
📄 License
This project is licensed under the GPL-3.0 License.
