clone init
This commit is contained in:
88
scripts/migrate-blog-to-mdx.ts
Normal file
88
scripts/migrate-blog-to-mdx.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import 'dotenv/config';
|
||||
import axios from 'axios';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
|
||||
const WP_URL = 'https://klz-cables.com';
|
||||
|
||||
async function fetchAllPosts() {
|
||||
let page = 1;
|
||||
let allPosts: any[] = [];
|
||||
|
||||
while (true) {
|
||||
console.log(`Fetching posts page ${page}...`);
|
||||
try {
|
||||
const response = await axios.get(`${WP_URL}/wp-json/wp/v2/posts`, {
|
||||
params: {
|
||||
per_page: 100,
|
||||
page: page,
|
||||
_embed: true
|
||||
}
|
||||
});
|
||||
|
||||
const posts = response.data;
|
||||
if (posts.length === 0) break;
|
||||
|
||||
allPosts = allPosts.concat(posts);
|
||||
page++;
|
||||
} catch (error: any) {
|
||||
if (error.response && error.response.status === 400) {
|
||||
// End of pagination
|
||||
break;
|
||||
}
|
||||
console.error('Error fetching posts:', error);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return allPosts;
|
||||
}
|
||||
|
||||
function generateMdxContent(post: any, locale: 'en' | 'de') {
|
||||
const frontmatter = {
|
||||
title: post.title.rendered,
|
||||
date: post.date,
|
||||
excerpt: post.excerpt.rendered.replace(/<[^>]*>/g, '').trim(),
|
||||
featuredImage: post._embedded?.['wp:featuredmedia']?.[0]?.source_url || null,
|
||||
locale: locale
|
||||
};
|
||||
|
||||
return `---
|
||||
${JSON.stringify(frontmatter, null, 2)}
|
||||
---
|
||||
|
||||
# ${post.title.rendered}
|
||||
|
||||
${post.content.rendered}
|
||||
`;
|
||||
}
|
||||
|
||||
async function run() {
|
||||
const posts = await fetchAllPosts();
|
||||
console.log(`Fetched ${posts.length} posts.`);
|
||||
|
||||
for (const post of posts) {
|
||||
// Determine locale.
|
||||
// If using Polylang, we might check categories or tags, or a specific field if exposed.
|
||||
// Or we can check the link structure if it contains /de/ or /en/ (though API link might be different)
|
||||
// Let's try to guess from the link or content language detection if needed.
|
||||
// For now, let's assume we can filter by category or just save all and manually sort if needed.
|
||||
// Actually, Polylang usually exposes 'lang' in the API if configured, or we might need to fetch by lang.
|
||||
|
||||
// Simple heuristic: check if link contains '/de/'
|
||||
const locale = post.link.includes('/de/') ? 'de' : 'en';
|
||||
|
||||
const mdx = generateMdxContent(post, locale);
|
||||
|
||||
const outDir = path.join(process.cwd(), 'data', 'blog', locale);
|
||||
if (!fs.existsSync(outDir)) {
|
||||
fs.mkdirSync(outDir, { recursive: true });
|
||||
}
|
||||
|
||||
const filename = `${post.slug}.mdx`;
|
||||
fs.writeFileSync(path.join(outDir, filename), mdx);
|
||||
console.log(`Saved ${filename} (${locale})`);
|
||||
}
|
||||
}
|
||||
|
||||
run().catch(console.error);
|
||||
79
scripts/migrate-pages-to-mdx.ts
Normal file
79
scripts/migrate-pages-to-mdx.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import 'dotenv/config';
|
||||
import axios from 'axios';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
|
||||
const WP_URL = 'https://klz-cables.com';
|
||||
|
||||
async function fetchAllPages() {
|
||||
let page = 1;
|
||||
let allPages: any[] = [];
|
||||
|
||||
while (true) {
|
||||
console.log(`Fetching pages page ${page}...`);
|
||||
try {
|
||||
const response = await axios.get(`${WP_URL}/wp-json/wp/v2/pages`, {
|
||||
params: {
|
||||
per_page: 100,
|
||||
page: page,
|
||||
_embed: true
|
||||
}
|
||||
});
|
||||
|
||||
const pages = response.data;
|
||||
if (pages.length === 0) break;
|
||||
|
||||
allPages = allPages.concat(pages);
|
||||
page++;
|
||||
} catch (error: any) {
|
||||
if (error.response && error.response.status === 400) {
|
||||
break;
|
||||
}
|
||||
console.error('Error fetching pages:', error);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return allPages;
|
||||
}
|
||||
|
||||
function generateMdxContent(page: any, locale: 'en' | 'de') {
|
||||
const frontmatter = {
|
||||
title: page.title.rendered,
|
||||
excerpt: page.excerpt.rendered.replace(/<[^>]*>/g, '').trim(),
|
||||
featuredImage: page._embedded?.['wp:featuredmedia']?.[0]?.source_url || null,
|
||||
locale: locale
|
||||
};
|
||||
|
||||
return `---
|
||||
${JSON.stringify(frontmatter, null, 2)}
|
||||
---
|
||||
|
||||
# ${page.title.rendered}
|
||||
|
||||
${page.content.rendered}
|
||||
`;
|
||||
}
|
||||
|
||||
async function run() {
|
||||
const pages = await fetchAllPages();
|
||||
console.log(`Fetched ${pages.length} pages.`);
|
||||
|
||||
for (const page of pages) {
|
||||
// Determine locale.
|
||||
const locale = page.link.includes('/de/') ? 'de' : 'en';
|
||||
|
||||
const mdx = generateMdxContent(page, locale);
|
||||
|
||||
const outDir = path.join(process.cwd(), 'data', 'pages', locale);
|
||||
if (!fs.existsSync(outDir)) {
|
||||
fs.mkdirSync(outDir, { recursive: true });
|
||||
}
|
||||
|
||||
const filename = `${page.slug}.mdx`;
|
||||
fs.writeFileSync(path.join(outDir, filename), mdx);
|
||||
console.log(`Saved ${filename} (${locale})`);
|
||||
}
|
||||
}
|
||||
|
||||
run().catch(console.error);
|
||||
144
scripts/migrate-products-to-mdx.ts
Normal file
144
scripts/migrate-products-to-mdx.ts
Normal file
@@ -0,0 +1,144 @@
|
||||
import 'dotenv/config';
|
||||
import axios from 'axios';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { buildDatasheetModel } from './pdf/model/build-datasheet-model';
|
||||
import type { ProductData } from './pdf/model/types';
|
||||
|
||||
const WC_URL = process.env.WOOCOMMERCE_URL;
|
||||
const WC_KEY = process.env.WOOCOMMERCE_CONSUMER_KEY;
|
||||
const WC_SECRET = process.env.WOOCOMMERCE_CONSUMER_SECRET;
|
||||
|
||||
if (!WC_URL || !WC_KEY || !WC_SECRET) {
|
||||
console.error('Missing WooCommerce credentials in .env');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
async function fetchAllProducts() {
|
||||
let page = 1;
|
||||
let allProducts: any[] = [];
|
||||
|
||||
while (true) {
|
||||
console.log(`Fetching page ${page}...`);
|
||||
try {
|
||||
const response = await axios.get(`${WC_URL}/wp-json/wc/v3/products`, {
|
||||
params: {
|
||||
consumer_key: WC_KEY,
|
||||
consumer_secret: WC_SECRET,
|
||||
per_page: 100,
|
||||
page: page
|
||||
}
|
||||
});
|
||||
|
||||
const products = response.data;
|
||||
if (products.length === 0) break;
|
||||
|
||||
allProducts = allProducts.concat(products);
|
||||
page++;
|
||||
} catch (error) {
|
||||
console.error('Error fetching products:', error);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return allProducts;
|
||||
}
|
||||
|
||||
function mapWcProductToProductData(wcProduct: any, locale: 'en' | 'de'): ProductData {
|
||||
// This mapping needs to be adjusted based on actual WC response structure
|
||||
// and how translations are handled (e.g. if they are separate products or same product with different fields)
|
||||
|
||||
// Assuming standard WC response
|
||||
return {
|
||||
id: wcProduct.id,
|
||||
name: wcProduct.name,
|
||||
shortDescriptionHtml: wcProduct.short_description,
|
||||
descriptionHtml: wcProduct.description,
|
||||
images: wcProduct.images.map((img: any) => img.src),
|
||||
featuredImage: wcProduct.images[0]?.src || null,
|
||||
sku: wcProduct.sku,
|
||||
slug: wcProduct.slug,
|
||||
categories: wcProduct.categories.map((cat: any) => ({ name: cat.name })),
|
||||
attributes: wcProduct.attributes.map((attr: any) => ({
|
||||
name: attr.name,
|
||||
options: attr.options
|
||||
})),
|
||||
locale: locale // This might need to be derived
|
||||
};
|
||||
}
|
||||
|
||||
function generateMdxContent(product: ProductData, technicalData: any, locale: 'en' | 'de') {
|
||||
const frontmatter = {
|
||||
title: product.name,
|
||||
sku: product.sku,
|
||||
description: product.shortDescriptionHtml.replace(/<[^>]*>/g, ''), // Simple strip tags
|
||||
categories: product.categories.map(c => c.name),
|
||||
images: product.images,
|
||||
locale: locale
|
||||
};
|
||||
|
||||
const technicalDataJson = JSON.stringify(technicalData, null, 2);
|
||||
|
||||
return `---
|
||||
${JSON.stringify(frontmatter, null, 2)}
|
||||
---
|
||||
|
||||
# ${product.name}
|
||||
|
||||
${product.descriptionHtml}
|
||||
|
||||
## Technical Data
|
||||
|
||||
<ProductTechnicalData data={${technicalDataJson}} />
|
||||
`;
|
||||
}
|
||||
|
||||
async function run() {
|
||||
const products = await fetchAllProducts();
|
||||
console.log(`Fetched ${products.length} products.`);
|
||||
|
||||
for (const product of products) {
|
||||
// Determine locale. WC might return 'lang' property if using plugins like Polylang
|
||||
// Or we might have to infer it.
|
||||
// For now, let's assume we can detect it or default to 'en'.
|
||||
// If the site uses Polylang, usually there is a 'lang' field.
|
||||
|
||||
const locale = product.lang || 'en'; // Default to en if not found
|
||||
|
||||
// We need to handle both en and de.
|
||||
// If the API returns mixed, we process them.
|
||||
// If the API only returns default lang, we might need to fetch translations specifically.
|
||||
|
||||
// Let's try to generate for the detected locale.
|
||||
|
||||
const productData = mapWcProductToProductData(product, locale as 'en' | 'de');
|
||||
|
||||
// Build datasheet model to get technical data
|
||||
// We need to try both locales if we are not sure, or just the one we have.
|
||||
// But buildDatasheetModel takes a locale.
|
||||
|
||||
const model = buildDatasheetModel({ product: productData, locale: locale as 'en' | 'de' });
|
||||
|
||||
if (model.voltageTables.length > 0 || model.technicalItems.length > 0) {
|
||||
console.log(`Generated technical data for ${product.name} (${locale})`);
|
||||
} else {
|
||||
console.warn(`No technical data found for ${product.name} (${locale})`);
|
||||
}
|
||||
|
||||
const mdx = generateMdxContent(productData, {
|
||||
technicalItems: model.technicalItems,
|
||||
voltageTables: model.voltageTables
|
||||
}, locale as 'en' | 'de');
|
||||
|
||||
const outDir = path.join(process.cwd(), 'data', 'products', locale);
|
||||
if (!fs.existsSync(outDir)) {
|
||||
fs.mkdirSync(outDir, { recursive: true });
|
||||
}
|
||||
|
||||
const filename = `${product.slug}.mdx`;
|
||||
fs.writeFileSync(path.join(outDir, filename), mdx);
|
||||
console.log(`Saved ${filename}`);
|
||||
}
|
||||
}
|
||||
|
||||
run().catch(console.error);
|
||||
@@ -9,10 +9,10 @@ type ExcelRow = Record<string, unknown>;
|
||||
export type ExcelMatch = { rows: ExcelRow[]; units: Record<string, string> };
|
||||
|
||||
const EXCEL_SOURCE_FILES = [
|
||||
path.join(process.cwd(), 'data/source/high-voltage.xlsx'),
|
||||
path.join(process.cwd(), 'data/source/medium-voltage-KM.xlsx'),
|
||||
path.join(process.cwd(), 'data/source/low-voltage-KM.xlsx'),
|
||||
path.join(process.cwd(), 'data/source/solar-cables.xlsx'),
|
||||
path.join(process.cwd(), 'data/excel/high-voltage.xlsx'),
|
||||
path.join(process.cwd(), 'data/excel/medium-voltage-KM.xlsx'),
|
||||
path.join(process.cwd(), 'data/excel/low-voltage-KM.xlsx'),
|
||||
path.join(process.cwd(), 'data/excel/solar-cables.xlsx'),
|
||||
];
|
||||
|
||||
let EXCEL_INDEX: Map<string, ExcelMatch> | null = null;
|
||||
|
||||
Reference in New Issue
Block a user