Last active 1741105238

melanie revised this gist 1741105238. Go to revision

No changes

melanie revised this gist 1732922324. Go to revision

No changes

melanie revised this gist 1732918418. Go to revision

1 file changed, 27 insertions

tapestry-attachment-extraction.js(file created)

@@ -0,0 +1,27 @@
1 + /*
2 + Attachment Extracting
3 + for Iconfactory's Tapestry plugins
4 +
5 + Grabs urls from a string, like `content_html` in a JSON Feed, and makes them attachments.
6 +
7 + Made to be used in your Tapestry plugins like so:
8 +
9 + ```js
10 + const myItem = Item.createWithUriDate(url, date);
11 + myItem.attachments = extractAttachments(content_html, [rest?.external_url, rest?.image])
12 + ```
13 + */
14 +
15 + const URL_REGEX =
16 + /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/g;
17 + const MEDIA_EXT = ["png", "tif", "tiff", "jpeg", "jpg", "gif", "bmp", "bmpf", "ico", "cur", "xbm", "aac", "aiff", "aifc", "avi", "ac3", "mp4", "m2v", "ts", "mpg", "mp2", "mp3", "au", "3gp", "3g2"];
18 +
19 + function extractAttachments(from = "", exclude = []) {
20 + return (from.match(URL_REGEX) || [])
21 + .filter((url) => !exclude.includes(url))
22 + .map((url) =>
23 + MEDIA_EXT.some((ext) => url.includes(`.${ext}`))
24 + ? MediaAttachment.createWithUrl(url)
25 + : LinkAttachment.createWithUrl(url)
26 + );
27 + }
Newer Older