miro-export 1.0.1 → 1.1.1

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.
@@ -2,7 +2,7 @@ name: Build
2
2
 
3
3
  on:
4
4
  push:
5
- pull_request:
5
+ workflow_dispatch:
6
6
 
7
7
  jobs:
8
8
  build:
@@ -14,7 +14,7 @@ jobs:
14
14
  version: 8
15
15
  - uses: actions/setup-node@v4
16
16
  with:
17
- node-version: "20.x"
17
+ node-version: "22.x"
18
18
  - name: Install dependencies
19
19
  run: pnpm i --frozen-lockfile
20
20
  - name: Lint
@@ -1,19 +1,14 @@
1
1
  name: Dependabot auto-merge
2
2
  on: pull_request
3
3
 
4
- permissions:
5
- pull-requests: write
6
-
7
4
  jobs:
8
5
  dependabot:
9
6
  runs-on: ubuntu-latest
7
+ permissions:
8
+ pull-requests: write
9
+ contents: write
10
10
  if: github.actor == 'dependabot[bot]'
11
11
  steps:
12
- - name: Dependabot metadata
13
- id: metadata
14
- uses: dependabot/fetch-metadata@v1
15
- with:
16
- github-token: "${{ secrets.GITHUB_TOKEN }}"
17
12
  - name: Merge a PR
18
13
  run: gh pr merge --auto --merge "$PR_URL"
19
14
  env:
@@ -14,7 +14,7 @@ jobs:
14
14
  version: 8
15
15
  - uses: actions/setup-node@v4
16
16
  with:
17
- node-version: "20.x"
17
+ node-version: "22.x"
18
18
  registry-url: "https://registry.npmjs.org"
19
19
  - run: pnpm install --frozen-lockfile
20
20
  - run: pnpm run build
@@ -0,0 +1,23 @@
1
+ name: Test
2
+
3
+ on:
4
+ push:
5
+ workflow_dispatch:
6
+
7
+ jobs:
8
+ test:
9
+ runs-on: ubuntu-latest
10
+ steps:
11
+ - uses: actions/checkout@v4
12
+ - uses: pnpm/action-setup@v3
13
+ with:
14
+ version: 8
15
+ - uses: actions/setup-node@v4
16
+ with:
17
+ node-version: "22.x"
18
+ - name: Install dependencies
19
+ run: pnpm i --frozen-lockfile
20
+ - name: Test
21
+ run: pnpm run test
22
+ env:
23
+ TEST_BOARD_ID: ${{secrets.TEST_BOARD_ID}}
@@ -0,0 +1,3 @@
1
+ {
2
+ "typescript.tsdk": "node_modules/typescript/lib"
3
+ }
package/README.md CHANGED
@@ -1,12 +1,29 @@
1
1
  # Miro board exporter
2
2
 
3
- Exports Miro frames as full-detail SVGs or JSON using a headless Puppeteer browser. Requires a personal Miro token.
3
+ Exports Miro frames as full-detail SVGs or JSON using a headless Puppeteer browser.
4
4
 
