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
-
I'm migrating my blog from one engine to another. Now I want to move the first image of my blogs to the Is there a way to achieve this? I would basically need a transformer that takes the first image and moves that into the frontmatter. Any thoughts on how to do this? |
Beta Was this translation helpful? Give feedback.
Answered by
wooorm
Dec 31, 2020
Replies: 1 comment 5 replies
-
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)
} |
Beta Was this translation helpful? Give feedback.
5 replies
Answer selected by
wooorm
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
assuming when frontmatter is used that it’s YAML, then it would be: