ptn-helpers 0.0.17 → 0.0.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.d.ts +4 -4
- package/lib/index.js +38 -47
- package/lib/youtube.d.ts +18 -0
- package/lib/youtube.js +107 -0
- package/package.json +17 -11
package/lib/index.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
export declare const FEED_LINK_KEYS: string[];
|
|
2
|
-
export
|
|
3
|
-
export
|
|
2
|
+
export type FeedLinkKey = (typeof FEED_LINK_KEYS)[number];
|
|
3
|
+
export type FeedAttachment = {
|
|
4
4
|
[feedLinkKey in FeedLinkKey]?: string | undefined;
|
|
5
5
|
} & {
|
|
6
6
|
_ptr_media_type: string;
|
|
7
7
|
_ptr_open_graph_image_url?: string;
|
|
8
8
|
url?: string;
|
|
9
9
|
};
|
|
10
|
-
export
|
|
10
|
+
export type FeedItem = {
|
|
11
11
|
id: string;
|
|
12
12
|
date_published: string;
|
|
13
13
|
url: string;
|
|
@@ -15,7 +15,7 @@ export declare type FeedItem = {
|
|
|
15
15
|
attachments?: FeedAttachment[];
|
|
16
16
|
_ptr_sender_type: string;
|
|
17
17
|
};
|
|
18
|
-
export
|
|
18
|
+
export type PtnNode = {
|
|
19
19
|
text: string;
|
|
20
20
|
children: PtnNode[];
|
|
21
21
|
uid?: string;
|
package/lib/index.js
CHANGED
|
@@ -1,91 +1,83 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.organizeFeedItems = exports.itemToNode = exports.FEED_LINK_KEYS = void 0;
|
|
4
|
-
var date_fns_1 = require("date-fns");
|
|
5
|
-
exports.FEED_LINK_KEYS = [
|
|
1
|
+
import { format } from "date-fns";
|
|
2
|
+
export const FEED_LINK_KEYS = [
|
|
6
3
|
"title",
|
|
7
4
|
"_ptr_open_graph_description",
|
|
8
5
|
"_ptr_open_graph_site_name",
|
|
9
6
|
"_ptr_open_graph_type",
|
|
10
7
|
];
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
(_e = feedItem === null || feedItem === void 0 ? void 0 : feedItem.attachments) === null || _e === void 0 ? void 0 : _e.forEach(function (element) {
|
|
17
|
-
var _a;
|
|
8
|
+
export const itemToNode = (feedItem, hashtag) => {
|
|
9
|
+
const children = [];
|
|
10
|
+
let text = feedItem?.content_text ?? "";
|
|
11
|
+
if ((feedItem?.attachments?.length ?? 0) > 1 && feedItem?.attachments?.[0]._ptr_media_type === "text") {
|
|
12
|
+
feedItem?.attachments?.forEach((element) => {
|
|
18
13
|
children.push({
|
|
19
|
-
text:
|
|
14
|
+
text: element.title ?? "",
|
|
20
15
|
children: [],
|
|
21
16
|
});
|
|
22
17
|
});
|
|
23
18
|
}
|
|
24
19
|
else {
|
|
25
|
-
|
|
26
|
-
if (
|
|
27
|
-
if (
|
|
20
|
+
const attachment = feedItem?.attachments?.[0];
|
|
21
|
+
if (attachment?._ptr_media_type === "link") {
|
|
22
|
+
if (attachment?._ptr_open_graph_image_url?.length ?? 0 > 0) {
|
|
28
23
|
children.push({
|
|
29
|
-
text:
|
|
24
|
+
text: ``,
|
|
30
25
|
children: [],
|
|
31
26
|
});
|
|
32
27
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if ((_a = v === null || v === void 0 ? void 0 : v.length) !== null && _a !== void 0 ? _a : 0 > 0) {
|
|
28
|
+
FEED_LINK_KEYS.forEach((k) => {
|
|
29
|
+
const v = attachment[k];
|
|
30
|
+
if (v?.length ?? 0 > 0) {
|
|
37
31
|
children.push({
|
|
38
|
-
text:
|
|
32
|
+
text: `${k.replace("_ptr_", "").replace(/_/g, " ")}:: ${v?.trim()}`,
|
|
39
33
|
children: [],
|
|
40
34
|
});
|
|
41
35
|
}
|
|
42
36
|
});
|
|
43
37
|
}
|
|
44
|
-
else if (
|
|
45
|
-
if (
|
|
38
|
+
else if (attachment?._ptr_media_type === "text") {
|
|
39
|
+
if (attachment?.title?.length ?? 0 > 0) {
|
|
46
40
|
children.push({
|
|
47
|
-
text:
|
|
41
|
+
text: `${attachment.title}`,
|
|
48
42
|
children: [],
|
|
49
43
|
});
|
|
50
44
|
}
|
|
51
45
|
}
|
|
52
|
-
else if (
|
|
53
|
-
text =
|
|
46
|
+
else if (attachment?._ptr_media_type === "image") {
|
|
47
|
+
text = ``;
|
|
54
48
|
// this is captions
|
|
55
|
-
if (
|
|
49
|
+
if (attachment?.title?.length ?? 0 > 0) {
|
|
56
50
|
children.push({
|
|
57
|
-
text:
|
|
51
|
+
text: `${attachment.title}`,
|
|
58
52
|
children: [],
|
|
59
53
|
});
|
|
60
54
|
}
|
|
61
55
|
}
|
|
62
|
-
else if (
|
|
63
|
-
|
|
64
|
-
text =
|
|
56
|
+
else if (attachment?._ptr_media_type === "audio") {
|
|
57
|
+
const title = feedItem.content_text?.trim()?.length > 0 ? feedItem.content_text : "Audio Recording";
|
|
58
|
+
text = `[${title}](${attachment.url})`;
|
|
65
59
|
}
|
|
66
|
-
else if (
|
|
67
|
-
text =
|
|
60
|
+
else if (attachment?._ptr_media_type === "document" && attachment.url) {
|
|
61
|
+
text = `[${attachment?.title || "document attachment"}](${attachment.url})`;
|
|
68
62
|
}
|
|
69
63
|
}
|
|
70
|
-
text =
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
64
|
+
text = `${text.trim()}`;
|
|
65
|
+
const validHashtag = hashtag && typeof hashtag === "string" && hashtag.length > 0;
|
|
66
|
+
const existingTags = /#\w+to(roam|note)/;
|
|
67
|
+
const needsNewTag = !text.match(existingTags)?.length;
|
|
74
68
|
if (validHashtag && needsNewTag) {
|
|
75
|
-
text =
|
|
69
|
+
text = `${text} #${hashtag}`;
|
|
76
70
|
}
|
|
77
71
|
return {
|
|
78
|
-
text:
|
|
72
|
+
text: `${text}`,
|
|
79
73
|
children: children,
|
|
80
74
|
uid: feedItem.id,
|
|
81
75
|
};
|
|
82
76
|
};
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
var date = new Date(feedItem.date_published), pageName = (0, date_fns_1.format)(date, dateFormat), senderType = feedItem._ptr_sender_type;
|
|
88
|
-
if (!obj.hasOwnProperty(pageName)) {
|
|
77
|
+
export const organizeFeedItems = (feedItems, dateFormat = "MMMM do, yyyy") => {
|
|
78
|
+
const reduceFeedItems = (obj, feedItem) => {
|
|
79
|
+
const date = new Date(feedItem.date_published), pageName = format(date, dateFormat), senderType = feedItem._ptr_sender_type;
|
|
80
|
+
if (!Object.hasOwn(obj, pageName)) {
|
|
89
81
|
obj[pageName] = {};
|
|
90
82
|
}
|
|
91
83
|
if (!obj[pageName][senderType]) {
|
|
@@ -96,4 +88,3 @@ var organizeFeedItems = function (feedItems, dateFormat) {
|
|
|
96
88
|
};
|
|
97
89
|
return feedItems.reduce(reduceFeedItems, {});
|
|
98
90
|
};
|
|
99
|
-
exports.organizeFeedItems = organizeFeedItems;
|
package/lib/youtube.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare const MIN_PERIOD_PERCENTAGE = 0.3;
|
|
2
|
+
export declare const GENERIC_ERROR_MESSAGE = "sorry, we couldn't get a transcript for that video. future versions of this extension will support more videos";
|
|
3
|
+
export declare const NO_VIDEO_ERROR = "please select a block with a youtube url to get a transcript";
|
|
4
|
+
export declare const GET_TRANSCRIPT_LABEL = "Get Youtube Transcript";
|
|
5
|
+
export declare const LOADING_MESSAGE = "loading transcript...";
|
|
6
|
+
export declare type CaptionData = {
|
|
7
|
+
start: number;
|
|
8
|
+
text_arr: string[];
|
|
9
|
+
formatted_timestamp: string;
|
|
10
|
+
link: string;
|
|
11
|
+
};
|
|
12
|
+
declare const UKNOWN_ERROR = "sorry, an unknown error occurred";
|
|
13
|
+
declare function getPossibleYoutubeId(targetUid: string, getTextByBlockUid: (uid: string) => string, getParentUidByBlockUid: (uid: string) => string): {
|
|
14
|
+
possibleYoutubeId: string | undefined;
|
|
15
|
+
isTextOnBlock: boolean;
|
|
16
|
+
};
|
|
17
|
+
declare const generateTranscript: (youtubeId: string, uidToInsert: string, insertCaptions: (uidToInsert: string, data: CaptionData[]) => void) => Promise<void>;
|
|
18
|
+
export { getPossibleYoutubeId, generateTranscript, UKNOWN_ERROR };
|
package/lib/youtube.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.UKNOWN_ERROR = exports.generateTranscript = exports.getPossibleYoutubeId = exports.LOADING_MESSAGE = exports.GET_TRANSCRIPT_LABEL = exports.NO_VIDEO_ERROR = exports.GENERIC_ERROR_MESSAGE = exports.MIN_PERIOD_PERCENTAGE = void 0;
|
|
40
|
+
exports.MIN_PERIOD_PERCENTAGE = 0.3;
|
|
41
|
+
exports.GENERIC_ERROR_MESSAGE = "sorry, we couldn't get a transcript for that video. future versions of this extension will support more videos";
|
|
42
|
+
exports.NO_VIDEO_ERROR = "please select a block with a youtube url to get a transcript";
|
|
43
|
+
exports.GET_TRANSCRIPT_LABEL = "Get Youtube Transcript";
|
|
44
|
+
exports.LOADING_MESSAGE = "loading transcript...";
|
|
45
|
+
var PTN_ROOT = "https://app.phonetonote.com";
|
|
46
|
+
var BASE_HEADERS = { "Content-Type": "application/json" };
|
|
47
|
+
var UKNOWN_ERROR = "sorry, an unknown error occurred";
|
|
48
|
+
exports.UKNOWN_ERROR = UKNOWN_ERROR;
|
|
49
|
+
function extractYoutubeId(text) {
|
|
50
|
+
if (!text)
|
|
51
|
+
return undefined;
|
|
52
|
+
var regex = /(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:watch\?v=))([\w-]{11})/;
|
|
53
|
+
var match = text.match(regex);
|
|
54
|
+
if (match && match[1]) {
|
|
55
|
+
return match[1];
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function getPossibleYoutubeId(targetUid, getTextByBlockUid, getParentUidByBlockUid) {
|
|
62
|
+
var possibleYoutubeId = undefined;
|
|
63
|
+
var isTextOnBlock = false;
|
|
64
|
+
if (targetUid) {
|
|
65
|
+
var textOnBlock = getTextByBlockUid(targetUid);
|
|
66
|
+
possibleYoutubeId = extractYoutubeId(textOnBlock);
|
|
67
|
+
if (possibleYoutubeId) {
|
|
68
|
+
isTextOnBlock = true;
|
|
69
|
+
}
|
|
70
|
+
if (!possibleYoutubeId) {
|
|
71
|
+
var parentUid = getParentUidByBlockUid(targetUid);
|
|
72
|
+
var textOnParent = getTextByBlockUid(parentUid);
|
|
73
|
+
possibleYoutubeId = extractYoutubeId(textOnParent);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return { possibleYoutubeId: possibleYoutubeId, isTextOnBlock: isTextOnBlock };
|
|
77
|
+
}
|
|
78
|
+
exports.getPossibleYoutubeId = getPossibleYoutubeId;
|
|
79
|
+
var generateTranscript = function (youtubeId, uidToInsert, insertCaptions) { return __awaiter(void 0, void 0, void 0, function () {
|
|
80
|
+
var res, data;
|
|
81
|
+
return __generator(this, function (_a) {
|
|
82
|
+
switch (_a.label) {
|
|
83
|
+
case 0: return [4 /*yield*/, fetch("".concat(PTN_ROOT, "/youtube-transcript/generate"), {
|
|
84
|
+
method: "POST",
|
|
85
|
+
body: JSON.stringify({ youtube_id: youtubeId }),
|
|
86
|
+
headers: BASE_HEADERS,
|
|
87
|
+
})];
|
|
88
|
+
case 1:
|
|
89
|
+
res = _a.sent();
|
|
90
|
+
if (!(res.status.toString()[0] !== "2")) return [3 /*break*/, 2];
|
|
91
|
+
alert(UKNOWN_ERROR);
|
|
92
|
+
return [3 /*break*/, 4];
|
|
93
|
+
case 2: return [4 /*yield*/, res.json()];
|
|
94
|
+
case 3:
|
|
95
|
+
data = _a.sent();
|
|
96
|
+
if (data.error) {
|
|
97
|
+
alert(data.error);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
insertCaptions(uidToInsert, data);
|
|
101
|
+
}
|
|
102
|
+
_a.label = 4;
|
|
103
|
+
case 4: return [2 /*return*/];
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}); };
|
|
107
|
+
exports.generateTranscript = generateTranscript;
|
package/package.json
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ptn-helpers",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.18",
|
|
4
|
+
"type": "module",
|
|
4
5
|
"description": "helper functions to fetch and display ptn data",
|
|
5
6
|
"main": "lib/index.js",
|
|
6
7
|
"types": "lib/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./lib/index.d.ts",
|
|
11
|
+
"import": "./lib/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
7
14
|
"files": [
|
|
8
15
|
"lib/**/*"
|
|
9
16
|
],
|
|
@@ -11,7 +18,7 @@
|
|
|
11
18
|
"test": "jest --config jestconfig.json",
|
|
12
19
|
"build": "tsc",
|
|
13
20
|
"format": "prettier --write \"src/**/*.ts\"",
|
|
14
|
-
"lint": "eslint
|
|
21
|
+
"lint": "eslint src/",
|
|
15
22
|
"prepare": "npm run build",
|
|
16
23
|
"prepublishOnly": "npm test && npm run lint",
|
|
17
24
|
"preversion": "npm run lint",
|
|
@@ -34,16 +41,15 @@
|
|
|
34
41
|
},
|
|
35
42
|
"homepage": "https://github.com/phonetonote/ptn-helpers#readme",
|
|
36
43
|
"devDependencies": {
|
|
37
|
-
"@types/jest": "^
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"jest": "^
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"typescript": "^4.5.4"
|
|
44
|
+
"@types/jest": "^29.5.14",
|
|
45
|
+
"eslint": "^9.17.0",
|
|
46
|
+
"jest": "^29.7.0",
|
|
47
|
+
"prettier": "^3.4.2",
|
|
48
|
+
"ts-jest": "^29.2.5",
|
|
49
|
+
"typescript": "^5.7.2",
|
|
50
|
+
"typescript-eslint": "^8.19.0"
|
|
45
51
|
},
|
|
46
52
|
"dependencies": {
|
|
47
|
-
"date-fns": "^
|
|
53
|
+
"date-fns": "^4.1.0"
|
|
48
54
|
}
|
|
49
55
|
}
|