Skip to content

feat(block): normalize markdown code-fence aliases to Notion's enum#28

Merged
4ier merged 1 commit into
mainfrom
feat/code-lang-aliases
Apr 30, 2026
Merged

feat(block): normalize markdown code-fence aliases to Notion's enum#28
4ier merged 1 commit into
mainfrom
feat/code-lang-aliases

Conversation

@4ier
Copy link
Copy Markdown
Owner

@4ier 4ier commented Apr 30, 2026

What

Stop notion block append --file from hard-failing on the common short language labels modern editors and LLMs emit (ts, sh, yml, py, dockerfile, …).

Why

Tracked in #22. Notion's code.language is a fixed enum. Before this PR, a single ts fence in an otherwise-valid markdown file produced a ~10-line enum dump and rejected the entire PATCH.

Changes

New file cmd/code_lang.go:

  • notionCodeLanguages — the full accepted enum.
  • codeLangAliases — ~50 common labels mapped to their canonical form (tstypescript, shshell, ymlyaml, cppc++, csc#, protoprotobuf, dockerfiledocker, etc.).
  • normalizeCodeLanguage(raw):
    • trims & lowercases,
    • returns enum value unchanged if already valid,
    • returns mapped alias if known,
    • falls back to plain text + one-line stderr warning for unknown values (so a single bad fence can't break a 150-block document).

Wired into both markdown parsing and the --lang flag on inline append/insert.

Test plan

  • 40+ table-driven cases across known enum values, aliases, case/trim, unknowns (verifies stderr warning via pipe redirection).
  • Regression test: a multi-fence markdown with ts/sh/yml now parses to typescript/shell/yaml.

Closes #22

Markdown / LLM output routinely uses short language labels (`ts`, `sh`,
`yml`, `py`, …) that the Notion API rejects because code.language is a
fixed enum. Previously every such fence hard-failed the whole PATCH with:

    body.children[N].code.language should be "abap", ..., "typescript",
    "yaml", or "java/c/c++/c#", instead was "ts".

Introduce `normalizeCodeLanguage`:
  - passes through any value that's already in the Notion enum,
  - maps ~50 common aliases (ts→typescript, sh→shell, yml→yaml, py→python,
    cpp→c++, cs→c#, dockerfile→docker, md→markdown, proto→protobuf, …),
  - for unknown values, falls back to 'plain text' and emits a one-line
    stderr warning so a single unrecognized fence can't break a long doc.

Applied to both markdown parsing (`notion block append --file`) and the
`--lang` flag on inline append/insert, so CLI-typed aliases like
`--lang ts` also work.

Closes #22
@4ier 4ier merged commit 315a2aa into main Apr 30, 2026
4 checks passed
@4ier 4ier deleted the feat/code-lang-aliases branch April 30, 2026 05:01
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.

block append: map common markdown code-fence aliases (ts, js, sh, ...) to Notion's enum

1 participant