Skip to content

Commit

Permalink
Add tag import
Browse files Browse the repository at this point in the history
  • Loading branch information
MythicalFish committed Sep 26, 2024
1 parent 51a7208 commit 191c38c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
17 changes: 17 additions & 0 deletions internal/findTag.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import sanityClient from '@sanity/client';

export const client = sanityClient({
projectId: 'o2y1bt2g',
dataset: 'seqera',
token: process.env.SANITY_TOKEN,
useCdn: false,
});

async function findTag(slug) {
let title = slug.replace(/-/g, ' ');
if (title === 'pipelines') title = 'seqera pipelines';
const tag = await client.fetch(`*[_type == "tag" && lower(title) == lower($title)][0]`, { title });
return tag
}

export default findTag;
24 changes: 24 additions & 0 deletions internal/import.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from 'path';
import { customAlphabet } from 'nanoid';
import { marked } from 'marked';
import findPerson from './findPerson.mjs';
import findTag from './findTag.mjs';

const nanoid = customAlphabet('0123456789abcdef', 12);

Expand Down Expand Up @@ -306,6 +307,15 @@ async function migratePosts() {
}
}

const tags = []

for (const tag of post.tags.split(',')) {
const tagObj = await findTag(tag);
if (tagObj) tags.push(tagObj);
}

const tagRefs = tags.map(tag => ({ _type: 'reference', _ref: tag._id, _key: nanoid() }));

const person = await findPerson(post.author);
if (!person) {
console.log(`⭕ No person found with the name "${post.author}"; skipping import.`);
Expand All @@ -319,13 +329,27 @@ async function migratePosts() {
let dateStr = post.date.split('T')[0];
dateStr = `${dateStr} 8:00`;

const existingPost = await client.fetch(`*[_type == "blogPostDev" && meta.slug.current == $slug][0]`, { slug: newSlug });

if (existingPost) {
console.log(`Updating post: ${existingPost.title}`, tagRefs);
try {
await client.patch(existingPost._id).set({ tags: tagRefs }).commit();
} catch (error) {
console.error(`Failed to update post: ${existingPost.title}`);
console.error(error);
}
continue;
}

const sanityPost = {
_type: 'blogPostDev',
title: post.title,
meta: { slug: { current: newSlug } },
publishedAt: new Date(dateStr).toISOString(),
body: portableTextContent,
author: { _type: 'reference', _ref: person._id },
tags: tags.map(tag => ({ _type: 'reference', _ref: tag._id })),
};

try {
Expand Down

0 comments on commit 191c38c

Please sign in to comment.