Skip to content

Is there a way to move the first image from markdown to the frontmatter? #116

Answered by wooorm
marcofranssen asked this question in Q&A
Discussion options

You must be logged in to vote

assuming when frontmatter is used that it’s YAML, then it would be:

module.exports = myPlugin

function myPlugin() {
  return transform
}

function transform(tree) {
  if (!tree.children[0] || tree.children[0].type !== 'yaml') return
  if (
    !tree.children[1] ||
    tree.children[1].type !== 'paragraph' ||
    tree.children[1].children.length !== 1 ||
    tree.children[1].children[0].type !== 'image'
  ) return

  tree.children[0].value += '\nsomeCoverImageProp: ' + JSON.stringify(tree.children[1].children[0].url)
  tree.children.splice(1, 1)
}

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@marcofranssen
Comment options

@wooorm
Comment options

@marcofranssen
Comment options

@marcofranssen
Comment options

@marcofranssen
Comment options

Answer selected by wooorm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants