Behaviour SwRS-MDOKFSTUDIO-CODE-001
Fenced code in a supported language is highlighted, other code stays literal
When a document contains a fenced code block, the extension SHALL highlight a block in a supported language with the grammar selected from the fence label, and leave a block of an unknown or absent language, inline code and the text of every code block as literal text, each special character shown once.
Step 1 of path 1 in StRS-MDOKFSTUDIO-CODE-001: from “A document holds a fenced code block” to “The code is coloured”.
Scenarios 9
The situations the tests check; open one to read it.
nominalA supported language is highlighted by its grammar
Givena document with a fenced `rust` block and a `python` block
Whenthe preview renders the document
Thenthe keywords, strings, numbers and comments of each block are split into token spans by the Shiki TextMate grammar of its language, and the block gets the `shiki` class; the colours of the tokens are those of SwRS-MDOKFSTUDIO-CODE-040
boundaryThe fence label selects the grammar
| Fence label | Outcome |
|---|---|
| `py` (also the aliases `js`, `sh`, `rs` and `patch`) | The block is coloured with the grammar the alias names (`py`: the Python grammar) |
| `Python {linenos}` | The leading word, lower-cased, selects the Python grammar; the text after a space, a comma or `{` is ignored |
| `, python` | The leading word is empty: no grammar is selected and the block stays plain |
| `wobble` (an unlisted label) | The block stays plain text |
| `yaml`, `bash`, `json`, `sql`, `toml`, `c`, `cpp`, `go`, `java`, `html`, `css`, `diff`, `markdown`, `dockerfile` and the other bundled languages | The block is coloured by its grammar |
| `python` block with no content | An empty highlighted block is shown without error |
refusalUnknown or absent language leaves the block as plain text
Givena block fenced as `wobblescript`, and a block with no language label
Whenthe preview renders them
Theneach block is plain monospace text with no token spans, its text is unchanged and no `shiki` class is added; a block of two or more lines is still wrapped into numbered lines (SwRS-MDOKFSTUDIO-CODE-039)
nominalInline code is never highlighted
Givena paragraph with inline `code` and a one-line fenced block
Whenthe preview renders it
Thenthe inline code has no token spans and only the fenced block is highlighted
nominalFenced code text is shown literally
Givena `markdown` fence containing a table, a link and an inner fence under a longer outer fence
Whenthe preview renders the document
Thenthe table, link and inner fence appear as literal text and no table or anchor element is created inside the block
boundaryA shorter backtick line does not close the fence
Givena fence whose content has a line of backticks shorter than the outer fence
Whenthe preview renders the document
Thenthat line stays part of the code and does not close the block
refusalA failing grammar falls back to escaped text lines
Givena numbered, emphasized or diff block whose grammar throws while highlighting
Whenthe preview renders it
Thena block that is wrapped into lines shows the escaped source text in wrapped lines, and an unwrapped block is returned unchanged
nominalSpecial characters are shown once
Givena highlighted block containing `a < b && c` and `<`
Whenthe preview renders it
Thenthe page shows `a < b && c` and `<` exactly once each, with no double escaping
boundaryAn unknown named reference is kept as written
Givena highlighted block containing an unknown named reference such as `&bogus;`
Whenthe preview renders it
Thenthe text is shown as written, not altered
Implemented in 3
Tested by 6
Replaces
SwRS-MDOKFSTUDIO-CODE-002Resolve common fence labels to a supported grammar superseded superseded by SwRS-MDOKFSTUDIO-CODE-001 Markdown file
SwRS-MDOKFSTUDIO-CODE-003Read the fence label case-insensitively up to its first separator superseded superseded by SwRS-MDOKFSTUDIO-CODE-001 Markdown file
SwRS-MDOKFSTUDIO-CODE-004Leave unknown-language code as plain text superseded superseded by SwRS-MDOKFSTUDIO-CODE-001 Markdown file
SwRS-MDOKFSTUDIO-CODE-005Numbered, emphasized and diff blocks are wrapped into lines superseded superseded by SwRS-MDOKFSTUDIO-CODE-001, SwRS-MDOKFSTUDIO-CODE-039 Markdown file
SwRS-MDOKFSTUDIO-CODE-030Never highlight inline code superseded superseded by SwRS-MDOKFSTUDIO-CODE-001 Markdown file
SwRS-MDOKFSTUDIO-CODE-031Show fenced code text literally superseded superseded by SwRS-MDOKFSTUDIO-CODE-001 Markdown file
SwRS-MDOKFSTUDIO-CODE-037Fall back to escaped text lines when a grammar fails superseded superseded by SwRS-MDOKFSTUDIO-CODE-001 Markdown file