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.
- 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.
- Node.js (v14 or higher recommended)
npm i -g markdown-image-extractor
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:
- Check if the specified file is a markdown-compatible file.
- Extract all image URLs in the file.
- Download each image, saving it as
img1.jpg
,img2.jpg
, etc., in the current directory. - Update the markdown file, replacing each URL with the path to the downloaded image.
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:
-
Download the images as
img1.jpg
andimg2.png
. -
Update
example.md
to:![Image](./img1.jpg) Some text. ![Another Image](./img2.png)
Use the --help
or -h
flag for usage details:
markdown-image-extractor --help
-
start
: Runs the script locally (e.g.,node main.js
). -
lint
: Lints the codebase with ESLint.
This project is licensed under the MIT License. See LICENSE for more details.
Please report issues at GitHub Issues. Contributions are welcome!