Carbon Now CLI Transform Your Code Snippets with Ease for SEO Optimization

Introduction to carbon-now-cli

carbon-now-cli is an open-source tool that enables developers to create and share beautiful images of their source code directly from the terminal, enhancing code readability and shareability. The CLI interacts with the Carbon web application to generate these images, providing various customization options to tailor the appearance to your preference.

Getting Started

To install carbon-now-cli, use the following command:

npm install -g carbon-now-cli

Useful API Examples

Generate a Basic Code Image

Create a simple image of your code file:

carbon-now myfile.js

Specify Output Directory

Define a custom output directory for the generated image:

carbon-now myfile.js --output /path/to/dir

Set Window Theme

Choose a window theme for your code image:

carbon-now myfile.js --theme seti

Add a Custom Background Color

Specify a custom background color for the image:

carbon-now myfile.js --bg=rgba(255,255,255,0.85)

Set Code Language

Define the programming language for syntax highlighting:

carbon-now myfile.js --lang javascript

Adjust Image Size

Modify the image size to your preference:

carbon-now myfile.js --width 1024 --height 768

Create Multiple Code Images

Generate images from multiple code files in one command:

carbon-now file1.js file2.js

Creating a Sample Application with carbon-now-cli

Let’s create a simple Node.js application to demonstrate using carbon-now-cli within a project:


  // Install the necessary package
  npm install -g carbon-now-cli
  
  // Sample app.js file
  const fs = require('fs');
  const exec = require('child_process').exec;

  // Function to generate a code image
  function generateCodeImage(filePath) {
    exec(`carbon-now ${filePath} --output ./images`, (error, stdout, stderr) => {
      if (error) {
        console.error(`Error generating image: ${error.message}`);
        return;
      }
      console.log(`Image generated successfully: ${stdout}`);
    });
  }

  // Create a sample code file
  const codeContent = `console.log('Hello, world!');`;
  const filePath = './helloWorld.js';
  fs.writeFileSync(filePath, codeContent);

  // Generate the code image
  generateCodeImage(filePath);

This small application will create a file called helloWorld.js and generate an image of the content using carbon-now-cli.

Hash: 455813ca0cb66d1192027710d62ef559e64b13814254cd99a3c4ec1cf08000fe

Leave a Reply

Your email address will not be published. Required fields are marked *