From 0e2c0037e468fc61e999b15825ec2d4ff0255b2c Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 20 Sep 2023 17:16:46 +0200 Subject: [PATCH] Change to use `alt` on images --- lib/index.js | 15 ++++++++++----- test/fixtures/document/output.md | 2 ++ test/fixtures/image-in-link/output.md | 1 + 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/index.js b/lib/index.js index 62a0f0e..49c5e0d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,4 +1,5 @@ /** + * @typedef {import('mdast').PhrasingContent} PhrasingContent * @typedef {import('mdast').Root} Root */ @@ -31,11 +32,15 @@ export default function remarkUnlink() { node.type === 'imageReference' || node.type === 'definition') ) { - parent.children.splice( - index, - 1, - ...('children' in node ? node.children : []) - ) + /** @type {Array} */ + const replacement = + 'children' in node + ? node.children + : 'alt' in node && typeof node.alt === 'string' + ? [{type: 'text', value: node.alt}] + : [] + + parent.children.splice(index, 1, ...replacement) return index } }) diff --git a/test/fixtures/document/output.md b/test/fixtures/document/output.md index 2a548f6..bd16283 100644 --- a/test/fixtures/document/output.md +++ b/test/fixtures/document/output.md @@ -9,4 +9,6 @@ Section content may include some links. ## section 2 +some images are here also + More content. diff --git a/test/fixtures/image-in-link/output.md b/test/fixtures/image-in-link/output.md index e69de29..773b222 100644 --- a/test/fixtures/image-in-link/output.md +++ b/test/fixtures/image-in-link/output.md @@ -0,0 +1 @@ +image