seratch / notion-translator

CLI tool to translate Notion pages into a different language
MIT License
70 stars 20 forks source link

Image blocks do not work in a copied page #1

Closed jmeiss closed 2 years ago

jmeiss commented 2 years ago

First of all, thanks a lot for that very useful package, it will save us a lot of time and sweat 🙏

I just tried to use it on a page with an image but I'm getting the following error:

  body: '{"object":"error","status":400,"code":"validation_error","message":"body failed validation: body.children[7].image.external should be defined, instead was `undefined`."}'

I tried to add a statement to read the type === "image" but after digging a bit, it looks like I can't copy an image since the image URL is now expirable ( https://twitter.com/notionhq/status/1463669460938760198?lang=en )

Adding that block as a workaround prevent the script from crashing but I was really hopping to be able to copy the image:

      if (b.type === "image") {
        // The image blocks do not work in a copied page
        const notice = [
          {
            plain_text: "(The image was removed from this page)",
            text: { content: "" },
          },
        ];
        await translateText(notice, "en", to);
        b = {
          type: "paragraph",
          paragraph: {
            color: "default",
            rich_text: notice,
          },
        };
      }

From my understanding, I can't copy the image because I can't fetch then re-upload the image.

We don't currently support uploading files to Notion through the API

https://developers.notion.com/reference/file-object

My ticket is to:

  1. let you be aware of that issue
  2. hopping you would figure out a solution I didn't think about 🤞
seratch commented 2 years ago

Hi @jmeiss, thanks for sharing this! Your solution looks great to me. Would you mind sending a pull request to improve this?

jmeiss commented 2 years ago

Hi @seratch

At the end, I went for a completely different solution. Since I needed to translate many pages with their subpages and images, I simply duplicated the whole notion folder (aka Page) in order to not have to rebuild the folders and files structure as well as duplicate the images. I then went through each page, sent it to my updated version of your package in order to translate and translate in place each string.

Here is the code diff if you're interested in it: https://github.com/Fluicity/notion-translator/commit/4c6d82d3650afd26603a51613dcc66a726b39c1c

Once again, thanks a lot for that very helpful package 🙏

seratch commented 2 years ago

@jmeiss Thanks for sharing the details! I've checked this issue on my end and found that, as long as the image URLs are external ones, which do not expire, image blocks still work in the translation process.

I've been using this template for testing this tool. The images in the page do not expire. This is why I didn't realize the expiration of uploaded image URLs.

Your updates are awesome! I look forward to seeing the evolution on your fork 👍

midshipman commented 10 months ago

Hi, why not upload and replace it with external images? I asked ChatGPT and get the code below to use cloudinary, and it works :)

const cloudinary = require('cloudinary').v2;

// Configure your Cloudinary credentials cloudinary.config({ cloud_name: 'your_cloud_name', api_key: 'your_api_key', api_secret: 'your_api_secret' });

// The URL of the image you want to fetch and upload const imageUrl = 'http://external.domain.com/path-to-image.jpg';

// Use Cloudinary's API to fetch and upload the image cloudinary.uploader.upload(imageUrl, { resource_type: "image" }, function(error, result) { if (error) { console.log('Error:', error); } else { console.log('New Image URL:', result.url); } });