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

Commit

Permalink
Refactor to move code to lib/
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Sep 22, 2023
1 parent 4c2769e commit 6cf0c6d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 71 deletions.
72 changes: 1 addition & 71 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,71 +1 @@
/**
* @typedef {import('mdast').Root} Root
* @typedef {import('mdast').Paragraph} Paragraph
* @typedef {import('mdast').Link} Link
* @typedef {import('mdast').LinkReference} LinkReference
*/

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

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

/**
* Plugin to remove the wrapping paragraph for images.
*
* @type {import('unified').Plugin<void[], Root>}
*/
export default function remarkUnwrapImages() {
return (tree) => {
visit(tree, 'paragraph', (node, index, parent) => {
if (
parent &&
typeof index === 'number' &&
applicable(node) === containsImage
) {
parent.children.splice(index, 1, ...node.children)
return [SKIP, index]
}
})
}
}

/**
* @param {Paragraph|Link|LinkReference} node
* @param {boolean} [inLink]
* @returns {1|2|3}
*/
function applicable(node, inLink) {
/** @type {1|2|3} */
let image = unknown
let index = -1

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

if (whitespace(child)) {
// White space is fine.
} else if (child.type === 'image' || child.type === 'imageReference') {
image = containsImage
} else if (
!inLink &&
(child.type === 'link' || child.type === 'linkReference')
) {
const linkResult = applicable(child, true)

if (linkResult === containsOther) {
return containsOther
}

if (linkResult === containsImage) {
image = containsImage
}
} else {
return containsOther
}
}

return image
}
export {default} from './lib/index.js'
71 changes: 71 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* @typedef {import('mdast').Root} Root
* @typedef {import('mdast').Paragraph} Paragraph
* @typedef {import('mdast').Link} Link
* @typedef {import('mdast').LinkReference} LinkReference
*/

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

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

/**
* Plugin to remove the wrapping paragraph for images.
*
* @type {import('unified').Plugin<void[], Root>}
*/
export default function remarkUnwrapImages() {
return (tree) => {
visit(tree, 'paragraph', (node, index, parent) => {
if (
parent &&
typeof index === 'number' &&
applicable(node) === containsImage
) {
parent.children.splice(index, 1, ...node.children)
return [SKIP, index]
}
})
}
}

/**
* @param {Paragraph|Link|LinkReference} node
* @param {boolean} [inLink]
* @returns {1|2|3}
*/
function applicable(node, inLink) {
/** @type {1|2|3} */
let image = unknown
let index = -1

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

if (whitespace(child)) {
// White space is fine.
} else if (child.type === 'image' || child.type === 'imageReference') {
image = containsImage
} else if (
!inLink &&
(child.type === 'link' || child.type === 'linkReference')
) {
const linkResult = applicable(child, true)

if (linkResult === containsOther) {
return containsOther
}

if (linkResult === containsImage) {
image = containsImage
}
} else {
return containsOther
}
}

return image
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"main": "index.js",
"types": "index.d.ts",
"files": [
"lib/",
"index.d.ts",
"index.js"
],
Expand Down

0 comments on commit 6cf0c6d

Please sign in to comment.