Skip to content

Instantly share code, notes, and snippets.

@axcellence
Last active April 4, 2023 09:40
Show Gist options
  • Save axcellence/9304f8982302eb8c48b7d918cd819934 to your computer and use it in GitHub Desktop.
Save axcellence/9304f8982302eb8c48b7d918cd819934 to your computer and use it in GitHub Desktop.
Asking ChatGPT (GPT-4): How would I export my data from Sanity.io into markdown files?

To export your data from Sanity.io into markdown files, you'll need to create a script that fetches the data from Sanity and then converts it into the desired markdown format. You can use Node.js for this task. The following steps outline the process:

Install required dependencies: First, make sure you have Node.js installed on your computer. Then, create a new folder for your project and initialize it with npm:

mkdir sanity-export
cd sanity-export
npm init -y

Next, install the required dependencies: sanity-client, slugify, and fs-extra:

npm install sanity-client slugify fs-extra

Set up Sanity client: Create a new file named sanityClient.js in the project folder and configure the Sanity client with your project ID, dataset, and API token:

// sanityClient.js
const sanityClient = require('@sanity/client');

const client = sanityClient({
  projectId: 'your-project-id',
  dataset: 'your-dataset-name',
  token: 'your-api-token', // Get this from your Sanity project's dashboard
  useCdn: false,
});

module.exports = client;

Write a script to fetch data and convert it into markdown: Create a new file named exportToMarkdown.js in the project folder and write a script to fetch data from Sanity, convert it into markdown, and save it as markdown files:

// exportToMarkdown.js
const fs = require('fs-extra');
const slugify = require('slugify');
const sanityClient = require('./sanityClient');

const exportPath = './markdown-export';

const getPosts = async () => {
  const query = '*[_type == "post"]{title, content}';
  return await sanityClient.fetch(query);
};

const convertToMarkdown = (title, content) => {
  return `# ${title}\n\n${content}`;
};

const saveAsMarkdown = async (title, content) => {
  const slug = slugify(title, { lower: true, strict: true });
  const fileName = `${slug}.md`;
  const filePath = `${exportPath}/${fileName}`;

  await fs.outputFile(filePath, content);
  console.log(`Exported: ${filePath}`);
};

(async () => {
  try {
    const posts = await getPosts();

    if (!posts.length) {
      console.log('No posts found');
      return;
    }

    await fs.ensureDir(exportPath);

    for (const post of posts) {
      const markdownContent = convertToMarkdown(post.title, post.content);
      await saveAsMarkdown(post.title, markdownContent);
    }

    console.log('Export completed');
  } catch (error) {
    console.error('Error exporting data:', error.message);
  }
})();

Make sure to adjust the query variable to match your schema and data types. In the example above, we assumed that you have a "post" type with a "title" and "content" field. You may need to change this according to your specific schema.

Run the script: Execute the exportToMarkdown.js script using Node.js:

node exportToMarkdown.js

After running the script, a new folder named markdown-export will be created in your project directory with the exported markdown files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment