Skip to content

Commit

Permalink
Add link if there isn’t one
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 19, 2019
1 parent b97081a commit 11e51ca
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
21 changes: 17 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,28 @@ function transform(tree) {
}

function ontext(node, index, parent) {
const text = String(node.value).trim()
const value = String(node.value).trim()

if ((isUrl(text) || isImgPath(text)) && isImgExt(text)) {
parent.children[index] = {
if ((isUrl(value) || isImgPath(value)) && isImgExt(value)) {
let next = {
type: 'image',
url: text,
url: value,
title: null,
alt: null,
position: node.position
}

// Add a link if we’re not already in one.
if (parent.type !== 'link') {
next = {
type: 'link',
url: value,
title: null,
children: [next],
position: node.position
}
}

parent.children[index] = next
}
}
6 changes: 3 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test('remark-images', function(t) {
.use(images)
.processSync('/example.jpg')
.toString(),
'![](/example.jpg)\n',
'[![](/example.jpg)](/example.jpg)\n',
'should support absolute paths to images'
)

Expand All @@ -35,7 +35,7 @@ test('remark-images', function(t) {
.use(images)
.processSync('./example.jpg')
.toString(),
'![](./example.jpg)\n',
'[![](./example.jpg)](./example.jpg)\n',
'should support relative paths to images'
)

Expand All @@ -44,7 +44,7 @@ test('remark-images', function(t) {
.use(images)
.processSync('../example.jpg')
.toString(),
'![](../example.jpg)\n',
'[![](../example.jpg)](../example.jpg)\n',
'should support very relative paths to images'
)

Expand Down

0 comments on commit 11e51ca

Please sign in to comment.