This is an advanced-use feature, and likely not something you'll need to configure. What you probably want is the content types reference!
The image
field is used for content values that point to an image used on the page. This field allows you to upload new images by via dragging or selection in Finder. Note this field does not handle any images included in the Markdown body, those would be handled by the markdown component.
interface ImageConfig {component: 'image'name: stringlabel?: stringdescription?: stringclearable?: booleanparse(media: Media): stringpreviewSrc(formValues: any)?: stringuploadDir(formValues: any)?: string}
This interfaces only shows the keys unique to the image field. Visit the Field Config docs for a complete list of options.
Next.js
Below is an example of an image
field defined in a Next.js project.
const formOptions = {fields: [{label: 'Hero Image',name: 'frontmatter.hero_image',component: 'image',// Generate the frontmatter value based on the filenameparse: (media) => `/static/${media.filename}`,// Decide the file upload directory for the postuploadDir: () => '/public/static/',// Generate the src attribute for the preview image.previewSrc: (fullSrc) => fullSrc.replace('/public', ''),},],//...}
Gatsby
Below is an example of how a image
field could be defined in a Gatsby Remark form.
const BlogPostForm = {fields: [{label: 'Hero Image',name: 'rawFrontmatter.hero.image',component: 'image',parse: (media) => {if (!media) return ''return `../images/${media.filename}`},uploadDir: () => `/content/images/`,previewSrc: (src, path, formValues) => {if (!formValues.frontmatter.hero || !formValues.frontmatter.hero.image)return ''return formValues.frontmatter.hero.image.childImageSharp.fluid.src},},// ...],}
In order for image paths to be properly sourced into GraphQL, it's best if a relative path to the image is saved to the content file's front matter. Constructing this relative path will depend on where the image is uploaded to as well as the location of the content file. The following example uses a co-location strategy, where a blog post is stored in content/blog/$slug/index.md
and its images will be uploaded to content/blog/$slug/$image.png
:
import get from 'lodash.get'const path = require('path').posixconst BlogPostForm = {fields: [{name: 'rawFrontmatter.thumbnail',label: 'Thumbnail',component: 'image',previewSrc: (src, path, formValues) => {if (!formValues.frontmatter.thumbnail) return ''return formValues.frontmatter.thumbnail.childImageSharp.fluid.src},// upload images to same directory as content fileuploadDir: (formValues) => path.dirname(formValues.fileRelativePath),// image file is sibling of content fileparse: (filename) => `./${filename}`,},// ...],}
© TinaCMS 2019–2024