Convert between HTML, Markdown, and plain text from the command line.
An online companion tool is available at danburzo.ro/trimd.
Install trimd
globally using npm
:
npm install -g trimd
Or run trimd
on the fly using npx
:
npx trimd markdown my-file.html
trimd [command] [options] [file1, [file2, …]]
Trimd accepts one or more input files, or uses the standard input (stdin
) when no files are provided. You can also concatenate stdin
in addition to other input files using the -
operator.
A couple of general options are available:
-h, --help
- output help information.-v, --version
- output program version.--silent
- suppress the output of commands.
By default, the results of processing the operands are concatenated to stdout
. Use the --write
option to write the results back to their respective original files.
Below is a list of supported commands.
Convert HTML to Markdown.
trimd markdown my-file.html
You can pass your own preferences for generating Markdown with options in the form --md.<option>=<value>
, which will get forwarded to remark-stringify
:
trimd markdown --md.strong='*' my-file.html
These are the default Markdown options:
const MD_DEFAULTS = {
fences: true,
emphasis: '_',
strong: '_',
resourceLink: true,
rule: '-'
};
Convert Markdown to HTML.
trimd markup my-file.md
This command ignores any YAML/TOML front-matter data present in the source file.
Use the --data-url
flag to output the HTML as a base64-encoded data:
URL. This format can be useful for viewing the HTML content in a browser:
trimd markup --data-url my-file.md | xargs open -a Firefox
Note that at the time of writing, Firefox does not immediately render
data:
URLs passed from the command line (you need to press Return in the URL bar). See #1892289, which is in the process of being fixed.
Use the --no-sanitize
flag to skip the HTML sanitization step. Sanitization should only be disabled when the Markdown input is known to be safe.
Simplify HTML by converting it to Markdown and back. The command is more or less the equivalent of trimd markdown | trimd markup
.
Use the --data-url
flag to output the HTML as a base64-encoded data:
URL.
Use the --no-sanitize
flag to skip the HTML sanitization step. Sanitization should only be disabled when the HTML input is known to be safe.
Convert Markdown to Markdown. The command accepts the same options as trimd markdown
.
trimd remarkdown my-file.md
The trimd remarkdown
command is useful for converting Markdown that may contain raw HTML into the Markdown style specified with the --md.<option>=<value>
options.
This command preserves any YAML/TOML front-matter data present in the source file.
You can transform the Markdown output by providing a transform function specified in an external JavaScript file and passed via the -t <transform>
or --transform=<transform>
option.
trimd remarkdown --transform=exclamation.js my-input.md
// File: exclamation.js
export default function exclamation(visit) {
return function transform(tree, file) {
visit(tree, 'text', function (node, index, parent) {
node.value += '!';
});
};
}
The default export of your transformer will receive a reference to the visit
function from the unist-util-visit
package. This is provided as a convenience, you can use it or write your own processing logic.
This export must return a function that transforms the MDAST tree passed as the first argument. See unified.use(Plugin)
for more details.
Convert Markdown to plain text.
trimd demarkdown my-file.md
This command ignores any YAML/TOML front-matter data present in the source file.
Convert HTML to plain text.
trimd demarkup my-file.html
Plain text is produced according to the algorithm for HTMLElement.innerText
.
You can also convert HTML to plain text via Markdown by piping markdown
and demarkdown
commands:
trimd markdown my-file.html | trimd demarkdown
Trimd is a command-line interface on top of a chain of unified.js libraries.
- Clipboard Inspector, a tool to help you explore the kinds of data available when you paste something on a web page, or drop something onto it.
- percollate, a command-line tool to turn web pages into beautiful, readable PDF, EPUB, or HTML docs.
- hred, extract data from HTML (and XML) from the command line, using a syntax inspired by CSS selector.
- yamatter, inspect and transform YAML frontmatter data from the command line.