Ostatnio aktywny 1732922324

melanie's Avatar melanie zrewidował ten Gist 1732922324. Przejdź do rewizji

Brak zmian

melanie's Avatar melanie zrewidował ten Gist 1732918418. Przejdź do rewizji

1 file changed, 27 insertions

tapestry-attachment-extraction.js(stworzono plik)

@@ -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 + }
Nowsze Starsze