Skip to content

Commit

Permalink
okay well it works!
Browse files Browse the repository at this point in the history
  • Loading branch information
Adammatthiesen committed Oct 17, 2024
1 parent ca46ec1 commit 8a72429
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 7 deletions.
Binary file added local.db
Binary file not shown.
Binary file added local.db-shm
Binary file not shown.
Binary file added local.db-wal
Binary file not shown.
3 changes: 2 additions & 1 deletion packages/studiocms_devapps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"cheerio": "^1.0.0",
"@types/cheerio": "^0.22.35",
"turndown": "^7.2.0",
"@types/turndown": "^5.0.5"
"@types/turndown": "^5.0.5",
"html-entities": "^2.5.2"
},
"peerDependencies": {
"@astrojs/db": "catalog:min",
Expand Down
6 changes: 4 additions & 2 deletions packages/studiocms_devapps/src/routes/wp-api-importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const POST: APIRoute = async ({ request }: APIContext) => {
});
}

if (typeof url !== 'string' || typeof type !== 'string' || typeof useBlogPlugin !== 'string') {
if (typeof url !== 'string' || typeof type !== 'string') {
return new Response(null, {
status: 400,
statusText: 'Bad Request',
Expand All @@ -32,12 +32,14 @@ export const POST: APIRoute = async ({ request }: APIContext) => {
});
}

const useBlogPluginValue = useBlogPlugin === 'true';

switch (type) {
case 'pages':
await importPagesFromWPAPI(url);
break;
case 'posts':
await importPostsFromWPAPI(url, useBlogPlugin === 'true');
await importPostsFromWPAPI(url, useBlogPluginValue);
break;
default:
throw new Error('Invalid import type');
Expand Down
7 changes: 3 additions & 4 deletions packages/studiocms_devapps/src/utils/wp-api/converters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'node:path';
import Config from 'virtual:studiocms-devapps/wp-api/configPath';
import { decode } from 'html-entities';
import TurndownService from 'turndown';
import type { Page, Post } from '../../schema/wp-api';
import type { PageContent, PageData } from './index';
Expand All @@ -16,12 +17,11 @@ export const ConvertToPageData = (page: unknown): PageData => {
const pageData: PageData = {
id: crypto.randomUUID(),
title: data.title.rendered,
description: stripHtml(data.excerpt.rendered),
description: decode(stripHtml(data.excerpt.rendered)),
slug: data.slug,
publishedAt: new Date(data.date_gmt),
updatedAt: new Date(data.modified_gmt),
showOnNav: false,
heroImage: '',
contentLang: 'default',
package: 'studiocms',
};
Expand Down Expand Up @@ -68,12 +68,11 @@ export const ConvertToPostData = (post: unknown, useBlogPkg: boolean): PageData
const pageData: PageData = {
id: crypto.randomUUID(),
title: data.title.rendered,
description: stripHtml(data.excerpt.rendered),
description: decode(stripHtml(data.excerpt.rendered)),
slug: data.slug,
publishedAt: new Date(data.date_gmt),
updatedAt: new Date(data.modified_gmt),
showOnNav: false,
heroImage: '',
contentLang: 'default',
package: pkg,
};
Expand Down
9 changes: 9 additions & 0 deletions packages/studiocms_devapps/src/utils/wp-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ export const importPagesFromWPAPI = async (endpoint: string) => {
const url = apiEndpoint(endpoint, 'pages');
const pages: Page[] = await fetchAll(url);

console.log('pages', pages.length);

try {
for (const page of pages) {
console.log('importing page:', page.title.rendered);
await importPage(page);
}
} catch (error) {
Expand Down Expand Up @@ -94,10 +97,16 @@ const importPost = async (post: unknown, useBlogPkg: boolean) => {

export const importPostsFromWPAPI = async (endpoint: string, useBlogPkg: boolean) => {
const url = apiEndpoint(endpoint, 'posts');

console.log('fetching posts from:', url.origin);

const posts: Page[] = await fetchAll(url);

console.log('posts', posts.length);

try {
for (const post of posts) {
console.log('importing post:', post.title.rendered);
await importPost(post, useBlogPkg);
}
} catch (error) {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8a72429

Please sign in to comment.