marked-terminal

repository·master·Indexed 19 days ago

https://github.com/mikaelbr/marked-terminal

A custom renderer for the 'marked' markdown parser that enables printing formatted Markdown directly to the terminal. It supports pretty tables, syntax highlighting via cli-highlight, and customizable terminal styling using chalk colors. Version 7.3.0.

Tokens
3.2K
Snippets
14
Records
15
Agent score
66%

What's inside marked-terminal

  1. Configure syntax highlighting with cli-highlight

    master

    The renderer supports syntax highlighting via cli-highlight. You can pass configuration options for cli-highlight as the second argument to the TerminalRenderer constructor.

    // Example of passing highlightOptions as the second argument
    new TerminalRenderer(options, highlightOptions);
  2. Automate Git commits and tags with mversion

    master

    You can use the -m flag to automatically commit and tag your version changes in Git. The %s placeholder is replaced by the newly bumped version in both the commit message and the tag.

    By default, the commit message is v%s and the tag is v%s.

    # Auto-commit and tag with default 'v%s' message
    $ mversion minor -m
    
    # Auto-commit with a custom message and a custom tag name
    $ mversion minor -m 'Bumped to v%s' --tag 'v%s-src'
  3. Use the mversion CLI to update module versions

    master

    The mversion command updates the version number in one or all of the following files: package.json, component.json, bower.json, manifest.json, and *.jquery.json.

    To get the current version: Run the command without any arguments.

    To update the version: You can specify a new semver-valid version string or a release type (major, minor, patch, prerelease, or build).

    # Get current version
    $ mversion
    
    # Update to a specific version
    $ mversion 1.0.1-beta
    
    # Increment the minor version
    $ mversion minor
  4. Configure TerminalRenderer options

    master

    The options object allows you to override default terminal styling (using chalk colors), list formatting, reflow behavior, and more.

    Key configuration areas include:

    • Colors: Override styles for elements like code, heading, strong, em, etc.
    • Lists: A list function to format bullet points and numbers.
    • Layout: width (for reflow) and reflowText.
    • Tables: tableOptions passed to cli-table3.
    • Misc: unescape (to undo entity escaping), emoji support, and tab size.
    // Example of overriding a specific style
    marked.setOptions({
      renderer: new TerminalRenderer({
        codespan: chalk.underline.magenta
      })
    });
  5. Integrate marked-terminal with marked

    master

    To use marked-terminal as a renderer for the marked library, use the markedTerminal function. This function returns an object containing a renderer property that maps all standard Markdown rendering methods to the terminal-optimized versions provided by this package. You can integrate it using marked.use() or marked.setOptions().

    import { markedTerminal } from 'marked-terminal';
    import { marked } from 'marked';
    
    // Using marked.use()
    marked.use(markedTerminal());
    
    // Or using marked.setOptions()
    marked.setOptions(markedTerminal());
    
    const output = marked.parse('# Hello Terminal');
    console.log(output);
  6. Use marked-terminal with marked (CommonJS/Older versions)

    master

    If you are using older versions of marked or a CommonJS environment, instantiate TerminalRenderer and pass it to marked.setOptions via the renderer key.

    const marked = require('marked');
    const TerminalRenderer = require('marked-terminal');
    
    marked.setOptions({
      // Define custom renderer
      renderer: new TerminalRenderer()
    });
    
    // Show the parsed data
    console.log(
      marked('# Hello \n This is **markdown** printed in the `terminal`')
    );
  7. Use marked-terminal with marked (ESM)

    master

    For modern ESM environments, use marked.use() to register the markedTerminal renderer. You can optionally pass options and highlightOptions to the renderer.

    import { marked } from 'marked';
    import { markedTerminal } from 'marked-terminal';
    
    marked.use(markedTerminal([options][, highlightOptions]));
    
    marked.parse('# Hello \n This is **markdown** printed in the `terminal`');
  8. Configure marked-terminal options

    master

    The markedTerminal function accepts two arguments: options and highlightOptions. The options object allows you to customize the ANSI colors and behavior for various Markdown elements.

    Key configuration options include:

    • code: Color for code blocks (default: chalk.yellow)
    • blockquote: Style for blockquotes (default: chalk.gray.italic)
    • heading: Color for headings (default: chalk.green.bold)
    • firstHeading: Color for H1 (default: chalk.magenta.underline.bold)
    • emoji: Boolean to enable/disable emoji replacement (default: true)
    • unescape: Boolean to enable/disable HTML entity unescaping (default: true)
    • width: The wrap width for text reflowing (default: 80)
    • reflowText: Boolean to enable text reflowing based on width (default: false)
    • tab: Number of spaces or a string for tab characters (default: 4)
    • tableOptions: Configuration object passed to cli-table3 for rendering tables.
    • showSectionPrefix: Boolean to show # prefixes in headings (default: true)
    const options = {
      code: chalk.cyan,
      heading: chalk.red.bold,
      emoji: true,
      width: 100,
      reflowText: true,
      tableOptions: {
        padding: { left: 1, right: 1 }
      }
    };
    
    // Pass options to markedTerminal
    marked.use(markedTerminal(options));
  9. Reference: TerminalRenderer options schema

    master

    The following keys are available in the options object passed to TerminalRenderer to customize the terminal output.

    var defaultOptions = {
      // Colors
      code: chalk.yellow,
      blockquote: chalk.gray.italic,
      html: chalk.gray,
      heading: chalk.green.bold,
      firstHeading: chalk.magenta.underline.bold,
      hr: chalk.reset,
      listitem: chalk.reset,
      table: chalk.reset,
      paragraph: chalk.reset,
      strong: chalk.bold,
      em: chalk.italic,
      codespan: chalk.yellow,
      del: chalk.dim.gray.strikethrough,
      link: chalk.blue,
      href: chalk.blue.underline,
    
      // Formats the bulletpoints and numbers for lists
      list: function (body, ordered) {/* ... */},
    
      // Reflow and print-out width
      width: 80, // only applicable when reflow is true
      reflowText: false,
    
      // Should it prefix headers?
      showSectionPrefix: true,
    
      // Whether or not to undo marked escaping
      // of enitities (" -> " etc)
      unescape: true,
    
      // Whether or not to show emojis
      emoji: true,
    
      // Options passed to cli-table3
      tableOptions: {},
    
      // The size of tabs in number of spaces or as tab characters
      tab: 3 // examples: 4, 2, \t, \t\t
    
      image: function (href, title, text) {} // function for overriding the default image handling.
    };
  10. Reference: mversion CLI flags and arguments

    master

    The mversion CLI supports the following arguments and flags for version bumping and Git integration.

    mversion [ <newversion> | major | minor | patch | prerelease ] [ -m <optional message> ] [ -n | --no-prefix ]
    
    Arguments:
    - `<newversion>`: A semver-valid version string.
    - `major` | `minor` | `patch` | `prerelease`: Release types to increment the version.
    
    Flags:
    - `-m <message>`: Auto-commits and tags the change. Use `%s` as a placeholder for the new version. The default message is `v%s`.
    - `--tag` or `-t <tagname>`: Overrides the tag name used for the Git tag. Does not change the commit message behavior.
    - `--no-prefix` or `-n`: Sets the tag name without a `v` prefix. Does not change the commit message behavior.