Skip to content

About

Move Notion-hosted images to your PicGo image host and free Notion file storage

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

picgo-plugin-notion-migrator

中文说明

Move images stored in Notion to the image host you use in PicGo, and free Notion file storage.

Notion's Free plan limits a workspace to 512 MB of uploaded files. Images that are embedded by URL don't count, and per Notion's help neither do files you replaced or files that only remain in version history. This plugin finds image blocks whose file is hosted by Notion, downloads each one, uploads it through PicGo, and switches the same block to the new URL. The block keeps its id, position, caption and comments, so links to the block still work.

Requirements

  • PicGo 3.0.0 or later (GUI), or PicGo-Core 3.0.0 or later (CLI), on Node.js 20.19 or later.
  • An image host that already works in PicGo. Everything you configured runs as usual: the uploader, compression or watermark plugins, rename and URL rewrite rules.
  • A Notion token with read and update access to the pages you want to migrate.

Install

GUI: open PicGo → Plugins, search for notion-migrator and install it.

CLI:

picgo install notion-migrator

Get a Notion token

Either kind of token works; the plugin only sends it as a Bearer token.

Personal access token (recommended). It acts as you, so it sees every page you can see and needs no sharing.

  1. Open Personal access tokens in the Notion developer portal and select New token.
  2. Name it, give it the Notion API capability, pick the workspace and an expiration.
  3. Select Create token and copy the value. Notion shows it only once.

Internal connection. It acts as a separate bot and only sees pages you share with it. You need to be a workspace owner to create one.

  1. In the developer portal, select New connection, choose API token as the authentication method and create it.
  2. On the Configuration tab, copy the API token and make sure the connection can read and update content.
  3. Share pages with it: either Content access → Edit access in the developer portal, or on a page ••• → Connections → Add connection. Sharing a page also shares its child pages.

Configure

GUI: Plugins → notion-migrator → Config plugin. CLI: picgo set plugin notion-migrator asks for each field, and picgo notion config set sets them without prompts.

Field Meaning
Notion token The token from above. Stored in PicGo's config file. For the CLI you can set NOTION_TOKEN instead; it wins over the saved value.
Pages Optional. Page or database URLs or ids, separated by commas, spaces or new lines. Each one is scanned with everything under it. Empty means every page that Notion's search returns for the token.
Include Optional regular expression on the file name. Only matching images are migrated.
Exclude Optional regular expression on the file name. Matching images are skipped.

Page URLs can be copied from the browser or from Share → Copy link. Ids with or without dashes work, and so do database URLs opened in peek mode (?p=).

Config without prompts

For scripts and AI agents, where an interactive prompt would hang.

To hand over the token without typing it into a command, write it into a file with any editor, for example ~/.picgo/notion.env:

NOTION_TOKEN=ntn_xxx

Then save it and the other fields:

picgo notion config set --token-file ~/.picgo/notion.env
picgo notion config set --pages "<url-or-id>, <url-or-id>" --exclude "\.gif$"
picgo notion config set --include ""   # an empty value clears a field

picgo notion config            # show the config, token masked
picgo notion config --json     # the same as one JSON object
picgo notion config --check    # try the token with one request to Notion

config set options: --token-file <path>, --token-stdin, --token <token>, --pages <list>, --include <regex>, --exclude <regex>. It saves only the fields you pass, in the same place as the prompt and the GUI. Values are checked like the plugin checks its config, and if any is invalid nothing is saved and it exits with 1.

