Skip to content

Markdown: Preserve code blocks within list items #3459

@infotexture

Description

@infotexture

Prettier 1.9.1
Playground link

--parser markdown

Input:

Code blocks may be nested within list items per the original Markdown syntax for lists:

To put a code block within a list item, the code block needs to be indented twice
— 8 spaces or two tabs:

1. Change to your home directory:

········cd

2. List the contents:

········ls -l

Output:

Prettier strips the 8 leading spaces and replaces them with 3 to align with the list item content:

1. Change to your home directory:

···cd

2. List the contents:

···ls -l

This treats the nested code blocks as subsequent paragraphs in the list items rather than as code blocks:

  1. Change to your home directory:

    cd

  2. List the contents:

    ls -l

Expected behavior:

The original input should either be preserved, indented with tabs or fenced to ensure the nested code blocks are rendered properly:

  1. Change to your home directory:

    cd
    
  2. List the contents:

    ls -l
    

Subsequent runs change result

If the nested code blocks are indented with 2 tabs (rather than 8 spaces), they are initially converted to 7 spaces. If the same file is saved again, the 7 spaces are converted to 3 spaces.

Fenced code blocks as workaround

If the nested code blocks are fenced, the input is preserved and output is generated as expected:

1. Change to your home directory:

   ```sh
   cd
   ```

2. List the contents:

   ```sh
   ls -l
   ```

Metadata

Metadata

Assignees

No one assigned

    Labels

    lang:markdownIssues affecting Markdownlocked-due-to-inactivityPlease open a new issue and fill out the template instead of commenting.priority:highCode is printed in a way that alters the AST, breaks syntax, or is a significant regression. Urgent!scope:dependencyIssues that cannot be solved inside Prettier itself, and must be fixed in a dependency

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions