Markdown, YAML and XML: when whitespace becomes syntax

Topic illustration for the article: Markdown, YAML and XML: when whitespace becomes syntax
Practical illustration for this guide

The same U+00A0 may be harmless in Markdown prose, break an expected separation in YAML and remain text inside an XML element. Position and grammar define its role; a universal replacement has neither context.

Load an NBSP sample

Markdown is position-sensitive

Indentation creates lists and code blocks, while trailing spaces can affect line breaks. CommonMark defines ASCII space and tab in syntactic positions. An NBSP copied before a list marker looks blank but need not count as indentation.

YAML forbids tabs for indentation

YAML structure uses space indentation. A tab cannot indent a node, although it can occur in some quoted content. Replacing every tab can change literal values; repair the parser-reported position only.

XML has a strict character repertoire

XML 1.0 accepts UTF-8 and UTF-16, normalizes line endings and excludes most C0 controls. Text and attributes also follow escaping rules. Deleting a character before parsing can hide corruption rather than explain it.

Parser first, editor second

Preserve a minimal sample and the exact line-and-column error. Reveal spaces, tabs, CR and LF. Change one character and run the same parser again. A visually tidy editor view is not proof of validity.

Do not clean code blocks as prose

A Markdown document may contain YAML front matter, an XML example and a fenced code block. The outer grammar must not rewrite embedded languages. Automated repair should use a syntax tree or a format-specific linter.

Measure a round trip

After repair, parse, serialize and parse again with the same meaning. Compare keys, values and ordering where relevant. Keep the source beside the formatted copy and review the diff.

One character in three contexts

Here key and value are separated by NBSP, not ASCII space. The cleaner exposes the code point; the YAML parser confirms whether the targeted replacement restores the intended grammar.

const source = "key:\u00A0value";
console.log([...source].map(c => "U+" + c.codePointAt(0).toString(16).toUpperCase()));
// Replace only after confirming parser position and expected grammar.

Repair order

  1. Identify format and version.
  2. Capture the parser error.
  3. Reveal whitespace and code points.
  4. Change the smallest region.
  5. Repeat parse → serialize → parse and inspect the diff.

Open the cleaner

Sources and specifications

Related guides

Author: Mykola. Highlighting locates a character; format-specific parsers determine Markdown, YAML and XML validity.