gram/packages/format
Repository files (latest commit first)
Filename Latest commit message Latest commit date
abiwab a19a2f054b
Some checks failed
CI / Build & Test (pull_request) Has been cancelled
chore: release v1.6.1
2026-12-02 14:38:23 +01:00
..
src fix(format): trim trailing whitespace on closing frontmatter fence at EOF 2026-12-02 14:16:00 +01:00
tests fix(format): trim trailing whitespace on closing frontmatter fence at EOF 2026-12-02 14:16:00 +01:00
CHANGELOG.md chore: release v1.6.1 2026-12-02 14:38:23 +01:00
package.json chore: release v1.6.1 2026-12-02 14:38:23 +01:00
README.md docs: rewrite root and package READMEs for v1.6 2026-11-29 22:35:50 +01:00
tsconfig.json feat(format): unify CLI and language-server .gram formatters into @gram-lang/format 2026-07-24 21:09:31 +02:00

@gram-lang/format

npm License: GPL v3

Part of the Gram monorepo · Documentation

Canonical .gram source formatter, shared by the CLI (gram format) and the Language Server (Format on Save).

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{ 200.0g }  and  @milk{200ml}.
`;

const { content, changes } = formatGram(unformatted);

if (hasChanges(changes)) {
  console.log('Summary:', summarizeChanges(changes));
  console.log('Formatted content:\n', content);
}

Structure and rules

  • src/index.ts: exports formatGram, hasChanges, and summarizeChanges.

Formatting rules enforced across .gram files (preserving frontmatter boundaries and 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 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 a single blank line
  13. Ensure a single trailing newline at end of file

License

Licensed under the GPL-3.0 license.