@design-sdk/figma-url 0.0.53 → 0.1.0

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/dist/index.mjs ADDED
@@ -0,0 +1,194 @@
1
+ // lib/constants.ts
2
+ var _FIGMA_EMBED_URL_PREFIX = "https://www.figma.com/embed";
3
+ var _FIGMA_FILE_URL_PREFIX = "https://www.figma.com/file";
4
+ var _FIGMA_DESIGN_URL_PREFIX = "https://www.figma.com/design";
5
+ var _FIGMA_BOARD_URL_PREFIX = "https://www.figma.com/board";
6
+ var _FIGMA_SLIDES_URL_PREFIX = "https://www.figma.com/slides";
7
+ var _FIGMA_SITE_URL_PREFIX = "https://www.figma.com/site";
8
+ var _PARAM_NODE_ID = "node-id";
9
+ var __FIGMA_DEMO_DEFAULT_FILE_ID = "Y0Gh77AqBoHH7dG1GtK3xF";
10
+ var __FIGMA_DEMO_DEFAULT_FILE_URL = "https://www.figma.com/file/Y0Gh77AqBoHH7dG1GtK3xF/";
11
+ var __FIGMA_DEMO_DEFAULT_FILE_NODE_URL = "https://www.figma.com/file/Y0Gh77AqBoHH7dG1GtK3xF/?node-id=264%3A49";
12
+
13
+ // lib/parse-url.ts
14
+ function parseFileId(url) {
15
+ const supportedPatterns = [
16
+ "https://www.figma.com/file/",
17
+ "https://www.figma.com/design/",
18
+ "https://www.figma.com/board/",
19
+ "https://www.figma.com/slides/",
20
+ "https://www.figma.com/site/"
21
+ ];
22
+ const matchedPattern = supportedPatterns.find(
23
+ (pattern) => url.includes(pattern)
24
+ );
25
+ if (matchedPattern) {
26
+ return url.split("/")[4];
27
+ } else {
28
+ throw `figma url must contain one of the supported patterns (file/, design/, board/, slides/, site/). the given was ${url}, which we cannot extract file id from it.`;
29
+ }
30
+ }
31
+ function formatNodeId(node_id) {
32
+ if (node_id.includes(":") || node_id.includes("%3A") || node_id.includes("-")) {
33
+ if (node_id.includes("%3A")) {
34
+ node_id = decodeURIComponent(node_id);
35
+ }
36
+ if (node_id.includes("-")) {
37
+ node_id = node_id.split("-").join(":");
38
+ }
39
+ if (node_id.match(/[0-9]+:[0-9]+/) !== null) {
40
+ return node_id;
41
+ }
42
+ }
43
+ }
44
+ function parseFileAndNodeId(url) {
45
+ try {
46
+ const _url = new URL(url);
47
+ const params = new URLSearchParams(_url.search);
48
+ const nodeId = formatNodeId(params.get("node-id"));
49
+ const fileId = parseFileId(url);
50
+ return {
51
+ url,
52
+ file: fileId,
53
+ node: nodeId
54
+ };
55
+ } catch (_) {
56
+ return;
57
+ }
58
+ }
59
+
60
+ // lib/analyze-url.ts
61
+ var FigmaUrlType = /* @__PURE__ */ ((FigmaUrlType2) => {
62
+ FigmaUrlType2["embed"] = "embed";
63
+ FigmaUrlType2["file"] = "file";
64
+ FigmaUrlType2["node"] = "node";
65
+ FigmaUrlType2["empty"] = "empty";
66
+ return FigmaUrlType2;
67
+ })(FigmaUrlType || {});
68
+ var FigmaFileOrNodeIdType = /* @__PURE__ */ ((FigmaFileOrNodeIdType2) => {
69
+ FigmaFileOrNodeIdType2["nodeid"] = "nodeid";
70
+ FigmaFileOrNodeIdType2["maybe_nodeid"] = "maybe_nodeid";
71
+ FigmaFileOrNodeIdType2["fileid"] = "fileid";
72
+ FigmaFileOrNodeIdType2["maybe_fileid"] = "maybe_fileid";
73
+ return FigmaFileOrNodeIdType2;
74
+ })(FigmaFileOrNodeIdType || {});
75
+ function analyze(url) {
76
+ if (!url) {
77
+ return "empty" /* empty */;
78
+ }
79
+ let _u;
80
+ try {
81
+ _u = new URL(url);
82
+ } catch (_) {
83
+ const maybeidlike = url;
84
+ if (maybeidlike.length > 0) {
85
+ if (formatNodeId(maybeidlike)) {
86
+ return "maybe_nodeid" /* maybe_nodeid */;
87
+ } else if (maybeidlike.length >= 22) {
88
+ const _taget = decodeURI(maybeidlike);
89
+ if (_taget.match(/[a-zA-Z0-9]/) !== null) {
90
+ if (_taget.length == 22) {
91
+ return "fileid" /* fileid */;
92
+ } else {
93
+ return "maybe_fileid" /* maybe_fileid */;
94
+ }
95
+ }
96
+ }
97
+ }
98
+ throw `this url cannot be analyzed. this is not a valid url string - "${url}"`;
99
+ }
100
+ if (url.startsWith(_FIGMA_EMBED_URL_PREFIX)) {
101
+ return "embed" /* embed */;
102
+ } else {
103
+ if (_u.hostname == "figma.com" || _u.hostname == "www.figma.com") {
104
+ const supportedPathPatterns = [
105
+ "file/",
106
+ "design/",
107
+ "board/",
108
+ "slides/",
109
+ "site/"
110
+ ];
111
+ const matchedPattern = supportedPathPatterns.find(
112
+ (pattern) => _u.pathname.includes(pattern)
113
+ );
114
+ if (matchedPattern) {
115
+ if (_u.searchParams.get(_PARAM_NODE_ID)?.length > 0) {
116
+ return "node" /* node */;
117
+ } else {
118
+ return "file" /* file */;
119
+ }
120
+ }
121
+ }
122
+ throw `not a valid figma url.`;
123
+ }
124
+ }
125
+
126
+ // lib/embed-url.ts
127
+ function embed(src) {
128
+ const url = builEmbedableSourceUrl(src);
129
+ const urltype = analyze(url);
130
+ switch (urltype) {
131
+ case "embed" /* embed */:
132
+ return url;
133
+ case "file" /* file */:
134
+ console.warn(
135
+ "incomplete url input. the target node is not specified in the embedding target source url. this wont display contents as expected"
136
+ );
137
+ return _build(url);
138
+ case "node" /* node */:
139
+ return _build(url);
140
+ case "empty" /* empty */:
141
+ return;
142
+ }
143
+ }
144
+ function _build(url) {
145
+ const _embed_url = `${_FIGMA_EMBED_URL_PREFIX}?embed_host=astra&url=${url}`;
146
+ return _embed_url;
147
+ }
148
+ function builEmbedableSourceUrl(src) {
149
+ if (!src) {
150
+ return;
151
+ }
152
+ if (typeof src == "string") {
153
+ return src;
154
+ } else if ("url" in src) {
155
+ return src.url;
156
+ } else if ("fileid" in src) {
157
+ return `https://www.figma.com/design/${src.fileid}/${src.nodeid && `?node-id=${src.nodeid}`}`;
158
+ } else {
159
+ return void 0;
160
+ }
161
+ }
162
+
163
+ // lib/access-check.ts
164
+ async function isPublic(url) {
165
+ analyze(url);
166
+ {
167
+ try {
168
+ const res = await fetch(url);
169
+ return res.status == 200;
170
+ } catch (e) {
171
+ return false;
172
+ }
173
+ }
174
+ return false;
175
+ }
176
+
177
+ // lib/compare-url.ts
178
+ function isSameDesignUrl(url1, url2) {
179
+ const analysis = analyze(url1);
180
+ switch (analysis) {
181
+ case "embed" /* embed */:
182
+ case "empty" /* empty */:
183
+ case "file" /* file */:
184
+ return false;
185
+ case "node" /* node */:
186
+ const parsed = parseFileAndNodeId(url1);
187
+ const _this_parsed = parseFileAndNodeId(url2);
188
+ return _this_parsed.file === parsed.file && _this_parsed.node === parsed.node;
189
+ }
190
+ }
191
+
192
+ export { FigmaFileOrNodeIdType, FigmaUrlType, _FIGMA_BOARD_URL_PREFIX, _FIGMA_DESIGN_URL_PREFIX, _FIGMA_EMBED_URL_PREFIX, _FIGMA_FILE_URL_PREFIX, _FIGMA_SITE_URL_PREFIX, _FIGMA_SLIDES_URL_PREFIX, _PARAM_NODE_ID, __FIGMA_DEMO_DEFAULT_FILE_ID, __FIGMA_DEMO_DEFAULT_FILE_NODE_URL, __FIGMA_DEMO_DEFAULT_FILE_URL, analyze, builEmbedableSourceUrl, embed, formatNodeId, isPublic, isSameDesignUrl, parseFileAndNodeId, parseFileId };
193
+ //# sourceMappingURL=index.mjs.map
194
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../lib/constants.ts","../lib/parse-url.ts","../lib/analyze-url.ts","../lib/embed-url.ts","../lib/access-check.ts","../lib/compare-url.ts"],"names":["FigmaUrlType","FigmaFileOrNodeIdType"],"mappings":";AAMO,IAAM,uBAAA,GAA0B;AAOhC,IAAM,sBAAA,GAAyB;AAO/B,IAAM,wBAAA,GAA2B;AAOjC,IAAM,uBAAA,GAA0B;AAOhC,IAAM,wBAAA,GAA2B;AAOjC,IAAM,sBAAA,GAAyB;AAO/B,IAAM,cAAA,GAAiB;AAOvB,IAAM,4BAAA,GAA+B;AACrC,IAAM,6BAAA,GACX;AACK,IAAM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A;AAAA,IACT;AAAA,EACF;AACA,EAAA,OAAO,KAAA;AACT;;;ACrBO,SAAS,eAAA,CAAgB,MAAc,IAAA,EAAuB;AACnE,EAAA,MAAM,QAAA,GAAW,QAAQ,IAAI,CAAA;AAC7B,EAAA,QAAQ,QAAA;AAAU,IAChB,KAAA,OAAA;AAAA,IACA,KAAA,OAAA;AAAA,IACA,KAAA,MAAA;AACE,MAAA,OAAO,KAAA;AAAA,IACT,KAAA,MAAA;AACE,MAAA,MAAM,MAAA,GAAS,mBAAmB,IAAI,CAAA;AACtC,MAAA,MAAM,YAAA,GAAe,mBAAmB,IAAI,CAAA;AAC5C,MAAA,OACE,aAAa,IAAA,KAAS,MAAA,CAAO,IAAA,IAAQ,YAAA,CAAa,SAAS,MAAA,CAAO,IAAA;AAAA;AAG1E","file":"index.mjs","sourcesContent":["/**\n * e.g. full url should look like -\n * https://www.figma.com/embed?embed_host=astra&url=https://www.figma.com/design/HSozKEVWhh8saZa2vr1Nxd?node-id=111%3A0\n *\n * ref: https://www.figma.com/developers/embed\n */\nexport const _FIGMA_EMBED_URL_PREFIX = \"https://www.figma.com/embed\";\n\n/**\n * Legacy file URL prefix (for backward compatibility)\n * e.g. full url should look like -\n * https://www.figma.com/file/HSozKEVWhh8saZa2vr1Nxd?node-id=111%3A0\n */\nexport const _FIGMA_FILE_URL_PREFIX = \"https://www.figma.com/file\";\n\n/**\n * Design file URL prefix (current standard, replaces /file/)\n * e.g. full url should look like -\n * https://www.figma.com/design/HSozKEVWhh8saZa2vr1Nxd?node-id=111%3A0\n */\nexport const _FIGMA_DESIGN_URL_PREFIX = \"https://www.figma.com/design\";\n\n/**\n * FigJam board URL prefix\n * e.g. full url should look like -\n * https://www.figma.com/board/HSozKEVWhh8saZa2vr1Nxd?node-id=111%3A0\n */\nexport const _FIGMA_BOARD_URL_PREFIX = \"https://www.figma.com/board\";\n\n/**\n * Figma Slides URL prefix\n * e.g. full url should look like -\n * https://www.figma.com/slides/HSozKEVWhh8saZa2vr1Nxd?node-id=111%3A0\n */\nexport const _FIGMA_SLIDES_URL_PREFIX = \"https://www.figma.com/slides\";\n\n/**\n * Figma Site URL prefix\n * e.g. full url should look like -\n * https://www.figma.com/site/HSozKEVWhh8saZa2vr1Nxd?node-id=111%3A0\n */\nexport const _FIGMA_SITE_URL_PREFIX = \"https://www.figma.com/site\";\n\n/**\n * param key for node id specification.\n *\n * e.g. - `https://www.figma.com/file/ABC/?node-id=1234` `(node-id = 1234)`\n */\nexport const _PARAM_NODE_ID = \"node-id\";\n\n// ======================================================================================================\n// escape with __ for \"super-internal\" usage\n/**\n * file id of Grida design - https://www.figma.com/file/Y0Gh77AqBoHH7dG1GtK3xF/\n */\nexport const __FIGMA_DEMO_DEFAULT_FILE_ID = \"Y0Gh77AqBoHH7dG1GtK3xF\";\nexport const __FIGMA_DEMO_DEFAULT_FILE_URL =\n \"https://www.figma.com/file/Y0Gh77AqBoHH7dG1GtK3xF/\";\nexport const __FIGMA_DEMO_DEFAULT_FILE_NODE_URL =\n \"https://www.figma.com/file/Y0Gh77AqBoHH7dG1GtK3xF/?node-id=264%3A49\";\n// ======================================================================================================\n","import { FigmaTargetNodeConfig } from \"./target-node-config\";\n\n/**\n * extracts file id from share link\n *\n * Supports multiple Figma URL patterns:\n * - Old: \"https://www.figma.com/file/Y0Gh77AqBoHH7dG1GtK3xF/?node-id=775%3A112\"\n * - New: \"https://www.figma.com/design/Y0Gh77AqBoHH7dG1GtK3xF/?node-id=775%3A112\"\n * - New: \"https://www.figma.com/board/Y0Gh77AqBoHH7dG1GtK3xF/?node-id=775%3A112\"\n * - New: \"https://www.figma.com/slides/Y0Gh77AqBoHH7dG1GtK3xF/?node-id=775%3A112\"\n * - New: \"https://www.figma.com/site/Y0Gh77AqBoHH7dG1GtK3xF/?node-id=775%3A112\"\n *\n * out - \"Y0Gh77AqBoHH7dG1GtK3xF\"\n * @param url\n * @returns\n */\nexport function parseFileId(url: string) {\n // File ID is in the same position (4th segment) for all URL patterns\n const supportedPatterns = [\n \"https://www.figma.com/file/\",\n \"https://www.figma.com/design/\",\n \"https://www.figma.com/board/\",\n \"https://www.figma.com/slides/\",\n \"https://www.figma.com/site/\",\n ];\n\n const matchedPattern = supportedPatterns.find((pattern) =>\n url.includes(pattern)\n );\n\n if (matchedPattern) {\n return url.split(\"/\")[4];\n } else {\n throw `figma url must contain one of the supported patterns (file/, design/, board/, slides/, site/). the given was ${url}, which we cannot extract file id from it.`;\n }\n}\n\n/**\n * Figma file url's node-id changed over time, this function is to make sure we can handle both formats.\n * to keep `00:00` format\n * @param node_id\n * @returns\n */\nexport function formatNodeId(node_id: string) {\n if (\n node_id.includes(\":\") ||\n node_id.includes(\"%3A\") ||\n node_id.includes(\"-\")\n ) {\n // \"%3A\" is \":\" as in url encoding\n if (node_id.includes(\"%3A\")) {\n // decode value, assuming it is url encoded\n node_id = decodeURIComponent(node_id);\n }\n if (node_id.includes(\"-\")) {\n // if id is formatted with `-` instead of `:`, replace it.\n node_id = node_id.split(\"-\").join(\":\");\n }\n // 2. run regex\n if (node_id.match(/[0-9]+:[0-9]+/) !== null) {\n return node_id;\n }\n }\n}\n\n/**\n * pattern examples:\n * - \"https://www.figma.com/file/Y0Gh77AqBoHH7dG1GtK3xF/?node-id=775%3A112\"\n * - \"https://www.figma.com/design/Y0Gh77AqBoHH7dG1GtK3xF/?node-id=775%3A112\"\n * - \"https://www.figma.com/board/Y0Gh77AqBoHH7dG1GtK3xF/?node-id=775%3A112\"\n * - \"https://www.figma.com/slides/Y0Gh77AqBoHH7dG1GtK3xF/?node-id=775%3A112\"\n * - \"https://www.figma.com/site/Y0Gh77AqBoHH7dG1GtK3xF/?node-id=775%3A112\"\n * @param url\n */\nexport function parseFileAndNodeId(\n url: string\n): FigmaTargetNodeConfig | undefined {\n try {\n const _url = new URL(url);\n const params = new URLSearchParams(_url.search);\n const nodeId = formatNodeId(params.get(\"node-id\"));\n const fileId = parseFileId(url);\n return {\n url: url,\n file: fileId,\n node: nodeId,\n };\n } catch (_) {\n // empty url, invalud url\n return;\n }\n}\n","import {\n _FIGMA_EMBED_URL_PREFIX,\n _FIGMA_FILE_URL_PREFIX,\n _PARAM_NODE_ID,\n} from \"./constants\";\nimport { formatNodeId } from \"./parse-url\";\n\nexport enum FigmaUrlType {\n /**\n * embed ready url\n */\n embed = \"embed\",\n\n /**\n * file id only url. this cannot be used for embedding (cannot be used as a source)\n */\n file = \"file\",\n\n /**\n * url with file & node this can be used for embedding. (can be used as a source)\n */\n node = \"node\",\n\n /**\n * empty url. undefined or \"\"\n */\n empty = \"empty\",\n}\n\nexport enum FigmaFileOrNodeIdType {\n nodeid = \"nodeid\",\n maybe_nodeid = \"maybe_nodeid\",\n fileid = \"fileid\",\n maybe_fileid = \"maybe_fileid\",\n}\n\nexport type FigmaInputAnalysisResult = FigmaUrlType | FigmaFileOrNodeIdType;\n\nexport function analyze(url: string): FigmaInputAnalysisResult {\n if (!url) {\n // return if url is empty\n return FigmaUrlType.empty;\n }\n let _u: URL;\n\n // pre url validation\n try {\n _u = new URL(url);\n } catch (_) {\n const maybeidlike = url;\n if (maybeidlike.length > 0) {\n if (formatNodeId(maybeidlike)) {\n return FigmaFileOrNodeIdType.maybe_nodeid;\n }\n\n // e.g. kLzb7R9xYuuphfX4TssVNe\n // e.g. 4hqwYFw6FKw1njvzEl3VUh\n // fileid is 22 chars at this point.\n else if (maybeidlike.length >= 22) {\n const _taget = decodeURI(maybeidlike);\n // figma file id does not contain special characters. it's like mongodb id\n if (_taget.match(/[a-zA-Z0-9]/) !== null) {\n if (_taget.length == 22) {\n return FigmaFileOrNodeIdType.fileid;\n } else {\n return FigmaFileOrNodeIdType.maybe_fileid;\n }\n }\n }\n }\n\n throw `this url cannot be analyzed. this is not a valid url string - \"${url}\"`;\n }\n\n //\n if (url.startsWith(_FIGMA_EMBED_URL_PREFIX)) {\n return FigmaUrlType.embed;\n } else {\n // you might think the value of the embed target might be encoded,\n // so it is wrong to compare value with raw string,\n // but the only case the url is encoded is when it's for embeding\n // - which will be returned above. it's fine.\n if (_u.hostname == \"figma.com\" || _u.hostname == \"www.figma.com\") {\n // Support all Figma URL patterns: file/, design/, board/, slides/, site/\n const supportedPathPatterns = [\n \"file/\",\n \"design/\",\n \"board/\",\n \"slides/\",\n \"site/\",\n ];\n const matchedPattern = supportedPathPatterns.find((pattern) =>\n _u.pathname.includes(pattern)\n );\n\n if (matchedPattern) {\n if (_u.searchParams.get(_PARAM_NODE_ID)?.length > 0) {\n return FigmaUrlType.node;\n } else {\n return FigmaUrlType.file;\n }\n }\n }\n // otherwise,\n throw `not a valid figma url.`;\n }\n}\n","import { analyze, FigmaUrlType } from \"./analyze-url\";\nimport { _FIGMA_EMBED_URL_PREFIX } from \"./constants\";\n\nexport type FigmaEmbedInput =\n | string\n | { url: string }\n | { fileid: string; nodeid?: string };\n\n/**\n * build embedding url. - https://www.figma.com/developers/embed\n * @param src : ;\n * @returns\n */\nexport function embed(src: FigmaEmbedInput) {\n const url = builEmbedableSourceUrl(src);\n const urltype = analyze(url);\n switch (urltype) {\n case FigmaUrlType.embed:\n return url;\n case FigmaUrlType.file:\n console.warn(\n \"incomplete url input. the target node is not specified in the embedding target source url. this wont display contents as expected\"\n );\n return _build(url);\n case FigmaUrlType.node:\n return _build(url);\n case FigmaUrlType.empty:\n return;\n }\n}\n\nfunction _build(url: string) {\n const _embed_url = `${_FIGMA_EMBED_URL_PREFIX}?embed_host=astra&url=${url}`;\n return _embed_url;\n}\n\n/**\n * e.g. - https://www.figma.com/design/HSozKEVWhh8saZa2vr1Nxd?node-id=111%3A0\n *\n * (*this does not contain logic for checking if input url is valid.*)\n * @param src : ;\n * @returns\n */\nexport function builEmbedableSourceUrl(\n src?: FigmaEmbedInput\n): string | undefined {\n if (!src) {\n return;\n }\n\n if (typeof src == \"string\") {\n return src;\n } else if (\"url\" in src) {\n return src.url;\n } else if (\"fileid\" in src) {\n /// WWW prefix is required. if non passed, figma embed won't accept it.\n /// Use /design/ pattern for new URLs (replaces /file/)\n return `https://www.figma.com/design/${src.fileid}/${\n src.nodeid && `?node-id=${src.nodeid}`\n }`;\n } else {\n return undefined;\n }\n}\n","///\n/// simple functions to check if file is accessible by public.\n///\n\nimport { analyze, FigmaUrlType } from \"./analyze-url\";\n\n/**\n * e.g.\n *\n * - **ok**: (public file) - https://www.figma.com/file/Y0Gh77AqBoHH7dG1GtK3xF/grida?node-id=1545%3A247\n * - **not found**: (private file) - https://www.figma.com/file/8R57Uv5Siu1ZhhGkVIyyQg/private-file-api-demo?node-id=2%3A2\n *\n */\nexport async function isPublic(url: string): Promise<boolean> {\n const type = analyze(url);\n if (type == FigmaUrlType.embed || FigmaUrlType.file || FigmaUrlType.node) {\n try {\n const res = await fetch(url);\n return res.status == 200;\n } catch (e) {\n return false;\n }\n }\n return false;\n}\n","import { analyze, FigmaUrlType } from \"./analyze-url\";\nimport { parseFileAndNodeId } from \"./parse-url\";\n\nexport function isSameDesignUrl(url1: string, url2: string): boolean {\n const analysis = analyze(url1);\n switch (analysis) {\n case FigmaUrlType.embed:\n case FigmaUrlType.empty:\n case FigmaUrlType.file:\n return false;\n case FigmaUrlType.node:\n const parsed = parseFileAndNodeId(url1);\n const _this_parsed = parseFileAndNodeId(url2);\n return (\n _this_parsed.file === parsed.file && _this_parsed.node === parsed.node\n );\n }\n}\n"]}
package/package.json CHANGED
@@ -1,29 +1,33 @@
1
1
  {
2
- "name": "@design-sdk/figma-url",
3
- "version": "0.0.53",
4
- "description": "Figma url utils for sharing & embeding and url inspection",
5
- "license": "MIT",
6
- "private": false,
7
- "main": "dist/index.js",
8
- "types": "dist/index.d.ts",
9
- "homepage": "https://github.com/gridaco/design-sdk",
10
- "repository": "https://github.com/gridaco/design-sdk",
11
- "authors": [
12
- "grida.co",
13
- "softmarshmallow <universe@grida.co>"
14
- ],
15
- "scripts": {
16
- "clean": "rimraf dist",
17
- "build": "tsc",
18
- "test": "jest"
19
- },
20
- "files": [
21
- "dist",
22
- "README",
23
- "LICENSE"
24
- ],
25
- "publishConfig": {
26
- "access": "public"
27
- },
28
- "gitHead": "5784ab284f17fcb2b20bc821fc140fd004964d7e"
29
- }
2
+ "name": "@design-sdk/figma-url",
3
+ "version": "0.1.0",
4
+ "description": "Figma url utils for sharing & embeding and url inspection",
5
+ "license": "MIT",
6
+ "private": false,
7
+ "main": "dist/index.js",
8
+ "types": "dist/index.d.ts",
9
+ "homepage": "https://github.com/gridaco/figma-sdk",
10
+ "repository": "https://github.com/gridaco/figma-sdk",
11
+ "authors": [
12
+ "grida.co",
13
+ "softmarshmallow <universe@grida.co>"
14
+ ],
15
+ "files": [
16
+ "dist",
17
+ "README",
18
+ "LICENSE"
19
+ ],
20
+ "publishConfig": {
21
+ "access": "public"
22
+ },
23
+ "devDependencies": {
24
+ "@vitest/coverage-v8": "^4.0.16"
25
+ },
26
+ "scripts": {
27
+ "clean": "rimraf dist",
28
+ "dev": "tsup --watch",
29
+ "build": "tsup lib/index.ts",
30
+ "test": "vitest run",
31
+ "typecheck": "tsc --noEmit"
32
+ }
33
+ }
@@ -1,8 +0,0 @@
1
- /**
2
- * e.g.
3
- *
4
- * - **ok**: (public file) - https://www.figma.com/file/Y0Gh77AqBoHH7dG1GtK3xF/grida?node-id=1545%3A247
5
- * - **not found**: (private file) - https://www.figma.com/file/8R57Uv5Siu1ZhhGkVIyyQg/private-file-api-demo?node-id=2%3A2
6
- *
7
- */
8
- export declare function isPublic(url: string): Promise<boolean>;
@@ -1,39 +0,0 @@
1
- "use strict";
2
- ///
3
- /// simple functions to check if file is accessible by public.
4
- ///
5
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
6
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
7
- return new (P || (P = Promise))(function (resolve, reject) {
8
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
9
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
10
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
11
- step((generator = generator.apply(thisArg, _arguments || [])).next());
12
- });
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.isPublic = void 0;
16
- const analyze_url_1 = require("./analyze-url");
17
- /**
18
- * e.g.
19
- *
20
- * - **ok**: (public file) - https://www.figma.com/file/Y0Gh77AqBoHH7dG1GtK3xF/grida?node-id=1545%3A247
21
- * - **not found**: (private file) - https://www.figma.com/file/8R57Uv5Siu1ZhhGkVIyyQg/private-file-api-demo?node-id=2%3A2
22
- *
23
- */
24
- function isPublic(url) {
25
- return __awaiter(this, void 0, void 0, function* () {
26
- const type = (0, analyze_url_1.analyze)(url);
27
- if (type == analyze_url_1.FigmaUrlType.embed || analyze_url_1.FigmaUrlType.file || analyze_url_1.FigmaUrlType.node) {
28
- try {
29
- const res = yield fetch(url);
30
- return res.status == 200;
31
- }
32
- catch (e) {
33
- return false;
34
- }
35
- }
36
- return false;
37
- });
38
- }
39
- exports.isPublic = isPublic;
@@ -1,26 +0,0 @@
1
- export declare enum FigmaUrlType {
2
- /**
3
- * embed ready url
4
- */
5
- embed = "embed",
6
- /**
7
- * file id only url. this cannot be used for embedding (cannot be used as a source)
8
- */
9
- file = "file",
10
- /**
11
- * url with file & node this can be used for embedding. (can be used as a source)
12
- */
13
- node = "node",
14
- /**
15
- * empty url. undefined or ""
16
- */
17
- empty = "empty"
18
- }
19
- export declare enum FigmaFileOrNodeIdType {
20
- nodeid = "nodeid",
21
- maybe_nodeid = "maybe_nodeid",
22
- fileid = "fileid",
23
- maybe_fileid = "maybe_fileid"
24
- }
25
- export declare type FigmaInputAnalysisResult = FigmaUrlType | FigmaFileOrNodeIdType;
26
- export declare function analyze(url: string): FigmaInputAnalysisResult;
@@ -1,91 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.analyze = exports.FigmaFileOrNodeIdType = exports.FigmaUrlType = void 0;
4
- const constants_1 = require("./constants");
5
- const parse_url_1 = require("./parse-url");
6
- var FigmaUrlType;
7
- (function (FigmaUrlType) {
8
- /**
9
- * embed ready url
10
- */
11
- FigmaUrlType["embed"] = "embed";
12
- /**
13
- * file id only url. this cannot be used for embedding (cannot be used as a source)
14
- */
15
- FigmaUrlType["file"] = "file";
16
- /**
17
- * url with file & node this can be used for embedding. (can be used as a source)
18
- */
19
- FigmaUrlType["node"] = "node";
20
- /**
21
- * empty url. undefined or ""
22
- */
23
- FigmaUrlType["empty"] = "empty";
24
- })(FigmaUrlType = exports.FigmaUrlType || (exports.FigmaUrlType = {}));
25
- var FigmaFileOrNodeIdType;
26
- (function (FigmaFileOrNodeIdType) {
27
- FigmaFileOrNodeIdType["nodeid"] = "nodeid";
28
- FigmaFileOrNodeIdType["maybe_nodeid"] = "maybe_nodeid";
29
- FigmaFileOrNodeIdType["fileid"] = "fileid";
30
- FigmaFileOrNodeIdType["maybe_fileid"] = "maybe_fileid";
31
- })(FigmaFileOrNodeIdType = exports.FigmaFileOrNodeIdType || (exports.FigmaFileOrNodeIdType = {}));
32
- function analyze(url) {
33
- var _a;
34
- if (!url) {
35
- // return if url is empty
36
- return FigmaUrlType.empty;
37
- }
38
- let _u;
39
- // pre url validation
40
- try {
41
- _u = new URL(url);
42
- }
43
- catch (_) {
44
- const maybeidlike = url;
45
- if (maybeidlike.length > 0) {
46
- if ((0, parse_url_1.formatNodeId)(maybeidlike)) {
47
- return FigmaFileOrNodeIdType.maybe_nodeid;
48
- }
49
- // e.g. kLzb7R9xYuuphfX4TssVNe
50
- // e.g. 4hqwYFw6FKw1njvzEl3VUh
51
- // fileid is 22 chars at this point.
52
- else if (maybeidlike.length >= 22) {
53
- const _taget = decodeURI(maybeidlike);
54
- // figma file id does not contain special characters. it's like mongodb id
55
- if (_taget.match(/[a-zA-Z0-9]/) !== null) {
56
- if (_taget.length == 22) {
57
- return FigmaFileOrNodeIdType.fileid;
58
- }
59
- else {
60
- return FigmaFileOrNodeIdType.maybe_fileid;
61
- }
62
- }
63
- }
64
- }
65
- throw `this url cannot be analyzed. this is not a valid url string - "${url}"`;
66
- }
67
- //
68
- if (url.startsWith(constants_1._FIGMA_EMBED_URL_PREFIX)) {
69
- return FigmaUrlType.embed;
70
- }
71
- else {
72
- // you might think the value of the embed target might be encoded,
73
- // so it is wrong to compare value with raw string,
74
- // but the only case the url is encoded is when it's for embeding
75
- // - which will be returned above. it's fine.
76
- if (_u.hostname == "figma.com" || _u.hostname == "www.figma.com") {
77
- // file path is required for both file and node.
78
- if (_u.pathname.includes("file/")) {
79
- if (((_a = _u.searchParams.get(constants_1._PARAM_NODE_ID)) === null || _a === void 0 ? void 0 : _a.length) > 0) {
80
- return FigmaUrlType.node;
81
- }
82
- else {
83
- return FigmaUrlType.file;
84
- }
85
- }
86
- }
87
- // otherwise,
88
- throw `not a valid figma url.`;
89
- }
90
- }
91
- exports.analyze = analyze;
@@ -1 +0,0 @@
1
- export declare function isSameDesignUrl(url1: string, url2: string): boolean;
@@ -1,19 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isSameDesignUrl = void 0;
4
- const analyze_url_1 = require("./analyze-url");
5
- const parse_url_1 = require("./parse-url");
6
- function isSameDesignUrl(url1, url2) {
7
- const analysis = (0, analyze_url_1.analyze)(url1);
8
- switch (analysis) {
9
- case analyze_url_1.FigmaUrlType.embed:
10
- case analyze_url_1.FigmaUrlType.empty:
11
- case analyze_url_1.FigmaUrlType.file:
12
- return false;
13
- case analyze_url_1.FigmaUrlType.node:
14
- const parsed = (0, parse_url_1.parseFileAndNodeId)(url1);
15
- const _this_parsed = (0, parse_url_1.parseFileAndNodeId)(url2);
16
- return (_this_parsed.file === parsed.file && _this_parsed.node === parsed.node);
17
- }
18
- }
19
- exports.isSameDesignUrl = isSameDesignUrl;
@@ -1,24 +0,0 @@
1
- /**
2
- * e.g. full url should look like -
3
- * https://www.figma.com/embed?embed_host=astra&url=https://www.figma.com/file/HSozKEVWhh8saZa2vr1Nxd?node-id=111%3A0
4
- *
5
- * ref: https://www.figma.com/developers/embed
6
- */
7
- export declare const _FIGMA_EMBED_URL_PREFIX = "https://www.figma.com/embed";
8
- /**
9
- * e.g. full url should look like -
10
- * https://www.figma.com/file/HSozKEVWhh8saZa2vr1Nxd?node-id=111%3A0
11
- */
12
- export declare const _FIGMA_FILE_URL_PREFIX = "https://www.figma.com/file";
13
- /**
14
- * param key for node id specification.
15
- *
16
- * e.g. - `https://www.figma.com/file/ABC/?node-id=1234` `(node-id = 1234)`
17
- */
18
- export declare const _PARAM_NODE_ID = "node-id";
19
- /**
20
- * file id of Grida design - https://www.figma.com/file/Y0Gh77AqBoHH7dG1GtK3xF/
21
- */
22
- export declare const __FIGMA_DEMO_DEFAULT_FILE_ID = "Y0Gh77AqBoHH7dG1GtK3xF";
23
- export declare const __FIGMA_DEMO_DEFAULT_FILE_URL = "https://www.figma.com/file/Y0Gh77AqBoHH7dG1GtK3xF/";
24
- export declare const __FIGMA_DEMO_DEFAULT_FILE_NODE_URL = "https://www.figma.com/file/Y0Gh77AqBoHH7dG1GtK3xF/?node-id=264%3A49";
package/dist/constants.js DELETED
@@ -1,30 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.__FIGMA_DEMO_DEFAULT_FILE_NODE_URL = exports.__FIGMA_DEMO_DEFAULT_FILE_URL = exports.__FIGMA_DEMO_DEFAULT_FILE_ID = exports._PARAM_NODE_ID = exports._FIGMA_FILE_URL_PREFIX = exports._FIGMA_EMBED_URL_PREFIX = void 0;
4
- /**
5
- * e.g. full url should look like -
6
- * https://www.figma.com/embed?embed_host=astra&url=https://www.figma.com/file/HSozKEVWhh8saZa2vr1Nxd?node-id=111%3A0
7
- *
8
- * ref: https://www.figma.com/developers/embed
9
- */
10
- exports._FIGMA_EMBED_URL_PREFIX = "https://www.figma.com/embed";
11
- /**
12
- * e.g. full url should look like -
13
- * https://www.figma.com/file/HSozKEVWhh8saZa2vr1Nxd?node-id=111%3A0
14
- */
15
- exports._FIGMA_FILE_URL_PREFIX = "https://www.figma.com/file";
16
- /**
17
- * param key for node id specification.
18
- *
19
- * e.g. - `https://www.figma.com/file/ABC/?node-id=1234` `(node-id = 1234)`
20
- */
21
- exports._PARAM_NODE_ID = "node-id";
22
- // ======================================================================================================
23
- // escape with __ for "super-internal" usage
24
- /**
25
- * file id of Grida design - https://www.figma.com/file/Y0Gh77AqBoHH7dG1GtK3xF/
26
- */
27
- exports.__FIGMA_DEMO_DEFAULT_FILE_ID = "Y0Gh77AqBoHH7dG1GtK3xF";
28
- exports.__FIGMA_DEMO_DEFAULT_FILE_URL = "https://www.figma.com/file/Y0Gh77AqBoHH7dG1GtK3xF/";
29
- exports.__FIGMA_DEMO_DEFAULT_FILE_NODE_URL = "https://www.figma.com/file/Y0Gh77AqBoHH7dG1GtK3xF/?node-id=264%3A49";
30
- // ======================================================================================================
@@ -1,20 +0,0 @@
1
- export declare type FigmaEmbedInput = string | {
2
- url: string;
3
- } | {
4
- fileid: string;
5
- nodeid?: string;
6
- };
7
- /**
8
- * build embedding url. - https://www.figma.com/developers/embed
9
- * @param src : ;
10
- * @returns
11
- */
12
- export declare function embed(src: FigmaEmbedInput): string;
13
- /**
14
- * e.g. - https://www.figma.com/file/HSozKEVWhh8saZa2vr1Nxd?node-id=111%3A0
15
- *
16
- * (*this does not contain logic for checking if input url is valid.*)
17
- * @param src : ;
18
- * @returns
19
- */
20
- export declare function builEmbedableSourceUrl(src?: FigmaEmbedInput): string | undefined;
package/dist/embed-url.js DELETED
@@ -1,56 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.builEmbedableSourceUrl = exports.embed = void 0;
4
- const analyze_url_1 = require("./analyze-url");
5
- const constants_1 = require("./constants");
6
- /**
7
- * build embedding url. - https://www.figma.com/developers/embed
8
- * @param src : ;
9
- * @returns
10
- */
11
- function embed(src) {
12
- const url = builEmbedableSourceUrl(src);
13
- const urltype = (0, analyze_url_1.analyze)(url);
14
- switch (urltype) {
15
- case analyze_url_1.FigmaUrlType.embed:
16
- return url;
17
- case analyze_url_1.FigmaUrlType.file:
18
- console.warn("incomplete url input. the target node is not specified in the embedding target source url. this wont display contents as expected");
19
- return _build(url);
20
- case analyze_url_1.FigmaUrlType.node:
21
- return _build(url);
22
- case analyze_url_1.FigmaUrlType.empty:
23
- return;
24
- }
25
- }
26
- exports.embed = embed;
27
- function _build(url) {
28
- const _embed_url = `${constants_1._FIGMA_EMBED_URL_PREFIX}?embed_host=astra&url=${url}`;
29
- return _embed_url;
30
- }
31
- /**
32
- * e.g. - https://www.figma.com/file/HSozKEVWhh8saZa2vr1Nxd?node-id=111%3A0
33
- *
34
- * (*this does not contain logic for checking if input url is valid.*)
35
- * @param src : ;
36
- * @returns
37
- */
38
- function builEmbedableSourceUrl(src) {
39
- if (!src) {
40
- return;
41
- }
42
- if (typeof src == "string") {
43
- return src;
44
- }
45
- else if ("url" in src) {
46
- return src.url;
47
- }
48
- else if ("fileid" in src) {
49
- /// WWW prefix is required. if non passed, figma embed won't accept it.
50
- return `https://www.figma.com/file/${src.fileid}/${src.nodeid && `?node-id=${src.nodeid}`}`;
51
- }
52
- else {
53
- return undefined;
54
- }
55
- }
56
- exports.builEmbedableSourceUrl = builEmbedableSourceUrl;
@@ -1,23 +0,0 @@
1
- import { FigmaTargetNodeConfig } from "./target-node-config";
2
- /**
3
- * extracts file id from share link
4
- *
5
- * e.g. in - "https://www.figma.com/file/Y0Gh77AqBoHH7dG1GtK3xF/?node-id=775%3A112"
6
- *
7
- * out - "Y0Gh77AqBoHH7dG1GtK3xF"
8
- * @param url
9
- * @returns
10
- */
11
- export declare function parseFileId(url: string): string;
12
- /**
13
- * Figma file url's node-id changed over time, this function is to make sure we can handle both formats.
14
- * to keep `00:00` format
15
- * @param node_id
16
- * @returns
17
- */
18
- export declare function formatNodeId(node_id: string): string;
19
- /**
20
- * pattern is "https://www.figma.com/file/Y0Gh77AqBoHH7dG1GtK3xF/?node-id=775%3A112"
21
- * @param url
22
- */
23
- export declare function parseFileAndNodeId(url: string): FigmaTargetNodeConfig | undefined;