| Filename | Latest commit message | Latest commit date |
|---|---|---|
|
|
||
| .. | ||
| src | ||
| tests | ||
| CHANGELOG.md | ||
| package.json | ||
| README.md | ||
| tsconfig.json | ||
@gram-lang/format
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: exportsformatGram,hasChanges, andsummarizeChanges.
Formatting rules enforced across .gram files (preserving frontmatter boundaries and target state text inside ={...}):
- Lowercase ingredient IDs (
@Farine→@farine) - Remove space between
@idand{(@ing {10g}→@ing{10g}) - Trim spaces inside braces (
{ 10g }→{10g}) - Remove trailing decimal zeros after a point or comma (
{500.0g}→{500g},{1,50}→{1,5}, leaving{1,000}intact) - Normalize spacing around
+/-in compound quantities ({1 cup+2 tbsp}→{1 cup + 2 tbsp}) - 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 a single blank line
- Ensure a single trailing newline at end of file
License
Licensed under the GPL-3.0 license.