linters/eslint-rules/no-bare-code-element
ESLint rule: no-bare-code-element
Reports a <code> element built with createEl('code', ...). In Obsidian’s app UI - a notice, a setting’s name or description, a modal, a view - a bare <code> gets only a monospace font and no inline-code chip, so it does not read as code. Obsidian draws the chip only inside rendered markdown, which is what appendCodeBlock() from @mnaoumov/obsidian-lib’s html-element.ts builds: <strong class="markdown-rendered code"><code>. A translated message with a code value in it goes through createFragmentWithCodeBlocks() with asCodeBlock(), which calls appendCodeBlock() for each value.
The element is matched by its tag alone, whatever its text: a plugin name, a file path and a setting value all need the chip. The tag is read when it is a string literal, or a template literal without substitutions, passed as the first argument of a function or method named createEl.
There is no autofix: appendCodeBlock() appends to a parent and returns nothing, so a call whose result is kept, or which passes a callback or other options, has to be rewritten by hand. A module that builds the element as its implementation - appendCodeBlock() itself, or a highlighter filling a <pre><code> - disables the rule on that line with its reason. Tests are not linted by it: a fixture may build any element.
Variables
Section titled “Variables”| Variable | Description |
|---|---|
| MESSAGE_ID | Message ID reported for a bare <code> element. |
| noBareCodeElement | ESLint rule requiring a <code> element in UI to be built with appendCodeBlock(). |