Skip to content

Markdown integration #2444

@motiz88

Description

@motiz88

Hi everyone!

I've decided to dive into the Prettier source code with the intention of ultimately building full Markdown support into it. I can find my own way around the codebase OK for now, but I'd like to discuss here (given the truth of this tweet) some fundamental issues as I encounter them, to increase the chances of this actually landing in Prettier at some point.

Goal

A Markdown integration for Prettier, which, the way I envision it, involves formatting Markdown files and ideally also JS/CSS/etc code blocks within them.

Q: Is such a Markdown integration even desired?

Basic approach

Using remark as a parser and as a reference printer:

  1. Take a direct dependency on remark-parse to implement parser-remark.js (this part is straightforward).
  2. Fully port remark-stringify to Prettier's formatting primitives (and coding conventions) to implement printer-remark.js.
  3. Make stylistic tweaks to printer-remark.js once it has reached parity with remark-stringify.
  4. Multiparser??? - Haven't explored in any depth yet

Problems/questions

Passing user options to the parser/printer

remark supports several flavours of Markdown and takes various options; would we expose any of this to the user? (How?) The alternative is to essentially commit to a single flavour of Markdown, but that may be too opinionated to be viable.

A new document type for blockquotes

If we want to print Markdown blockquotes the "standard" pretty way - with hard wrapping and a > marker at the beginning of each line:

> This is a blockquote. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> This is a blockquote. Lorem ipsum dolor sit amet, consectetuer adipiscing
> elit. Aliquam hendrerit mi posuere lectus.

Then AFAICT we need a new document type to represent it and probably a different implementation of indent and align in doc-printer.js (supporting arbitrarily mixed indentation and non-space markers, including nested blockquotes etc). Can someone confirm this or point out something I've missed?

Multiparser architecture

I have not begun to tackle this at all, but would appreciate any thoughts about it nonetheless. My current thinking is that we can extract parts of the existing JS-centric "multiparser" for reuse in a Markdown multiparser, and potentially uncover interesting generalisations. Just realised the existing multiparser also does CSS in HTML; it's probably already general enough.


Thanks for a fantastic tool and thanks in advance for any help on this!

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.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions