-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(images): display images within posts
- Loading branch information
1 parent
5942a1d
commit c5e61df
Showing
4 changed files
with
66 additions
and
4 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
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
export interface ExtractedImgLinks { | ||
txtContent: string, | ||
imgLinks: string[], | ||
} | ||
|
||
export function extractImageLinks(textContent: string): ExtractedImgLinks { | ||
/* F(x) to extract image links within text content data */ | ||
|
||
const tmpTxtContent = textContent.replaceAll("\n", " "); | ||
const imgExtensions = ["png", "jpg", "jpeg", "webp", "gif"]; | ||
const words = tmpTxtContent.split(" "); | ||
let imgLinks: string[] = []; | ||
|
||
for (let i = 0; i < words.length; i++) { | ||
if (words[i].slice(0, 4) != "http") continue; | ||
|
||
const subWords = words[i].split("."); | ||
const ext = subWords[subWords.length - 1]; | ||
|
||
if (imgExtensions.includes(ext)) { | ||
imgLinks.push(words[i]); | ||
} else { | ||
continue | ||
}; | ||
|
||
textContent = textContent.replaceAll(words[i], ""); | ||
} | ||
|
||
return { | ||
txtContent: textContent, | ||
imgLinks, | ||
} | ||
} |