Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Sep 22, 2023
1 parent fe9b9a0 commit 69a65f4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
40 changes: 27 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
/**
* @typedef {import('mdast').Root} Root
* @typedef {import('mdast').Paragraph} Paragraph
* @typedef {import('mdast').Link} Link
* @typedef {import('mdast').LinkReference} LinkReference
* @typedef {import('mdast').Paragraph} Paragraph
* @typedef {import('mdast').Root} Root
*/

import {visit, SKIP} from 'unist-util-visit'
import {whitespace} from 'hast-util-whitespace'
import {SKIP, visit} from 'unist-util-visit'

const unknown = 1
const containsImage = 2
const containsOther = 3

/**
* Plugin to remove the wrapping paragraph for images.
* Remove the wrapping paragraph for images.
*
* @type {import('unified').Plugin<void[], Root>}
* @returns
* Transform.
*/
export default function remarkUnwrapImages() {
return (tree) => {
visit(tree, 'paragraph', (node, index, parent) => {
/**
* Transform.
*
* @param {Root} tree
* Tree.
* @returns {undefined}
* Nothing.
*/
return function (tree) {
visit(tree, 'paragraph', function (node, index, parent) {
if (
parent &&
typeof index === 'number' &&
applicable(node) === containsImage
applicable(node, false) === containsImage
) {
parent.children.splice(index, 1, ...node.children)
return [SKIP, index]
Expand All @@ -33,20 +42,25 @@ export default function remarkUnwrapImages() {
}

/**
* @param {Paragraph|Link|LinkReference} node
* @param {boolean} [inLink]
* @returns {1|2|3}
* Check if a node can be unraveled.
*
* @param {Link | LinkReference | Paragraph} node
* Node.
* @param {boolean} inLink
* Whether the node is in a link.
* @returns {1 | 2 | 3}
* Info.
*/
function applicable(node, inLink) {
/** @type {1|2|3} */
/** @type {1 | 2 | 3} */
let image = unknown
let index = -1

while (++index < node.children.length) {
const child = node.children[index]

if (child.type === 'text' && whitespace(child.value)) {
// White space is fine.
// Whitespace is fine.
} else if (child.type === 'image' || child.type === 'imageReference') {
image = containsImage
} else if (
Expand Down
6 changes: 6 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import {unified} from 'unified'
import remarkUnwrapImages from './index.js'

test('remarkUnwrapImages', async function (t) {
await t.test('should expose the public api', async function () {
assert.deepEqual(Object.keys(await import('./index.js')).sort(), [
'default'
])
})

await t.test('should unwrap images', async function () {
assert.equal(
String(
Expand Down

0 comments on commit 69a65f4

Please sign in to comment.