tinacms: Media folder relative to content directories not supported

Describe the issue that you’re seeing. Any Loom videos or screenshots usually help a lot!

[Edited]

I have a repo structure that follows the (original) https://github.com/gatsbyjs/gatsby-starter-blog setup: 2023-02-01_12h55_45 So the assets where I store the media is in a sibling folder to the place of the Markdown files (blog).

As a consequence my image links are like ../assets/my_image.jpg Both in the markdowns and in the frontmatter attributes. This is my target, to make these links work.

Setup no.1

My approach to make it work with Tina was this Config:

  media: {
    tina: {
      publicFolder: "content/blog",
      mediaRoot: "../assets",
    },
  },

Unfortunately it does not work.

  • newly created markdown text for an image is ![Image Name](/../assets/my_image.jpg) when saved - see the first character
    Though in Tina editor, even when reopened, the image loads and is working.
  • for previous / pre-Tine images the editor loading URLs are like
    https://assets.tina.io/19f5616b-0bba-4f92-a99c-84f7fe3eeffb../assets/my_image.jpg - obviously broken
  • syncing media fails with timeout. This is the dialog: 2023-02-01_12h52_26 and the console:
Failed to load resource: the server responded with a status of 405 ()
index.es.js:8971 SyntaxError: Unexpected token 'M', "Method Not Allowed" is not valid JSON
(anonymous) @ index.es.js:8971
index.es.js:9046 'Error in syncing media, check below for more details
Re @ index.es.js:9046
index.es.js:9047 Error: Time out error
    at a (index.es.js:8881:14)
Re @ index.es.js:9047

Setup no.2

Then I changed my Tina Config to:

  media: {
    tina: {
      publicFolder: "content",
      mediaRoot: "assets",
    },
  },

Results:

  • newly created markdown text for an image is ![](/assets/my_image.jpg) when saved - the double dot is missing
    Though in Tina editor, even when reopened, the image loads and is working.
  • for previous / pre-Tine images the editor loading URLs are like
    https://assets.tina.io/19f5616b-0bba-4f92-a99c-84f7fe3eeffb../my_image.jpg - still broken but differently
  • syncing media works

Again: my original and only goal is to have the ../assets/my_image.jpg image links work.

Maybe Tina has to be improved to be able to handle them, maybe it already can but with a different Config. Please help.

System Info

System:
    OS: Linux 5.15 Ubuntu 22.04.1 LTS 22.04.1 LTS (Jammy Jellyfish)
    CPU: (8) x64 AMD Ryzen 7 PRO 3700U w/ Radeon Vega Mobile Gfx
    Memory: 11.88 GB / 14.61 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 18.13.0 - /usr/bin/node
    Yarn: 1.22.19 - ~/Work/eztettem/node_modules/.bin/yarn
    npm: 8.19.3 - /usr/bin/npm
  npmPackages:
    @tinacms/cli: ^1.0.7 => 1.0.7
    tinacms: ^1.1.4 => 1.1.4

Validations

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 1
  • Comments: 18 (1 by maintainers)

Most upvoted comments

Anyone ever find a solution to this?

I also want my images to be uploaded into my /src/assets folder accessed via the root (src is sibling to tina directory)

I use Astro and use the integrated image processing, so ideally I would be able to upload images into the src folder rather than public so the images can be processed on build. Is this possible?

i ended up getting this working by setting tina up to upload to src/assets

// tina/config.ts
export default defineConfig({
  media: {
    tina: {
      mediaRoot: "src/assets",
      publicFolder: "",
    },
  },

and using a remark plugin to rewrite the tina paths to astro friendly paths

// @ts-check
import { defineConfig } from "astro/config";
import { visit } from "unist-util-visit";

// replaces tina friednly src/assets/ with @assets/
export function remarkAstroAssetsImages() {
  let options = {
    search: /^\/src\/assets\//,
    replace: "@assets/",
  };
  return function image() {
    return (tree) => {
      visit(tree, "image", (node) => {
        if (options.search.test(node.url)) {
          node.url = node.url.replace(options.search, options.replace);
        }
      });
    };
  };
}

export default defineConfig({
  markdown: {
    remarkPlugins: [
      remarkAstroAssetsImages(),
    ],
    rehypePlugins: [],
  },
});

I spent some time the past few days looking at this (or, a similar problem). I’m using Astro and their content collections and image optimizations, with TinaCMS for editing. To use that, I need to have the markdown image name formatted a specific way (full file path from base of project), and having images in the public directory is fine.

Goals:

  • Upload images to /public/images/media.jpg (this works in both local and production)
  • Put the image in the markdown with that full path (including public), so /public/images/media.jpg, I’ve now got this working too
  • Get the image preview to work. It currently works locally but not in production. In production it seems to take that “/public/images/media.jpg” from the markdown file and put the “https://assets.tina.io.xxx” at the front. In other words, it leaves the extra “/public” there so the image won’t show properly. Also, when I upload a new image it puts the preview at “/public/images/media.jpg” and completely gets rid of assets.tina.io stuff until I reload. That probably has to do with my parse function but I don’t know a good way around it right now.

Note: my media.tina.publicFolder is “public” and media.tina.mediaRoot is “”

Things I’m trying I have found in the docs, the main thing being this page which has this section:

image

However, this doesn’t seem to work fully. “previewSrc” does not seem supported at all for instance. Here is my current solution:

fields: [
  {
    name: "avatar",
    label: "Avatar",
    required: true,
    type: "image",
    ui: {
      component: "image",
  
      // generate the frontmatter value based on the filename
      parse(media) {
        // in production this starts with https://assets.tina.io
        if (media.includes("/public")) {
          return media.replace("/public", "");
        }
  
        // get everything after the last slash, looks like "media.jpg"
        const lastIdxOfSlash = media.lastIndexOf("/");
  
        // if no "/", return new file path + entire string
        if (lastIdxOfSlash === -1) {
          return "/public/images/" + media;
        }
  
        // return new file path + everything after the last "/"
        return "/public/images/" + media.substring(lastIdxOfSlash + 1);
      },
  
      // decide the file upload directory for the image, if different from the Tina media root
      // this goes to /{media.tina.publicFolder}/{media.tina.mediaRoot}{uploadDir}media.jpg
      // for this, that is /public/images/media.jpg
      uploadDir: () => "images/",
  
      // generate src attribute for the preview image
      // NOTE: This is discussed in the docs, but it does not seem to actually exist in the codebase
      previewSrc: (fullSrc) => fullSrc.replace("/public", ""),
    },
  },
]

This seems an ok workaround. Am I missing some feature I should be using? Or can support for this be added? I feel like this should be simple (but these things often never are), and would be a commonly requested feature. If this existed nicely, it would be great for many SSG’s.

I’m experiencing the same issue with broken image links due to .. in the path (for example, http://localhost:3000/../uploads/my_image.jpg).

To provide some context, I recently migrated from Forestry.io to TinaCMS, where everything was working smoothly.

I’m using Gridsome SSG (https://gridsome.org/) and my folder structure looks like this: image

Essentially, my media files are located in the “uploads” folder (at root level), which is a sibling to the “tina”, “src”, “static” and “data” (containing all my markdown files) folders.

Unfortunately, I can’t move my media files to a folder inside the “static” folder, as Gridsome needs to run optimizations on the images. Therefore, I need to keep my media files outside of the static folder.

Ideally, the images would be inside the “src” folder (https://gridsome.org/docs/images/), but doing so would mean losing the ability to manage media files through TinaCMS.

But once again, this data structure used to work very well in Forestry.