Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions carbon-projects/schemas/indexContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ export default defineType({
description:
"Short description, e.g. for retirement PDFs. Ideally 300-600 chars, no newlines, no bullet points.",
group: "info",
type: "text",
validation: (r) => r.min(20).max(700),
type: "text"
}),
defineField({
name: "longDescription",
description: "Longer description",
group: "info",
type: "text",
type: "array",
of: [{type: "block"}]
}),
defineField({
name: "sdgs",
Expand Down
6 changes: 3 additions & 3 deletions carbon-projects/schemas/projectContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ export default defineType({
description:
"Short description, e.g. for retirement PDFs. Ideally 300-600 chars, no newlines, no bullet points.",
group: "info",
type: "text",
validation: (r) => r.min(20).max(700),
type: "type"
}),
defineField({
name: "longDescription",
description: "Longer description",
group: "info",
type: "text",
type: "array",
of: [{type: "block"}]
}),
defineField({
name: "extraSdgs",
Expand Down
74 changes: 74 additions & 0 deletions carbon-projects/scripts/migrateProjectContentTextToBlock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// scripts/textToBlock.js

/* eslint-disable no-console */
import { customAlphabet } from "nanoid";
import sanityClient from "part:@sanity/base/client";
const client = sanityClient.withConfig({ apiVersion: "2021-09-01" });

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

const TYPE = "projectContent"; // document _type to consider

const fetchDocuments = () =>
client.fetch(`*[_type == "${TYPE}"][0..50] {_id, _rev, longDescription}`);

const buildPatches = (docs) =>
docs.map((doc) => {
const paragraphs = doc.longDescription.split("\n");
const output = paragraphs.map((paragraph) => ({
_key: nanoid(),
_type: "block",
markDefs: [],
style: "normal",
children: [
{
_key: nanoid(),
_type: "span",
marks: [],
text: paragraph,
},
],
}));

return {
id: doc._id,
patch: {
set: {
longDescription: output,
},
ifRevisionID: doc._rev,
},
};
});

const createTransaction = (patches) =>
patches.reduce(
(tx, patch) => tx.patch(patch.id, patch.patch),
client.transaction()
);

const commitTransaction = (tx) => tx.commit();

const migrateNextBatch = async () => {
const documents = await fetchDocuments();
const patches = buildPatches(documents);

if (patches.length === 0) {
console.log("No more documents to migrate!");
return null;
}
console.log(
`Migrating batch:\n %s`,
patches
.map((patch) => `${patch.id} => ${JSON.stringify(patch.patch)}`)
.join("\n")
);
const transaction = createTransaction(patches);
await commitTransaction(transaction);
return migrateNextBatch();
};

migrateNextBatch().catch((err) => {
console.error(err);
process.exit(1);
});