-
Notifications
You must be signed in to change notification settings - Fork 38
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
1 parent
86cb716
commit cb53c1b
Showing
4 changed files
with
50 additions
and
29 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
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,37 +1,47 @@ | ||
import { JSDOM } from 'jsdom'; | ||
|
||
const removeFirstImageIfMatch = (htmlString, imageUrl)=> { | ||
// Parse the HTML string with jsdom | ||
const removeFirstImageIfMatch = (htmlString, imageUrlToMatch) => { | ||
const dom = new JSDOM(htmlString); | ||
const document = dom.window.document; | ||
|
||
// Parse the imageUrlToMatch to ignore query strings | ||
const targetUrl = new dom.window.URL(imageUrlToMatch, 'http://example.com'); // Base URL for relative URLs | ||
|
||
// Get all image elements | ||
const images = document.querySelectorAll('img'); | ||
|
||
// Check if the first image matches the given URL and it's the very first element | ||
if (images.length > 0 && images[0].src === imageUrl) { | ||
const firstElementChild = images[0].parentNode.firstChild; | ||
let isVeryFirstImage = false; | ||
|
||
// Traverse to check if it's the very first element (ignoring text nodes, comments, etc.) | ||
let currentNode = firstElementChild; | ||
while (currentNode) { | ||
if (currentNode.nodeType === dom.window.Node.ELEMENT_NODE) { | ||
if (currentNode === images[0]) { | ||
isVeryFirstImage = true; | ||
if (images.length > 0) { | ||
// Create a URL object from the first image's src for comparison | ||
const firstImageUrl = new dom.window.URL(images[0].src, 'http://example.com'); // Same base URL for comparison | ||
|
||
// Check if the first image matches the given URL without the query string | ||
if ( | ||
firstImageUrl.origin === targetUrl.origin && | ||
firstImageUrl.pathname === targetUrl.pathname && | ||
images[0] === images[0].parentNode.firstChild | ||
) { | ||
// Determine if the image is within a <figure> element | ||
let parentElement = images[0].parentNode; | ||
while (parentElement && parentElement !== document.body) { | ||
if (parentElement.tagName.toLowerCase() === 'figure') { | ||
// If the parent <figure> is found, remove the <figure> element | ||
parentElement.parentNode.removeChild(parentElement); | ||
break; | ||
} else if (parentElement.parentNode) { | ||
// Move up the DOM tree if no <figure> is found immediately | ||
parentElement = parentElement.parentNode; | ||
} else { | ||
// If the image is not within a <figure>, remove just the image | ||
images[0].parentNode.removeChild(images[0]); | ||
break; | ||
} | ||
break; | ||
} | ||
currentNode = currentNode.nextSibling; | ||
} | ||
|
||
// If it's the very first image, remove it | ||
if (isVeryFirstImage) { | ||
images[0].parentNode.removeChild(images[0]); | ||
} | ||
} | ||
|
||
// Serialize the DOM back to a string and return it | ||
return dom.serialize(); | ||
} | ||
}; | ||
|
||
|
||
export default removeFirstImageIfMatch |
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
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