Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

help making yaml files more powerful #2817

Closed
andykais opened this issue Nov 6, 2017 · 3 comments
Closed

help making yaml files more powerful #2817

andykais opened this issue Nov 6, 2017 · 3 comments

Comments

@andykais
Copy link

andykais commented Nov 6, 2017

Hi all, can I get some help writing markdown within a yaml page? I am attempting a switch from jekyll to gatsby, but one of the nice features of jekyll was the liquid templating language which allowed you to pass any string (from a yaml file for instance) through a markdownify pipe. This was useful when I had several pieces of information I wanted structured on the page but organized together in a yaml file. Example:

- title: My Title
  image: ./assets/image.png
  description: >
    Here is a long winded description that includes a [link](https://code-somewhere.com).
- title: Second Item
  image: ./assets/image2.png
  description >
    more text ...

On a similar note, loading an image this way seems to not be possible, I have hacky solutions for that, like importing all the images somewhere else in the file, but is there a way to let webpack know to copy any images in the pages/ folder even if they 'appear' to not be used?

@KyleAMathews
Copy link
Contributor

On images, if you use gatsby-source-filesystem + gatsby-transformer-yaml + gatsby-transformer-sharp/gatsby-plugin-sharp, you can query these images.

See for example how gatsbyjs.org queries author avatars

https://github.com/gatsbyjs/gatsby/blob/master/www/src/templates/template-blog-post.js#L306 & https://github.com/gatsbyjs/gatsby/blob/master/docs/blog/author.yaml

On running yaml fields through markdown — you can create sub-nodes that you specify their mediatype as markdown and then gatsby-transformer-remark will convert them to html e.g.

function createTextNode(node, key, text, createNode) {

Sorry if that's super high-level and not very actionable. Gatsby is focused on making it easy to setup custom data transformation pipelines. For common tasks, people can create higher-level plugins which setup stuff like this for you automatically but I don't know that anyone has yet for yaml => markdown => html.

To make this happen you'll need to dive into Gatsby's docs and look at plugins to understand how to do what you want to do.

@andykais
Copy link
Author

andykais commented Nov 8, 2017

thanks for the advice, I dont think Im ready to commit that much time into essentially making a gatsby plugin right now. I have my naiive solution like so:

import data from './_data.yaml'
import markdownIt from 'markdown-it'
const md = markdownIt({
  html: true,
  linkify: true
})
const loadedData = data.map(item => ({
  ...item,
  image: item.image && require(item.image),
  description: md.render(item.description)
}))

this can still happen during the build stage so it works for my purposes. I think the problem is graphql is aimed at building data first and using a similar template for multiple pages. I am following a 'module' structure like so:

pages/
  one-page/
    index.js
    _data.yml
    assets/
      img1.png
  another-page/
    ...

@andykais andykais closed this as completed Nov 8, 2017
@KyleAMathews
Copy link
Contributor

A simple way to do this would be to compile the markdown fields using onCreateNode. https://www.gatsbyjs.org/tutorial/part-four/#programmatically-creating-pages-from-data and add the compiled field as a new field with createNodeField.

You'd just do that in your gatsby-node.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants