Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/core/src/figma/parseFigmaRef.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ describe("parseFigmaRef", () => {
expect(ref.nodeId).toBeUndefined();
});

it("uses the branch key of a branch URL, which is the key the REST API serves the branch under", () => {
expect(
parseFigmaRef("https://www.figma.com/design/MainKey123/branch/BranchKey456/Name?node-id=1-2"),
).toEqual({ fileKey: "BranchKey456", nodeId: "1:2" });
expect(parseFigmaRef("https://www.figma.com/file/MainKey123/branch/BranchKey456")).toEqual({
fileKey: "BranchKey456",
});
});

it("does not read a file named branch as a branch key", () => {
expect(parseFigmaRef("https://www.figma.com/design/MainKey123/branch")).toEqual({
fileKey: "MainKey123",
});
});

it("accepts fileKey:nodeId shorthand", () => {
expect(parseFigmaRef("ABC123:1-2")).toEqual({ fileKey: "ABC123", nodeId: "1:2" });
});
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/figma/parseFigmaRef.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { FigmaRef } from "./types";

const FILE_KEY_RE = /\/(?:design|file|proto)\/([A-Za-z0-9]+)/;
const FILE_KEY_RE = /\/(?:design|file|proto)\/([A-Za-z0-9]+)(?:\/branch\/([A-Za-z0-9]+))?/;

function normalizeNodeId(raw: string): string {
return raw.replaceAll("-", ":");
Expand All @@ -20,7 +20,7 @@ export function parseFigmaRef(input: string): FigmaRef {
}

const keyMatch = trimmed.match(FILE_KEY_RE);
const fileKey = keyMatch?.[1];
const fileKey = keyMatch?.[2] ?? keyMatch?.[1];
if (fileKey === undefined) throw new Error(`parseFigmaRef: no fileKey in "${input}"`);

const q = trimmed.indexOf("?");
Expand Down
Loading