Skip to content

Commit

Permalink
Update @types/mdast, utilities, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Sep 22, 2023
1 parent 6cf0c6d commit fe9b9a0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 36 deletions.
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function applicable(node, inLink) {
while (++index < node.children.length) {
const child = node.children[index]

if (whitespace(child)) {
if (child.type === 'text' && whitespace(child.value)) {
// White space is fine.
} else if (child.type === 'image' || child.type === 'imageReference') {
image = containsImage
Expand Down
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,23 @@
"index.js"
],
"dependencies": {
"@types/mdast": "^3.0.0",
"hast-util-whitespace": "^2.0.0",
"unified": "^10.0.0",
"unist-util-visit": "^4.0.0"
"@types/mdast": "^4.0.0",
"hast-util-whitespace": "^3.0.0",
"unist-util-visit": "^5.0.0"
},
"devDependencies": {
"@types/node": "^20.0.0",
"c8": "^8.0.0",
"prettier": "^3.0.0",
"remark": "^14.0.0",
"rehype-stringify": "^10.0.0",
"remark": "^15.0.0",
"remark-cli": "^11.0.0",
"remark-html": "^15.0.0",
"remark-parse": "^11.0.0",
"remark-preset-wooorm": "^9.0.0",
"remark-rehype": "^11.0.0",
"type-coverage": "^2.0.0",
"typescript": "^5.0.0",
"unified": "^11.0.0",
"xo": "^0.56.0"
},
"scripts": {
Expand Down
78 changes: 49 additions & 29 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import {remark} from 'remark'
import remarkHtml from 'remark-html'
import rehypeStringify from 'rehype-stringify'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import {unified} from 'unified'
import remarkUnwrapImages from './index.js'

test('remarkUnwrapImages', async function (t) {
await t.test('should unwrap images', async function () {
assert.equal(
String(
await remark()
await unified()
.use(remarkParse)
.use(remarkUnwrapImages)
.use(remarkHtml)
.use(remarkRehype)
.use(rehypeStringify)
.process('![hi](there.png)')
),
'<img src="there.png" alt="hi">\n'
'<img src="there.png" alt="hi">'
)
})

await t.test('should unwrap multiple images', async function () {
assert.equal(
String(
await remark()
await unified()
.use(remarkParse)
.use(remarkUnwrapImages)
.use(remarkHtml)
.use(remarkRehype)
.use(rehypeStringify)
.process('![alpha](alpha.png) ![bravo](bravo.png)')
),
'<img src="alpha.png" alt="alpha">\n \n<img src="bravo.png" alt="bravo">\n'
'<img src="alpha.png" alt="alpha">\n \n<img src="bravo.png" alt="bravo">'
)
})

Expand All @@ -34,25 +40,29 @@ test('remarkUnwrapImages', async function (t) {
async function () {
assert.equal(
String(
await remark()
await unified()
.use(remarkParse)
.use(remarkUnwrapImages)
.use(remarkHtml)
.use(remarkRehype)
.use(rehypeStringify)
.process('some text ![and](and.png) an image')
),
'<p>some text <img src="and.png" alt="and"> an image</p>\n'
'<p>some text <img src="and.png" alt="and"> an image</p>'
)
}
)

await t.test('should not unwrap if there are no images', async function () {
assert.equal(
String(
await remark()
await unified()
.use(remarkParse)
.use(remarkUnwrapImages)
.use(remarkHtml)
.use(remarkRehype)
.use(rehypeStringify)
.process('some text')
),
'<p>some text</p>\n'
'<p>some text</p>'
)
})

Expand All @@ -61,25 +71,29 @@ test('remarkUnwrapImages', async function (t) {
async function () {
assert.equal(
String(
await remark()
await unified()
.use(remarkParse)
.use(remarkUnwrapImages)
.use(remarkHtml)
.use(remarkRehype)
.use(rehypeStringify)
.process('[](#remark)')
),
'<p><a href="#remark"></a></p>\n'
'<p><a href="#remark"></a></p>'
)
}
)

await t.test('should supports links', async function () {
assert.equal(
String(
await remark()
await unified()
.use(remarkParse)
.use(remarkUnwrapImages)
.use(remarkHtml)
.use(remarkRehype)
.use(rehypeStringify)
.process('[![hi](there.png)](#remark)')
),
'<a href="#remark"><img src="there.png" alt="hi"></a>\n'
'<a href="#remark"><img src="there.png" alt="hi"></a>'
)
})

Expand All @@ -88,37 +102,43 @@ test('remarkUnwrapImages', async function (t) {
async function () {
assert.equal(
String(
await remark()
await unified()
.use(remarkParse)
.use(remarkUnwrapImages)
.use(remarkHtml)
.use(remarkRehype)
.use(rehypeStringify)
.process('[![hi](there.png)](#remark)!')
),
'<p><a href="#remark"><img src="there.png" alt="hi"></a>!</p>\n'
'<p><a href="#remark"><img src="there.png" alt="hi"></a>!</p>'
)
}
)

await t.test('should not unwrap links with other content', async function () {
assert.equal(
String(
await remark()
await unified()
.use(remarkParse)
.use(remarkUnwrapImages)
.use(remarkHtml)
.use(remarkRehype)
.use(rehypeStringify)
.process('[![Hello](there.png), world](#remark)')
),
'<p><a href="#remark"><img src="there.png" alt="Hello">, world</a></p>\n'
'<p><a href="#remark"><img src="there.png" alt="Hello">, world</a></p>'
)
})

await t.test('should supports image references', async function () {
assert.equal(
String(
await remark()
await unified()
.use(remarkParse)
.use(remarkUnwrapImages)
.use(remarkHtml)
.use(remarkRehype)
.use(rehypeStringify)
.process('![hi][image]\n\n[image]: kitten.png')
),
'<img src="kitten.png" alt="hi">\n'
'<img src="kitten.png" alt="hi">'
)
})
})

0 comments on commit fe9b9a0

Please sign in to comment.