extract-img-url
v0.1.3
Published
Extract first image url from markdown
Readme
extract-img-url
Extract the first image URL from a Markdown string.
This package uses regular expressions instead of a full Markdown parser, so it stays small and fast for common input.
Requirements
- Node.js 18 or later
Installation
npm install extract-img-urlUsage
import { extractImgUrl } from 'extract-img-url';
const markdown = `
# Hello World
Intro text.

`;
const imageUrl = extractImgUrl(markdown);
console.log(imageUrl);
// => "https://example.com/cover.png"If no image is found, the function returns undefined.
API
extractImgUrl(markdown: string): string | undefinedReturns the URL of the first image that appears in the input string.
Supported Input
Common Markdown image syntax:


HTML img tags:
<img src="https://example.com/image.png">
<img src="https://example.com/image.png" alt="image" />
<img
src="https://example.com/image.png"
alt="image"
/>Mixed content:
Text before

<img src="https://example.com/image-2.png" alt="image">When both Markdown images and HTML img tags are present, the function returns the one that appears first in the string.
Scope
This library is optimized for typical Markdown and HTML image patterns. It does not aim to fully replicate Markdown parser behavior for rare or highly irregular edge cases.
License
MIT
