This repository has been archived by the owner on Oct 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
73 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ | |
"main": "index.js", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"lib/", | ||
"index.d.ts", | ||
"index.js" | ||
], | ||
|