Skip to content

Fix GitHub highlighting of .cook files with YAML front matter - #30

Merged
dubadub merged 1 commit into
mainfrom
fix/github-frontmatter-highlighting
Dec 3, 2026
Merged

dubadub merged 1 commit into
mainfrom
fix/github-frontmatter-highlighting

Conversation

@dubadub

@dubadub dubadub commented Dec 2, 2026

Copy link
Copy Markdown
Member

Problem

GitHub highlights .cook files with this repo's grammar, which Linguist vendors as source.cooklang. On github.com, any recipe whose front matter has a mapping or a list item renders the whole body as YAML:

  • text before a colon (Mix the dry ingredients: @flour{200%g}) becomes a YAML key;
  • #pot lines become YAML comments;
  • no ingredients, cookware, timers or == sections == are highlighted.

Since almost every real recipe has title:/tags: front matter, this hits most .cook files on GitHub. VS Code is not affected.

Cause

The front matter rule was begin / while. On GitHub's highlighter, checked live through the /markdown API with a ```cooklang fence:

Front matter content Body after closing ---
empty, plain scalar (foo), or comment highlighted as Cooklang ✅
mapping (a: b, a:) or list item (- x) stays YAML ❌
closed with ... instead of --- stays YAML ❌

The included YAML grammar opens a multi-line rule for mappings and lists. GitHub doesn't end the outer while rule while that nested rule is still open, so the block never closes. VS Code checks every while on the rule stack at the start of each line, which is why it works there.

Fix

Use plain begin / end: begin: \A-{3}\s*$, end: ^(-{3}|\.{3})\s*$. The end match consumes the closing line. This is the same shape GitHub's own Markdown grammar (wooorm/markdown-tm-language) uses around the same source.yaml. Through the live API, that shape closes correctly after mappings, nested lists, | and > block scalars as the last key, and an open [a, flow list.

Effect in VS Code

I ran vscode-textmate with VS Code's built-in YAML grammar over 11 sample files covering empty front matter, ... closing, nested lists, block scalars, open flow collections, unclosed quotes and no front matter. The only token change is that the closing ---/... line now gets punctuation.definition.frontmatter.end.cooklang. Front matter and body tokens are identical.

Known limitation

With GitHub's YAML grammar (atom/language-yaml), an unclosed quote in front matter (title: "Pie) still runs past the closing ---. GitHub's Markdown front matter has the same limitation, and that front matter isn't valid YAML anyway.

When it shows on github.com

Linguist pins this repo's main and picks up grammar changes at its next release (see Linguist's CONTRIBUTING, "Fixing syntax highlighting"). No Linguist PR is needed.

https://claude.ai/code/session_01B7jPFQJPPhZyUuGSiAsq2f

…ghts the body

On github.com, any .cook file whose front matter contains a mapping or a
list item (`title: X`, `- tag`) rendered the whole recipe body as YAML:
text before a colon became a YAML key, `#cookware` became a YAML comment,
and no ingredients, cookware, timers or sections were highlighted.

The front matter rule used begin/while. GitHub's highlighter does not end
a `while` rule while a nested rule from the included YAML grammar is still
open, so the block never closed. Plain begin/end, the shape GitHub's own
Markdown grammar uses for front matter, closes correctly there.

In VS Code the only token change is that the closing `---` / `...` line
now gets `punctuation.definition.frontmatter.end.cooklang`.

Claude-Session: https://claude.ai/code/session_01B7jPFQJPPhZyUuGSiAsq2f
@dubadub
dubadub merged commit e914593 into main Dec 3, 2026
1 check passed
@dubadub
dubadub deleted the fix/github-frontmatter-highlighting branch December 3, 2026 07:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant