# MD038 - Remove extra spaces in code Aliases: `no-space-in-code` ## What this rule does Removes unnecessary spaces between backticks and code content in inline code spans. ## Why this matters - **Clean formatting**: Extra spaces make code snippets look unpolished - **Accurate code**: Leading/trailing spaces might be mistaken as part of the code - **Consistent appearance**: Code should be formatted the same way throughout - **Better readability**: Clear boundaries between code and regular text ## Examples ### ✅ Correct Code directly touches the backticks: ```markdown Use the `print()` function to display output. Call `getUserName()` to get the current user. The variable `isEnabled` stores the state. ``` ### ❌ Incorrect Extra spaces inside backticks: ```markdown Use the ` print()` function to display output. Call `getUserName() ` to get the current user. The variable ` isEnabled ` stores the state. ``` ### 🔧 Fixed Spaces removed from edges: ```markdown Use the `print()` function to display output. Call `getUserName()` to get the current user. The variable `isEnabled` stores the state. ``` ## Configuration This rule has no configuration options. ## Special cases ### Template shortcode tags Backticks inside a template shortcode tag are part of the arguments the template receives, not a code span whose padding may be trimmed, so `` {{% note `code ` %}} `` and `` {{< note `code ` >}} `` are left alone. This applies in every flavor. Only the tag is exempt: a code span in the body between a paired opening and closing shortcode is checked like any other. ### What counts as a space A code span strips one ASCII space (or line ending) from each end. U+00A0, U+2003 and the other Unicode spaces, and tabs, are code content, so a span that starts or ends with one is not reported. ### Code spans that cross a line break A code span can run past the end of a line, and then some of the whitespace inside it holds the document together rather than padding the code. Two runs are out of this rule's reach: - the run containing the line ending, because removing it joins two lines into one; - the indentation of the line the span closes on, because removing it moves that line out of its blockquote or indented block. When either end of a span is one of those, the whole span is left alone rather than trimmed at its other end. CommonMark removes a space from each end only when both ends have one, so trimming a single end would move the rendered space to the other end instead of removing it. ```markdown Text `a ` tail ``` This comes up most often where an indented fenced block follows a paragraph line, which CommonMark reads as one long code span rather than as a code block. A multi-line span whose spacing really is spacing is still reported and fixed. ## Flavor-specific behavior ### Obsidian flavor When using `--flavor obsidian`, this rule recognizes **Dataview plugin** inline queries and does not flag them as spacing errors: - **Inline DQL**: `` `= expression` `` - Queries that start with `= ` - **Inline DataviewJS**: `` `$= expression` `` - JavaScript expressions that start with `$= ` Examples that are valid in Obsidian flavor: ```markdown The file name is `= this.file.name` and it was modified on `= this.file.mtime`. Page count: `$= dv.pages('#project').length` projects found. Status: `= choice(this.done, "✅", "⏳")` ``` Note: These expressions don't have leading whitespace issues in practice since the `=` or `$=` comes directly after the opening backtick. The Dataview check serves as additional protection for edge cases. ### MkDocs flavor When using `--flavor mkdocs`, this rule allows **InlineHilite** syntax for syntax-highlighted inline code: ```markdown Use `#!python print("hello")` for inline Python highlighting. ``` ### Quarto flavor When using `--flavor quarto`, this rule allows inline R code expressions: ```markdown The result is `r nchar("test")` which equals 4. ``` ## Automatic fixes This rule will: - Remove spaces after opening backticks - Remove spaces before closing backticks - Preserve spaces within the code itself - Handle multiple code spans on the same line ## Learn more - [CommonMark specification for code spans](https://spec.commonmark.org/0.31.2/#code-spans) ## Related rules - [MD037 - Review spaces in apparent emphasis](md037.md) - Similar rule for bold and italic text