Skip to content

Instantly share code, notes, and snippets.

@kmelve
Last active June 1, 2018 22:05
Show Gist options
  • Select an option

  • Save kmelve/4442b4233e10d8c2a9a46c4b06ad01bf to your computer and use it in GitHub Desktop.

Select an option

Save kmelve/4442b4233e10d8c2a9a46c4b06ad01bf to your computer and use it in GitHub Desktop.
Block content to Markdown script
/* eslint-disable no-console */
/**
* Generate a markdown document in a dev.to format.
* This runs from the studio folder and requires additional
* dependencies:
* - @sanity/block-content-to-markdown
* - minimist
*
* Run this with $ sanity exec markdown.js -- --id=<document id>
*/
const client = require('part:@sanity/base/client')
const toMarkdown = require('@sanity/block-content-to-markdown')
const argv = require('minimist')(process.argv.slice(2));
const baseUrl = "https://sanity.io/blog"
const { id = '' } = argv
if (!id) {
console.error('You have to specify an id: sanity exec markdown.js -- --id=<document-id>')
process.exit(1)
}
const serializers = {
types: {
code: props => '```' + props.node.language + '\n' + props.node.code + '\n```',
gotcha: props => `## Gotcha\n\n${props.node.text}\n\n`,
callToAction: props => `[*${props.node.linkText}*](${props.node.url})`
}
}
client.fetch('*[_id == $id][0]', { id }).then(article => {
const { title, text = [], blurb = [], slug = '' } = article
console.log(`
---
title: ${title}
published: false
description: ${toMarkdown(blurb, {serializers, dataset: client.clientConfig.dataset, projectId: client.clientConfig.projectId})}
tags:
canonical_url: ${baseUrl}/${slug.current}
---
`)
console.log(toMarkdown(text, {serializers, dataset: client.clientConfig.dataset, projectId: client.clientConfig.projectId}))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment