gram/packages/format/README.md
abiwab a8f4353a08
All checks were successful
CI / Build & Test (pull_request) Successful in 4m21s
docs: refresh root and package READMEs for v1.5
2026-11-19 12:01:21 +01:00

2.3 KiB

@gram-lang/format

npm License: GPL v3

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: Exports formatGram, hasChanges, and summarizeChanges.

The package enforces canonical formatting rules across .gram files (preserving frontmatter boundary integrity and never altering target state text inside ={...}):

  1. Lowercase ingredient IDs (@Farine → @farine)
  2. Remove space between @id and { (@ing {10g} → @ing{10g})
  3. Trim spaces inside braces ({ 10g } → {10g})
  4. Remove trailing decimal zeros after a point or comma ({500.0g} → {500g}, {1,50} → {1,5}, leaving {1,000} intact)
  5. Normalize spacing around +/- in compound ingredient quantities ({1 cup+2 tbsp} → {1 cup + 2 tbsp})
  6. Normalize temperature unit spacing ({180 °C} → {180°C})
  7. Normalize composite ingredient separator spacing (<@)
  8. Normalize arrow declaration spacing (->&name{})
  9. Normalize section header spacing (## Header)
  10. Convert tabs to 4 spaces
  11. Trim trailing whitespace line by line
  12. Collapse consecutive blank lines to single blank lines
  13. Ensure single newline at end of file

📄 License

This project is licensed under the GPL-3.0 License.