5
- ## Getting the Miro token
5
+ - [Authentication](#authentication)
6
+ - [CLI](#cli)
7
+ - [Programmatic usage](#programmatic-usage)
6
8
 
7
- Log in to Miro using your regular web browser, and then copy the value of the "token" cookie from developer tools. This is the token that this tool requires.
9
+ ## Authentication
8
10
 
9
- ## Usage
11
+ If accessing a private board, a personal token is required. To get a token, log in to Miro using a regular web browser, and then copy the value of the "token" cookie from developer tools. This is the token that should be used. If the board can be accessed without an account using a public link, the token is optional.
12
+
13
+ ## CLI
14
+
15
+ You can use this tool as a command-line tool.
16
+
17
+ ### Prerequisites
18
+
19
+ - [Node.js >=22](https://nodejs.org/en/download)
20
+ - npm (built-in to Node.js), yarn, or pnpm
21
+
22
+ ### Installation
23
+
24
+ The CLI can be ran using [npx](https://docs.npmjs.com/cli/v8/commands/npx) with `npx miro-export [options]` (see options below). Alternatively, it's possible to install the package to the global scope with, for example, `npm i -g miro-export`.
25
+
26
+ ### Usage
10
27
 
11
28
  ```
12
29
  Options:
@@ -18,12 +35,15 @@ Options:
18
35
  -h, --help display help for command
19
36
  ```
20
37
 
21
- ## Examples
38
+ ### Examples
22
39
 
23
40
  ```sh
24
41
  # export "Frame 2" to the file "My Frame 2.svg"
25
42
  miro-export -t XYZ -b uMoVLkx8gIc= -f "Frame 2" -o "My Frame 2.svg"
26
43
 
44
+ # using npx
45
+ npx miro-export -t XYZ -b uMoVLkx8gIc= -f "Frame 2" -o "My Frame 2.svg"
46
+
27
47
  # export entire board to stdout
28
48
  miro-export -t XYZ -b uMoVLkx8gIc=
29
49
 
@@ -34,13 +54,13 @@ miro-export -t XYZ -b uMoVLkx8gIc= -f "Frame 2" "Frame 3" -o "{frameName}.svg"
34
54
  miro-export -t XYZ -b uMoVLkx8gIc= -f "Frame 2" -e json
35
55
  ```
36
56
 
37
- ## Capturing multiple frames at once
57
+ ### Capturing multiple frames at once
38
58
 
39
- It is possible to supply multiple frames to the `-f` switch, e.g., `-f "Frame 2" "Frame 3"`. However, for SVG export, this will capture all content that is within the outer bounding box when all frames have been selected, so content between the frames will be captured as well. If you want separate SVGs for each frame, use the output file switch with `{frameName}` in the file name, e.g., `-o "Export - {frameName}.svg"`. It is not possible to export separate SVGs without the output file specified (i.e., to stdout).
59
+ It is possible to give multiple frames to the `-f` switch, e.g., `-f "Frame 2" "Frame 3"`. However, for SVG export, this will capture all content that is within the outer bounding box when all frames have been selected, so content between the frames will be captured as well. If you want separate SVGs for each frame (and thus avoiding capturing content in between), use the output file switch with `{frameName}` in the file name, e.g., `-o "Export - {frameName}.svg"`. It is not possible to export separate SVGs without the output file specified (i.e., to stdout).
40
60
 
41
- ## JSON export
61
+ ### JSON export
42
62
 
43
- The JSON export format is a Miro-internal representation of all the board objects. It is not a documented format, but it is quite easy to understand. The exported format is always an array of objects that have the field `type` as a discriminator. Depending on the type, fields change, but there is always at least an `id` field. For example, a `sticky_note` object could look like this:
63
+ The JSON export format is a Miro-internal representation of all the board objects. It is not a documented format, but it is quite easy to understand. The exported format is always an array of objects that have the field `type` as a discriminator. Depending on the type, fields change. Some of the types have been documented as TypeScript interfaces at [miro-types.ts](src/miro-types.ts). For example, a `sticky_note` object could look like this:
44
64
 
45
65
  ```json
46
66
  {
@@ -68,3 +88,35 @@ The JSON export format is a Miro-internal representation of all the board object
68
88
  "height": 125.12
69
89
  }
70
90
  ```
91
+
92
+ ## Programmatic usage
93
+
94
+ ```ts
95
+ import { MiroBoard } from "miro-export";
96
+
97
+ await using miroBoard = new MiroBoard({
98
+ boardId: "uMoVLkx8gIc=", // required
99
+ token: "..." // optional
100
+ });
101
+
102
+ // get all board objects of type frame and with title "Frame 1"
103
+ const framesWithTitleFrame1 = await miroBoard.getBoardObjects(
104
+ { type: "frame" }, // required (but empty object is OK too), limited field support
105
+ { title: "Frame 1" } // optional additional filters
106
+ );
107
+
108
+ // get SVG of the first frame found above
109
+ const svgOfFrame1 = await miroBoard.getSvg([framesWithTitleFrame1[0].id]);
110
+
111
+ // if you can't use "await using" for disposal, you can also dispose manually:
112
+ // await miroBoard.dispose()
113
+ // this can also be used to close the browser at the middle of the current scope
114
+ ```
115
+
116
+ > [!WARNING]
117
+ > Remember to dispose the instance to make sure the browser is closed and the process
118
+ > can exit. `await using` (as shown above) does this automatically, but is not supported
119
+ > in all environments and may not be the optimal choise in every case. Alternatively,
120
+ > `miroBoard.dispose()` may be called at any time to dispose of the instance manually.
121
+
122
+ Types for many of the common board object types has been provided in [miro-types.ts](src/miro-types.ts).
package/build/cli.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
package/build/cli.js ADDED
@@ -0,0 +1,122 @@
1
+ #!/usr/bin/env node
2
+ var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
3
+ if (value !== null && value !== void 0) {
4
+ if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
5
+ var dispose, inner;
6
+ if (async) {
7
+ if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
8
+ dispose = value[Symbol.asyncDispose];
9
+ }
10
+ if (dispose === void 0) {
11
+ if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
12
+ dispose = value[Symbol.dispose];
13
+ if (async) inner = dispose;
14
+ }
15
+ if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
16
+ if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
17
+ env.stack.push({ value: value, dispose: dispose, async: async });
18
+ }
19
+ else if (async) {
20
+ env.stack.push({ async: true });
21
+ }
22
+ return value;
23
+ };
24
+ var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
25
+ return function (env) {
26
+ function fail(e) {
27
+ env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
28
+ env.hasError = true;
29
+ }
30
+ var r, s = 0;
31
+ function next() {
32
+ while (r = env.stack.pop()) {
33
+ try {
34
+ if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
35
+ if (r.dispose) {
36
+ var result = r.dispose.call(r.value);
37
+ if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
38
+ }
39
+ else s |= 1;
40
+ }
41
+ catch (e) {
42
+ fail(e);
43
+ }
44
+ }
45
+ if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
46
+ if (env.hasError) throw env.error;
47
+ }
48
+ return next();
49
+ };
50
+ })(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
51
+ var e = new Error(message);
52
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
53
+ });
54
+ import { writeFile } from "fs/promises";
55
+ import { program } from "@commander-js/extra-typings";
56
+ import { MiroBoard } from "./index.js";
57
+ const { token, boardId, frameNames, outputFile, exportFormat } = program
58
+ .option("-t, --token <token>", "Miro token")
59
+ .requiredOption("-b, --board-id <boardId>", "The board ID")
60
+ .option("-f, --frame-names <frameNames...>", "The frame name(s), leave empty to export entire board")
61
+ .option("-o, --output-file <filename>", "A file to output the SVG to (stdout if not supplied)")
62
+ .option("-e, --export-format <format>", "'svg' or 'json' (default: 'svg')")
63
+ .parse()
64
+ .opts();
65
+ (async () => {
66
+ const env_1 = { stack: [], error: void 0, hasError: false };
67
+ try {
68
+ const miroBoard = __addDisposableResource(env_1, new MiroBoard({ token, boardId }), true);
69
+ async function getFrames(frameNames) {
70
+ const frames = await miroBoard.getBoardObjects({ type: "frame" }, { title: frameNames });
71
+ if (frames && frames.length !== frameNames.length) {
72
+ throw Error(`${frameNames.length - frames.length} frame(s) could not be found on the board.`);
73
+ }
74
+ return frames;
75
+ }
76
+ async function getSvg(frames) {
77
+ return await miroBoard.getSvg(frames?.map(({ id }) => id).filter((id) => !!id));
78
+ }
79
+ async function getJson(frames) {
80
+ if (frames) {
81
+ const frameChildren = await miroBoard.getBoardObjects({
82
+ id: frames.flatMap((frame) => frame.childrenIds)
83
+ });
84
+ const groupChildren = await miroBoard.getBoardObjects({
85
+ id: frameChildren
86
+ .filter((child) => child.type === "group")
87
+ .flatMap((child) => child.itemsIds)
88
+ });
89
+ return JSON.stringify([...frames, ...frameChildren, ...groupChildren]);
90
+ }
91
+ return JSON.stringify(await miroBoard.getBoardObjects({}));
92
+ }
93
+ const getFn = exportFormat === "json" ? getJson : getSvg;
94
+ if (outputFile?.includes("{frameName}")) {
95
+ if (!frameNames) {
96
+ throw Error("Expected frame names to be given when the output file name format expects a frame name.");
97
+ }
98
+ for (const frameName of frameNames) {
99
+ const output = await getFn(await getFrames([frameName]));
100
+ await writeFile(outputFile.replace("{frameName}", frameName), output);
101
+ }
102
+ }
103
+ else {
104
+ const svg = await getFn(frameNames && (await getFrames(frameNames)));
105
+ if (outputFile) {
106
+ await writeFile(outputFile, svg);
107
+ }
108
+ else {
109
+ process.stdout.write(svg);
110
+ }
111
+ }
112
+ }
113
+ catch (e_1) {
114
+ env_1.error = e_1;
115
+ env_1.hasError = true;
116
+ }
117
+ finally {
118
+ const result_1 = __disposeResources(env_1);
119
+ if (result_1)
120
+ await result_1;
121
+ }
122
+ })();
@@ -0,0 +1,24 @@
1
+ import type { BoardObject } from "./miro-types.ts";
2
+ import type { GetBoardsFilter } from "./miro-runtime.ts";
3
+ interface InitialMiroBoardOptions {
4
+ token?: string;
5
+ boardId: string;
6
+ }
7
+ type AdditionalFilter<T> = Partial<T> | Partial<{
8
+ [K in keyof T]: T[K][];
9
+ }>;
10
+ type FilteredResultsByType<F extends string | string[] | undefined, T> = F extends string ? Extract<BoardObject, {
11
+ type: F;
12
+ }>[] : T[];
13
+ export declare class MiroBoard {
14
+ private context;
15
+ constructor(options: InitialMiroBoardOptions);
16
+ private initialize;
17
+ dispose(): Promise<void>;
18
+ [Symbol.asyncDispose](): Promise<void>;
19
+ private get browser();
20
+ private get page();
21
+ getBoardObjects<F extends GetBoardsFilter>(filter: F, additionalFilter?: AdditionalFilter<BoardObject>): Promise<FilteredResultsByType<F["type"], BoardObject>>;
22
+ getSvg(objectsIds?: string[]): Promise<string>;
23
+ }
24
+ export {};
package/build/index.js CHANGED
@@ -1,97 +1,94 @@
1
- #!/usr/bin/env node
2
- "use strict";
3
- var __importDefault = (this && this.__importDefault) || function (mod) {
4
- return (mod && mod.__esModule) ? mod : { "default": mod };
5
- };
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- const promises_1 = require("fs/promises");
8
- const puppeteer_1 = __importDefault(require("puppeteer"));
9
- const extra_typings_1 = require("@commander-js/extra-typings");
10
- const { token, boardId, frameNames, outputFile, exportFormat } = extra_typings_1.program
11
- .requiredOption("-t, --token <token>", "Miro token")
12
- .requiredOption("-b, --board-id <boardId>", "The board ID")
13
- .option("-f, --frame-names <frameNames...>", "The frame name(s), leave empty to export entire board")
14
- .option("-o, --output-file <filename>", "A file to output the SVG to (stdout if not supplied)")
15
- .option("-e, --export-format <format>", "'svg' or 'json' (default: 'svg')")
16
- .parse()
17
- .opts();
18
- (async () => {
19
- const browser = await puppeteer_1.default.launch({ headless: true });
20
- const page = await browser.newPage();
21
- await page.setCookie({
22
- name: "token",
23
- value: token,
24
- domain: "miro.com"
25
- });
26
- await page.setViewport({ width: 1080, height: 1024 });
27
- await page.goto(`https://miro.com/app/board/${boardId}/`, {
28
- waitUntil: "domcontentloaded"
29
- });
30
- await page.evaluate(() => new Promise((resolve) => {
31
- const interval = setInterval(() => {
32
- try {
33
- if (typeof window.miro?.board !== "undefined") {
34
- resolve();
35
- clearInterval(interval);
36
- }
37
- }
38
- catch (e) {
39
- // ignored
40
- }
41
- }, 100);
42
- }));
43
- const getSvgForFrames = (frameNames) => page.evaluate(async (frameNames) => {
44
- if (frameNames) {
45
- const frames = await window.miro.board.get({ type: ["frame"] });
46
- const selectedFrames = frames.filter((frame) => frameNames.includes(frame.title));
47
- if (selectedFrames.length !== frameNames.length) {
48
- throw Error(`${frameNames.length - selectedFrames.length} frame(s) could not be found on the board.`);
49
- }
50
- await window.miro.board.deselect();
51
- for (const { id } of selectedFrames) {
52
- await window.miro.board.select({ id });
53
- }
1
+ import puppeteer, { Browser, Page } from "puppeteer";
2
+ export class MiroBoard {
3
+ context = Promise.withResolvers();
4
+ constructor(options) {
5
+ this.initialize(options);
6
+ }
7
+ async initialize(options) {
8
+ const browser = await puppeteer.launch({ headless: true });
9
+ const page = await browser.newPage();
10
+ if (options.token) {
11
+ await browser.browserContexts()[0].setCookie({
12
+ name: "token",
13
+ value: options.token,
14
+ domain: "miro.com",
15
+ path: "/",
16
+ expires: -1,
17
+ sameParty: false,
18
+ httpOnly: false,
19
+ secure: false
20
+ });
54
21
  }
55
- return await window.cmd.board.api.export.makeVector();
56
- }, frameNames);
57
- const getJsonForFrames = (frameNames) => page.evaluate(async (frameNames) => {
58
- if (frameNames) {
59
- const frames = await window.miro.board.get({ type: ["frame"] });
60
- const selectedFrames = frames.filter((frame) => frameNames.includes(frame.title));
61
- if (selectedFrames.length !== frameNames.length) {
62
- throw Error(`${frameNames.length - selectedFrames.length} frame(s) could not be found on the board.`);
22
+ await page.setViewport({ width: 1080, height: 1024 });
23
+ await page.goto(`https://miro.com/app/board/${options.boardId}/`, {
24
+ waitUntil: "domcontentloaded"
25
+ });
26
+ await page.evaluate(() => new Promise((resolve) => {
27
+ if (window.miro) {
28
+ resolve();
63
29
  }
64
- const children = await window.miro.board.get({
65
- id: selectedFrames.flatMap((frame) => frame.childrenIds)
66
- });
67
- const groupChildren = await window.miro.board.get({
68
- id: children
69
- .filter((child) => child.type === "group")
70
- .flatMap((child) => child.itemsIds)
30
+ let miroValue;
31
+ Object.defineProperty(window, "miro", {
32
+ get() {
33
+ return miroValue;
34
+ },
35
+ set(value) {
36
+ miroValue = value;
37
+ resolve();
38
+ }
71
39
  });
72
- return JSON.stringify([...frames, ...children, ...groupChildren]);
73
- }
74
- return JSON.stringify(await window.miro.board.get({}));
75
- }, frameNames);
76
- const getFn = exportFormat === "json" ? getJsonForFrames : getSvgForFrames;
77
- if (outputFile?.includes("{frameName}")) {
78
- if (!frameNames) {
79
- throw Error("Expected frame names to be given when the output file name format expects a frame name.");
80
- }
81
- for (const frameName of frameNames) {
82
- const svg = await getFn([frameName]);
83
- await (0, promises_1.writeFile)(outputFile.replace("{frameName}", frameName), svg);
84
- }
40
+ }));
41
+ this.context.resolve({ browser, page });
85
42
  }
86
- else {
87
- const svg = await getFn(frameNames);
88
- if (outputFile) {
89
- await (0, promises_1.writeFile)(outputFile, svg);
90
- }
91
- else {
92
- process.stdout.write(svg);
93
- }
43
+ async dispose() {
44
+ await (await this.browser).close();
45
+ }
46
+ async [Symbol.asyncDispose]() {
47
+ await this.dispose();
48
+ }
49
+ get browser() {
50
+ return this.context.promise.then(({ browser }) => browser);
51
+ }
52
+ get page() {
53
+ return this.context.promise.then(({ page }) => page);
54
+ }
55
+ async getBoardObjects(filter, additionalFilter) {
56
+ return (await this.page).evaluate(async (filter, additionalFilter) => {
57
+ // @ts-expect-error - https://github.com/evanw/esbuild/issues/2605#issuecomment-2050808084
58
+ window.__name = (func) => func;
59
+ const objectFilterMatches = (filter, target) => {
60
+ for (const key in filter) {
61
+ if (!(key in target)) {
62
+ return false;
63
+ }
64
+ const targetValue = target[key];
65
+ if (Array.isArray(filter[key])) {
66
+ if (!filter[key].includes(targetValue)) {
67
+ return false;
68
+ }
69
+ }
70
+ else if (targetValue !== filter[key]) {
71
+ return false;
72
+ }
73
+ }
74
+ return true;
75
+ };
76
+ const objects = await window.miro.board.get(filter);
77
+ const filteredObjects = additionalFilter
78
+ ? objects.filter((object) => objectFilterMatches(additionalFilter, object))
79
+ : objects;
80
+ return filteredObjects;
81
+ }, filter, additionalFilter);
82
+ }
83
+ async getSvg(objectsIds) {
84
+ return (await this.page).evaluate(async (objectsIds) => {
85
+ await window.miro.board.deselect();
86
+ if (objectsIds) {
87
+ for (const id of objectsIds) {
88
+ await window.miro.board.select({ id });
89
+ }
90
+ }
91
+ return await window.cmd.board.api.export.makeVector();
92
+ }, objectsIds);
94
93
  }
95
- await page.close();
96
- await browser.close();
97
- })();
94
+ }