|
| 1 | +--- |
| 2 | +title: "QMD Writer" |
| 3 | +--- |
| 4 | + |
| 5 | +The QMD writer converts the parsed AST representation back into Quarto Markdown (`.qmd`) format. This writer is designed to support roundtripping: parsing a document and writing it back should preserve the original structure and meaning. |
| 6 | + |
| 7 | +## Purpose |
| 8 | + |
| 9 | +The QMD writer is used for: |
| 10 | + |
| 11 | +- **Roundtripping**: Converting documents through the parse → AST → write pipeline while preserving content |
| 12 | +- **Format normalization**: Standardizing markdown formatting across documents |
| 13 | +- **Document transformation**: Making programmatic changes to documents via the AST |
| 14 | + |
| 15 | +## Differences from Pandoc |
| 16 | + |
| 17 | +While `quarto-markdown` aims to be largely compatible with Pandoc, the QMD writer intentionally differs from Pandoc's markdown writer in certain cases to support better roundtripping and preserve explicit markup. |
| 18 | + |
| 19 | +### Span Elements with Empty Attributes |
| 20 | + |
| 21 | +**Pandoc behavior**: When a span has empty attributes (`["", [], []]`), Pandoc's markdown writer omits the brackets and braces, outputting only the content. |
| 22 | + |
| 23 | +```markdown |
| 24 | +Input: [hello]{} |
| 25 | +Pandoc: hello |
| 26 | +``` |
| 27 | + |
| 28 | +**quarto-markdown behavior**: The QMD writer preserves the explicit span syntax, even when attributes are empty. |
| 29 | + |
| 30 | +```markdown |
| 31 | +Input: [hello]{} |
| 32 | +quarto-markdown: [hello]{} |
| 33 | +``` |
| 34 | + |
| 35 | +**Rationale**: Preserving the explicit span markup supports proper roundtripping. If a document author wrote `[content]{}`, they did so intentionally, and this information should be preserved through the parse-write cycle. This also maintains the distinction between: |
| 36 | + |
| 37 | +- Text that was marked up as a span: `[hello]{}` |
| 38 | +- Plain text that happens to have no special formatting: `hello` |
| 39 | + |
| 40 | +## Usage |
| 41 | + |
| 42 | +The QMD writer can be invoked using the `quarto-markdown-pandoc` binary: |
| 43 | + |
| 44 | +```bash |
| 45 | +# Read from stdin, write QMD to stdout |
| 46 | +echo "**bold text**" | quarto-markdown-pandoc -t qmd |
| 47 | + |
| 48 | +# Read from file, write QMD to stdout |
| 49 | +quarto-markdown-pandoc -i input.qmd -t qmd |
| 50 | + |
| 51 | +# Roundtrip through JSON |
| 52 | +quarto-markdown-pandoc -i input.qmd -t json | \ |
| 53 | + quarto-markdown-pandoc -f json -t qmd |
| 54 | +``` |
| 55 | + |
| 56 | +## Formatting Conventions |
| 57 | + |
| 58 | +The QMD writer follows these formatting conventions: |
| 59 | + |
| 60 | +- **Emphasis**: Uses `*` for emphasis and `**` for strong emphasis |
| 61 | +- **Line breaks**: Soft breaks become newlines (differs from Pandoc which uses spaces) |
| 62 | +- **Smart quotes**: Unicode right single quotation marks (') are converted back to ASCII apostrophes (') |
| 63 | +- **Escaping**: Special markdown characters (`\`, `>`, `#`) are escaped as needed |
| 64 | +- **Attributes**: Written in the format `{#id .class key="value"}` |
0 commit comments