Command-line tool

edit

Meziantou.Framework.HtmlToMarkdown.Tool converts HTML to Markdown without writing code: use it in scripts, or to convert pages saved from a browser.

The command reference lists every option of the tool.

Install the tool

Install it as a global tool:

dotnet tool install --global Meziantou.Framework.HtmlToMarkdown.Tool

Convert a file

--input is the HTML file, and --output the Markdown file to write. The folder of the output file is created if needed:

Meziantou.Framework.HtmlToMarkdown.Tool --input page.html --output page.md

The input can be a complete page: the tool converts the content of its body, and ignores the head. This page:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Getting started</title>
  <style>body { font-family: sans-serif; }</style>
</head>
<body>
  <h1>Getting started</h1>
  <p>Install the package, then call <code>Convert</code>:</p>
  <pre><code class="language-bash">dotnet add package MyLibrary</code></pre>
  <ul>
    <li><a href="install.html">Installation</a></li>
    <li><a href="usage.html">Usage</a></li>
  </ul>
</body>
</html>

gives this Markdown:

# Getting started

Install the package, then call `Convert`:

```bash
dotnet add package MyLibrary
```

- [Installation](install.html)
- [Usage](usage.html)

The output file is written in UTF-8, without byte order mark, and ends with a line break. A fragment, without html or body elements, is converted the same way.

Use the standard input and output

Without --output, the Markdown is written to the standard output. Without --input, the HTML is read from the standard input, so the tool can be part of a pipeline:

echo "<h1>Title</h1>" | Meziantou.Framework.HtmlToMarkdown.Tool

With --input, the standard input is not read.

Set the formatting options

Each option of HtmlToMarkdownOptions has a command-line option, with the same values and the same defaults:

Meziantou.Framework.HtmlToMarkdown.Tool --input page.html --heading-style Setext --emphasis-marker Underscore --emoji-shortcode-mode GitHub
OptionValuesLibrary option
--emphasis-markerAsterisk (default), UnderscoreEmphasisMarker
--heading-styleAtx (default), SetextHeadingStyle
--code-block-styleFenced (default), IndentedCodeBlockStyle
--code-block-fence-character` (default), ~CodeBlockFenceCharacter
--unordered-list-marker- (default), *, +UnorderedListMarker
--thematic-breakThe text of a horizontal rule, --- by defaultThematicBreak
--line-break-styleTrailingSpaces (default), BackslashLineBreakStyle
--simple-punctuationA flag: replaces the typographic punctuation with ASCII charactersUseSimplePunctuation
--emoji-shortcode-modeNone (default), GitHub, UnicodeEmojiShortcodeMode
--unknown-element-handlingPassThrough (default), Strip, StripKeepContentUnknownElementHandling

Formatting options and Unknown elements describe what they do. Quote the characters that your shell reads, such as ` and *: --unordered-list-marker "*".

Other options

OptionDescription
--input <path>The HTML file to convert. When omitted, the standard input.
--output <path>The Markdown file to write. When omitted, the standard output.
-?, -h, --helpShows the help.
--versionShows the version of the tool.

Errors

The tool exits with code 0 when it succeeds. A missing input file gives exit code 1 and a message on the standard error. A value that is not valid for an option, such as --unordered-list-marker ab, gives a message on the standard error and a non-zero exit code.