Skip to content

Commit

Permalink
Add link option
Browse files Browse the repository at this point in the history
Closes GH-6.
  • Loading branch information
wooorm committed Sep 19, 2024
1 parent 46f964c commit c67de7f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* @property {ReadonlyArray<string> | null | undefined} [imageExtensions]
* File extensions (without dot) to treat as images (default:
* `defaultImageExtensions`).
* @property {boolean | null | undefined} [link]
* Whether to wrap the image with a link to it (default: `true`).
*/

import {collapseWhiteSpace} from 'collapse-white-space'
Expand All @@ -30,6 +32,7 @@ export default function remarkImages(options) {
const settings = options || emptyOptions
const imageExtensions = settings.imageExtensions || defaultImageExtensions
const imageExtensionRegex = new RegExp(`\\.(${imageExtensions.join('|')})$`)
const link = settings.link !== false

/**
* Transform.
Expand Down Expand Up @@ -79,7 +82,7 @@ export default function remarkImages(options) {
}

// Add a link if we’re not already in one.
if (!interactive) {
if (link && !interactive) {
replacement = {
type: 'link',
url: value,
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ Configuration (TypeScript type).
* `imageExtensions` (`Array<string>`, default:
[`defaultImageExtensions`][api-default-image-extensions])
— file extensions (without dot) to treat as images
* `link` (`boolean`, default: `true`)
— whether to wrap the image with a link to it

## Syntax

Expand Down
22 changes: 22 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,26 @@ test('remarkImages', async function (t) {
'/a b c.jpg\n'
)
})

await t.test('should support `link: false`', async function () {
assert.equal(
String(
await remark()
.use(remarkImages, {link: false})
.process('https://example.com/alpha.jpg')
),
'![](https://example.com/alpha.jpg)\n'
)
})

await t.test('should support `link: true`', async function () {
assert.equal(
String(
await remark()
.use(remarkImages, {link: true})
.process('https://example.com/alpha.jpg')
),
'[![](https://example.com/alpha.jpg)](https://example.com/alpha.jpg)\n'
)
})
})

0 comments on commit c67de7f

Please sign in to comment.