--token-file accepts a dotenv-style file with a NOTION_TOKEN= line (blank lines, # comments, an export prefix and quotes are fine) or a file that holds only the token; a leading ~ means your home folder. --token-stdin reads everything piped in, such as pbpaste | picgo notion config set --token-stdin on macOS, and in a terminal asks with hidden input instead. Use one of these, the prompt or the GUI rather than --token <token>, which leaves the token in shell history and the process list. Only one token option is allowed per call. If NOTION_TOKEN is set in the environment, it wins over the saved token, and config set warns about it.

config shows the token as its last 4 characters (ntn_…ab12) and where it comes from, the configured pages as ids, and the include and exclude patterns. --check calls GET /v1/users/me and prints the integration and workspace name, or the error, such as 401 for an invalid token; it exits with 1 when the check fails or no token is set.

Use

CLI

# List Notion-hosted images; nothing is changed
picgo notion scan
picgo notion scan --page https://www.notion.so/My-Page-0123456789abcdef0123456789abcdef --size
picgo notion scan --json > images.json
picgo notion scan --out images.json

# See what would happen, then migrate a few, then everything
picgo notion migrate --dry-run
picgo notion migrate --limit 5
picgo notion migrate
picgo notion migrate --page <url-or-id> --yes

scan options: --page <url-or-id...> (overrides the configured pages), --scan-mode <markdown|blocks> (see Scan modes), --json (print JSON instead of text), --out <file> (also write JSON to a file), --size (ask Notion's storage for each file size, one extra request per image).

migrate options: --page <url-or-id...>, --scan-mode <markdown|blocks>, --dry-run, --limit <n>, --yes (skip the confirmation; required when not in a terminal), --batch-size <n> (images per PicGo upload, default 5).

Press Ctrl+C once to stop after the current image, twice to quit immediately.

GUI

In the plugin's menu:

  • Scan shows how many Notion-hosted images there are, per page.
  • Migrate all scans the configured pages (or everything) and asks before changing anything.
  • Migrate a page asks for one page URL or id.
  • Stop stops the current run after the image in progress.

Progress shows as a notification at every 10%, and a final notification has the counts. If Rename before upload is on in PicGo settings, the plugin warns first, because PicGo would open a rename window for every image.

How it works

  1. Scan. Starting from the configured pages (or from Notion search), the plugin reads every page, including nested blocks such as toggles, columns, callouts, lists, tables and tabs. It follows child pages and inline databases, reading every row. Synced block copies are skipped, because their content lives in the original block. Trashed blocks and images that already use a URL are skipped.
  2. For each image, in batches: read the block again, download it to a temporary file named after the block id, upload the batch with PicGo, then update the block with PATCH /v1/blocks/{id} to point at the new URL. Reading the block right before the update gets a fresh download link (Notion's links expire after an hour) and checks that it is still an image holding the same Notion-hosted file, so an image you replaced or deleted after the scan is followed or skipped instead of overwritten.
  3. Every upload and every block update is appended to a journal. Run the same command again to resume: finished blocks are skipped, and blocks that were uploaded but not yet updated are only updated, without uploading again.

Files the plugin writes, under PicGo's config folder (~/.picgo by default):

  • notion-migrator/journal.jsonl: the journal, one JSON line per event.
  • notion-migrator/reports/*.json: one report per migrate run, listing each image with its new URL or the reason it failed or was skipped.

Temporary downloads go to the system temp folder and are deleted at the end of each run.

Scan modes

  • markdown (default) reads each page with one request, GET /v1/pages/{id}/markdown, which returns the whole page with nested blocks inlined. Each Notion-hosted image carries its block id in the link, so no block has to be read one by one. Pages over about 20,000 blocks come back truncated; the missing parts are fetched by id, one request each. If Notion refuses a page there (400 or 403), that page is read block by block instead, and after three refusals in a row with no success the rest of the scan switches to blocks.
  • blocks walks GET /v1/blocks/{id}/children: one request per page and per block that has children (each toggle, column, callout, list item with sub-items...), plus one per 100 children. Use it if the markdown endpoint misbehaves for your workspace.

Both modes find the same images. Blocks Markdown can't show, such as template buttons, are read with the block API in either mode.

Request cost

Step Requests
Each configured page 1 to read its title
Each page, markdown mode 1, plus 1 per truncated part of a page over about 20,000 blocks
Each page, blocks mode 1 per block with children, plus 1 per 100 children
Each database 1, plus 1 per 100 rows
Search, when no pages are configured 1 per 100 results
Each migrated image 2: read the block, then update it
scan --size 1 per image, to Notion's file storage, not the API

Titles for reports come from what the scan already read: search results, database rows and child page links. Downloads go to Notion's file storage and don't count toward the API limit.

For example, the test page used during development (columns, a toggle, a synced block, a child page and an inline database with one row) took 11 requests in blocks mode and 6 in markdown mode. Pages with many toggles, columns or nested lists save more.

All Notion requests share one rate limiter at about 2.5 requests per second, under Notion's limit of 180 per minute. When Notion answers 429 or 529, the plugin waits for the Retry-After time and retries, and it retries server errors and network errors with backoff. Requests go through PicGo's HTTP client, so the proxy in PicGo settings applies.

Privacy

Files uploaded to Notion are private: Notion serves them through signed links that expire. Most image hosts return public URLs that anyone with the link can open, forever. Before migrating, make sure the images are fine to be public, or use a host with private buckets or access control. Use Exclude or --page to leave sensitive pages alone.

The token is stored in PicGo's config file in plain text, like other PicGo secrets. Prefer a personal access token with a short expiration, and revoke it when you are done.

Limitations

  • Only image blocks are migrated. Not yet supported: file, PDF, video and audio blocks, page covers and icons, and files in database properties.
  • Notion's search, used when no pages are configured, is not guaranteed to return every page. If something is missing, add its parent page to Pages.
  • Linked database views of a database the token can't read are reported as warnings and skipped.
  • Notion's API lists these extensions for external images: bmp, gif, heic, jpeg, jpg, png, svg, tif, tiff. WebP and other unlisted extensions are migrated with a warning in the report, since the Notion app displays them. AVIF is skipped, because Notion doesn't list it anywhere and a working image shouldn't be replaced by one that may not render. If a compression plugin turns uploads into .avif, those updates fail instead of being written.
  • Notion rejects external URLs longer than 2000 characters; such images are reported as failed.
  • Uploaded files are named <blockId>.<ext> so names never collide. Rename plugins and PicGo's own rename settings still apply.
  • Migrating changes the blocks, so pages show as edited by you or the connection.
  • The old file stays in the page's version history, which per Notion's help no longer counts toward storage.
  • PicGo can't cancel an upload in progress, so Stop takes effect after the current batch's uploads finish.

Development

pnpm install
pnpm build      # TypeScript → dist/ (CommonJS, loaded by PicGo with require)
pnpm lint       # eslint + type check
pnpm test       # build, then unit and end-to-end tests

The end-to-end test loads dist/index.js into a real PicGo-Core instance and runs the CLI and GUI menu against a local mock Notion server. Two environment variables exist for that and are not meant for normal use: NOTION_MIGRATOR_API_URL (API base URL) and NOTION_MIGRATOR_RPS (requests per second).

Commits and releases

The repo follows the PicGo commit convention through @picgo/bump-version: use pnpm cz to write a commit message, and the commit-msg hook checks it with commitlint.

To release, run pnpm release (patch by default, -t minor or -t major for bigger bumps). It bumps package.json, updates CHANGELOG.md, commits, and creates a v<version> tag. Push both with git push --follow-tags origin main; the tag starts the release workflow, which lints, tests, builds and publishes to npm with provenance through npm trusted publishing, so no npm token is stored in the repo.

License

MIT

About

Move Notion-hosted images to your PicGo image host and free Notion file storage

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages