markdown-image-extractor

1.0.1 • Public • Published

Markdown Image Extractor

A simple Node.js tool to extract and download images from markdown files. It’s designed for scenarios where markdown files, such as those from websites like Mathpix, contain images hosted online. This tool downloads those images and updates the markdown to reference local versions instead.

Features

  • Extracts image URLs from markdown syntax ![](<image-url>).
  • Detects markdown files dynamically, supporting .md, .mdx, and other variants.
  • Downloads each image and saves it with a specified prefix.
  • Overwrites previously downloaded images without requiring manual cleanup.
  • Replaces URLs in the markdown file with local paths, making it easy to host the images locally.
  • Full error handling to avoid interruptions, even with invalid URLs or file permissions.

Requirements

  • Node.js (v14 or higher recommended)

Installation

npm i -g markdown-image-extractor

Usage

Navigate to the directory with your markdown file(s) and run:

markdown-image-extractor <filename> [prefix]

Replace <filename> with the markdown file’s name and [prefix] with a string for naming the downloaded images. If no prefix is provided, it defaults to "img".

For example:

markdown-image-extractor filename.md "img"

This command will:

  1. Check if the specified file is a markdown-compatible file.
  2. Extract all image URLs in the file.
  3. Download each image, saving it as img1.jpg, img2.jpg, etc., in the current directory.
  4. Update the markdown file, replacing each URL with the path to the downloaded image.

Example

Given a markdown file (example.md) with content like:

![Image](https://cdn.domain.tld/cropped/image1.jpg)
Some text.
![Another Image](https://cdn.domain.tld/cropped/image2.png)

Running markdown-image-extractor example.md "img" will:

  1. Download the images as img1.jpg and img2.png.

  2. Update example.md to:

    ![Image](./img1.jpg)
    Some text.
    ![Another Image](./img2.png)

Help

Use the --help or -h flag for usage details:

markdown-image-extractor --help

Package Scripts

  • start: Runs the script locally (e.g., node main.js).
  • lint: Lints the codebase with ESLint.

License

This project is licensed under the MIT License. See LICENSE for more details.

Issues and Contributions

Please report issues at GitHub Issues. Contributions are welcome!

Package Sidebar

Install

npm i markdown-image-extractor

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

8.95 kB

Total Files

4

Last publish

Collaborators

  • suyeonorg