|
| 1 | +import { Glob } from "glob"; |
| 2 | +import matter from "gray-matter"; |
| 3 | +import fs from "fs"; |
| 4 | +import { createClient } from "@sanity/client"; |
| 5 | +import { micromark } from "micromark"; |
| 6 | +import { htmlToBlocks } from "@sanity/block-tools"; |
| 7 | +import { Schema } from "@sanity/schema"; |
| 8 | +import { JSDOM } from "jsdom"; |
| 9 | + |
| 10 | +export const client = createClient({ |
| 11 | + projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID, |
| 12 | + dataset: process.env.NEXT_PUBLIC_SANITY_DATASET, |
| 13 | + useCdn: false, |
| 14 | + token: process.env.SANITY_API_READ_TOKEN, |
| 15 | +}); |
| 16 | + |
| 17 | +import { v2 as cloudinary } from "cloudinary"; |
| 18 | + |
| 19 | +// Return "https" URLs by setting secure: true |
| 20 | +cloudinary.config({ |
| 21 | + secure: true, |
| 22 | +}); |
| 23 | + |
| 24 | +const TYPE = "sponsor"; |
| 25 | +const BASE = `../src/routes/(content-single)/(non-course)/${TYPE}/`; |
| 26 | +const g = new Glob(`${BASE}**/*.md`, {}); |
| 27 | + |
| 28 | +const delay = async (ms) => new Promise((res) => setTimeout(res, ms)); |
| 29 | +const addArticle = async (post) => { |
| 30 | + return client.create(post, { dryRun: false }); |
| 31 | +}; |
| 32 | + |
| 33 | +const findPublic = (url) => { |
| 34 | + const split = url.split("/"); |
| 35 | + const record = split.findIndex((str) => str === "main-codingcatdev-photo"); |
| 36 | + let id = ""; |
| 37 | + for (let i = record; i < split.length; i++) { |
| 38 | + if (i !== split.length - 1) { |
| 39 | + id += split[i]; |
| 40 | + id += "/"; |
| 41 | + } else { |
| 42 | + const lastSplit = split[i].replace(/\.[^/.]+$/, ""); |
| 43 | + id += lastSplit; |
| 44 | + } |
| 45 | + } |
| 46 | + return id; |
| 47 | +}; |
| 48 | + |
| 49 | +const getImage = async (url) => { |
| 50 | + return cloudinary.api.resource(findPublic(url)); |
| 51 | +}; |
| 52 | + |
| 53 | +const defaultSchema = Schema.compile({ |
| 54 | + name: "myBlog", |
| 55 | + types: [ |
| 56 | + { |
| 57 | + type: "object", |
| 58 | + name: "blogPost", |
| 59 | + fields: [ |
| 60 | + { |
| 61 | + title: "content", |
| 62 | + name: "content", |
| 63 | + type: "array", |
| 64 | + of: [{ type: "block" }], |
| 65 | + }, |
| 66 | + ], |
| 67 | + }, |
| 68 | + ], |
| 69 | +}); |
| 70 | +const blockContentType = defaultSchema |
| 71 | + .get("blogPost") |
| 72 | + .fields.find((field) => field.name === "content").type; |
| 73 | + |
| 74 | +for await (const file of g) { |
| 75 | + const mdFile = fs.readFileSync(file, { encoding: "utf8", flag: "r" }); |
| 76 | + const { data, content } = await matter(mdFile); // data has frontmatter, code is html |
| 77 | + const fm = data; |
| 78 | + if (!fm) continue; |
| 79 | + // TODO: We might need to add a check on cononical if this page is already in dev.to |
| 80 | + if ( |
| 81 | + fm?.slug && |
| 82 | + fm?.name && |
| 83 | + fm?.cover && |
| 84 | + // fm.slug === 'alex-patterson' && |
| 85 | + fm?.published === "published" && |
| 86 | + new Date(fm?.start) < new Date() |
| 87 | + ) { |
| 88 | + console.log("Adding", { slug: fm?.slug }); |
| 89 | + const core = { |
| 90 | + _createdAt: new Date(fm.start).toISOString(), |
| 91 | + _type: TYPE, |
| 92 | + _updatedAt: fm?.updated |
| 93 | + ? new Date(fm.updated).toISOString() |
| 94 | + : new Date(fm.start).toISOString(), |
| 95 | + slug: { current: fm.slug, _type: "slug" }, |
| 96 | + title: fm.name, |
| 97 | + excerpt: fm.excerpt, |
| 98 | + content: htmlToBlocks(micromark(content), blockContentType, { |
| 99 | + parseHtml: (html) => new JSDOM(html).window.document, |
| 100 | + }), |
| 101 | + }; |
| 102 | + if (fm?.socials) { |
| 103 | + core.socials = fm?.socials; |
| 104 | + } |
| 105 | + if (fm?.cover) { |
| 106 | + const image = await getImage(fm.cover); |
| 107 | + console.log(image); |
| 108 | + core.coverImage = image; |
| 109 | + } |
| 110 | + if (fm?.url) { |
| 111 | + core.url = fm.url; |
| 112 | + } |
| 113 | + |
| 114 | + const response = await addArticle(core); |
| 115 | + console.log("added", response._id); |
| 116 | + } |
| 117 | +} |
0 commit comments