Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Do not merge] Export purposes only #314

Draft
wants to merge 21 commits into
base: master
Choose a base branch
from
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
39 changes: 39 additions & 0 deletions internal/clearAll.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import sanityClient from '@sanity/client';

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

async function deleteAllBlogPostDev() {
try {
// 1. Fetch all documents of type 'blogPostDev'
const query = '*[_type == "blogPostDev"]._id';
const ids = await client.fetch(query);

console.log(`Found ${ids.length} blogPostDev documents to delete.`);

// 2. Delete the documents in batches
const batchSize = 100; // Adjust based on your needs
for (let i = 0; i < ids.length; i += batchSize) {
const batch = ids.slice(i, i + batchSize);
const transaction = client.transaction();

batch.forEach(id => {
transaction.delete(id);
});

console.log(`Deleting batch ${i / batchSize + 1}...`);
await transaction.commit();
console.log(`Batch ${i / batchSize + 1} deleted.`);
}

console.log('All blogPostDev documents have been deleted.');
} catch (error) {
console.error('Error deleting documents:', error);
}
}

deleteAllBlogPostDev();
927 changes: 927 additions & 0 deletions internal/export.json

Large diffs are not rendered by default.

144 changes: 144 additions & 0 deletions internal/export.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import fs from 'fs';
import path from 'path';
import matter from 'gray-matter';
import * as cheerio from 'cheerio';

const postsDirectory = path.join(process.cwd(), '../src/content/blog');
const outputFile = path.join(process.cwd(), 'export.json');

function extractImagePaths(content, postPath) {
const images = [];

// Extract HTML images
const $ = cheerio.load(content);
$('img').each((i, elem) => {
const src = $(elem).attr('src');
if (src) {
images.push(src);
}
});

// Extract Markdown images
const markdownImageRegex = /!\[.*?\]\((.*?)\)/g;
let match;
while ((match = markdownImageRegex.exec(content)) !== null) {
images.push(match[1]);
}

// Remove duplicates
return [...new Set(images)];
}

function sanitizeMarkdown(content) {
const $ = cheerio.load(`<div id="root">${content}</div>`);

$('p').each((i, elem) => {
const $elem = $(elem);
$elem.replaceWith(`\n\n${$elem.html().trim()}\n\n`);
});

$('s, del, strike').each((i, elem) => {
const $elem = $(elem);
$elem.replaceWith(`~~${$elem.html()}~~`);
});

$('sup').each((i, elem) => {
const $elem = $(elem);
$elem.replaceWith(`^${$elem.html()}^`);
});

$('sub').each((i, elem) => {
const $elem = $(elem);
$elem.replaceWith(`~${$elem.html()}~`);
});

$('a').each((i, elem) => {
const $elem = $(elem);
const href = $elem.attr('href');
const text = $elem.text().replace(/\n/g, ' ');
$elem.replaceWith(`[${text}](${href})`);
});

$('blockquote').each((i, elem) => {
const $elem = $(elem);
const text = $elem.html().trim().replace(/\n/g, '\n> ');
$elem.replaceWith(`\n\n> ${text}\n\n`);
});

$('em, i').each((i, elem) => {
const $elem = $(elem);
$elem.replaceWith(`*${$elem.html().replace(/\n/g, ' ')}*`);
});

$('strong, b').each((i, elem) => {
const $elem = $(elem);
$elem.replaceWith(`**${$elem.html().replace(/\n/g, ' ')}**`);
});

$('code').each((i, elem) => {
const $elem = $(elem);
if ($elem.parent().is('pre')) {
// This is a code block, leave it as is
return;
}
$elem.replaceWith(`\`${$elem.html()}\``);
});

$('hr').each((i, elem) => {
$(elem).replaceWith('\n\n---\n\n');
});

$('ul, ol').each((i, elem) => {
const $elem = $(elem);
const listItems = $elem.children('li').map((i, li) => {
const prefix = $elem.is('ul') ? '- ' : `${i + 1}. `;
return prefix + $(li).html().trim();
}).get().join('\n');
$elem.replaceWith(`\n\n${listItems}\n\n`);
});

// Remove any remaining HTML tags
// $('*').each((i, elem) => {
// const $elem = $(elem);
// $elem.replaceWith($elem.html());
// });

let markdown = $('#root').html().trim();
markdown = markdown.replace(/\n{3,}/g, '\n\n');
return markdown;
}

function getPostsRecursively(dir) {
let posts = [];
const items = fs.readdirSync(dir, { withFileTypes: true });

for (const item of items) {
const fullPath = path.join(dir, item.name);

if (item.isDirectory()) {
posts = posts.concat(getPostsRecursively(fullPath));
} else if (item.isFile() && item.name.endsWith('.md')) {
const fileContents = fs.readFileSync(fullPath, 'utf8');
const { data, content } = matter(fileContents);
const convertedContent = sanitizeMarkdown(content);
const images = extractImagePaths(convertedContent, fullPath);

posts.push({
slug: path.relative(postsDirectory, fullPath).replace('.md', ''),
title: data.title,
date: data.date,
content: convertedContent,
images: images,
author: data.author,
tags: data.tags,
});
}
}

return posts;
}

const posts = getPostsRecursively(postsDirectory);

fs.writeFileSync(outputFile, JSON.stringify(posts, null, 2));
console.log(`Exported ${posts.length} posts to ${outputFile}`);
15 changes: 15 additions & 0 deletions internal/findPerson.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import sanityClient from '@sanity/client';

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

async function findPerson(name) {
const person = await client.fetch(`*[_type == "person" && name == $name][0]`, { name });
return person
}

export default findPerson;
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;
Loading
Loading