Skip to content

add author and html to rss #455

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

Merged
merged 1 commit into from
May 19, 2023
Merged
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
36 changes: 16 additions & 20 deletions apps/codingcatdev/src/lib/server/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Content, Course } from '$lib/types';
import { env } from '$env/dynamic/private';
import { fileURLToPath } from 'url';
import { opendirSync } from "fs";
import type { SvelteComponentTyped, ComponentType } from 'svelte';

const LIMIT = 20;

Expand All @@ -29,45 +30,33 @@ export const getRootPath = (contentType: ContentType, courseDir?: string) => {
return root;
}

export const getContentTypeDirectory = async <T>(contentType: ContentType, courseDir?: string) => {
export const getContentTypeDirectory = async <T>(contentType: ContentType, courseDir?: string, render = false) => {
const contentList: T[] = [];
const dirs = opendirSync(getRootPath(contentType, courseDir));
for await (const dir of dirs) {
if (dir.isFile()) continue;
const parsed = await parseContentType<T>(`${getRootPath(contentType, courseDir)}/${dir.name}/${suffix}`) as T;
const parsed = await parseContentType<T>(`${getRootPath(contentType, courseDir)}/${dir.name}/${suffix}`, render) as T;
contentList.push(parsed);
}
return contentList;
}

export const getContentTypePath = async <T>(contentType: ContentType, path: string, courseDir?: string) => {
export const getContentTypePath = async <T>(contentType: ContentType, path: string, courseDir?: string, render = false) => {
const root = getRootPath(contentType, courseDir);
return await parseContentType<T>(`${root}/${path}/${suffix}`) as T;
return await parseContentType<T>(`${root}/${path}/${suffix}`, render) as T;
}

export const parseContentType = (async <T>(path: string) => {
// If we are in production everything has already been compiled to JavaScript
// and we can just read the metadata, otherwise compile and read.
// let frontmatter;
// let html;
// if (prod) {
const { metadata } = await import(path);
export const parseContentType = (async <T>(path: string, render = false) => {
const { metadata, default: page } = await import(path);
const frontmatter = metadata;
// } else {
// console.log('READ PATH', path);
// const md = readFileSync(path, 'utf8');
// const transformed = await compile(md);
// html = transformed?.code;
// frontmatter = transformed?.data?.fm as Content & Podcast | undefined;
// }
// TODO: Add more checks?

// TODO: Add more checks?
if (!frontmatter?.type) {
console.error('Missing Frontmatter details', path);
return;
}

const content = {
let content = {
...frontmatter,
cover: frontmatter?.cover ? decodeURI(frontmatter?.cover) : '',
type: frontmatter?.type as ContentType,
Expand All @@ -77,6 +66,13 @@ export const parseContentType = (async <T>(path: string) => {
slug: path.split('/').at(-2)
};

if (render) {
content = {
...content,
html: page?.render()?.html
}
}

if (frontmatter.type === ContentType.course) {
const lesson = (await listContent<Content>({
contentItems: await getContentTypeDirectory<Content>(ContentType.lesson, frontmatter.slug),
Expand Down
31 changes: 31 additions & 0 deletions apps/codingcatdev/src/routes/(rss)/feed-blog.json/+server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { getContentTypeDirectory, listContent } from '$lib/server/content';
import { ContentType, type Content, type Author } from '$lib/types';
import { buildFeed } from '../rss';


const contentType = ContentType.post;

/** @type {import('./$types').RequestHandler} */
export const GET = async () => {
const contentItems = (await listContent<Content>({
contentItems: await getContentTypeDirectory<Content>(contentType, undefined, true),
limit: 10000
})).content

const authorItems = (await listContent<Author>({
contentItems: await getContentTypeDirectory<Author>(ContentType.author),
limit: 10000
})).content

//xml rss feed response
return new Response(
buildFeed({
contentType, contents: contentItems, authorItems
}).json1(),
{
headers: {
'content-type': 'application/json', 'cache-control': 'max-age=0, s-maxage=3600',
},
}
)
}
11 changes: 8 additions & 3 deletions apps/codingcatdev/src/routes/(rss)/feed-blog.xml/+server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getContentTypeDirectory, listContent } from '$lib/server/content';
import { ContentType, type Content } from '$lib/types';
import { ContentType, type Content, type Author } from '$lib/types';
import { buildFeed } from '../rss';


Expand All @@ -8,14 +8,19 @@ const contentType = ContentType.post;
/** @type {import('./$types').RequestHandler} */
export const GET = async () => {
const contentItems = (await listContent<Content>({
contentItems: await getContentTypeDirectory<Content>(ContentType.post),
contentItems: await getContentTypeDirectory<Content>(contentType, undefined, true),
limit: 10000
})).content

const authorItems = (await listContent<Author>({
contentItems: await getContentTypeDirectory<Author>(ContentType.author),
limit: 10000
})).content

//xml rss feed response
return new Response(
buildFeed({
contentType, contents: (await listContent({ contentItems, limit: 10000 })).content
contentType, contents: contentItems, authorItems
}).rss2(),
{
headers: {
Expand Down
31 changes: 31 additions & 0 deletions apps/codingcatdev/src/routes/(rss)/feed-courses.json/+server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { getContentTypeDirectory, listContent } from '$lib/server/content';
import { ContentType, type Content, type Author } from '$lib/types';
import { buildFeed } from '../rss';


const contentType = ContentType.course;

/** @type {import('./$types').RequestHandler} */
export const GET = async () => {
const contentItems = (await listContent<Content>({
contentItems: await getContentTypeDirectory<Content>(contentType, undefined, true),
limit: 10000
})).content

const authorItems = (await listContent<Author>({
contentItems: await getContentTypeDirectory<Author>(ContentType.author),
limit: 10000
})).content

//xml rss feed response
return new Response(
buildFeed({
contentType, contents: contentItems, authorItems
}).json1(),
{
headers: {
'content-type': 'application/json', 'cache-control': 'max-age=0, s-maxage=3600',
},
}
)
}
14 changes: 10 additions & 4 deletions apps/codingcatdev/src/routes/(rss)/feed-courses.xml/+server.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import { getContentTypeDirectory, listContent } from '$lib/server/content';
import { ContentType, type Content } from '$lib/types';
import { ContentType, type Content, type Author } from '$lib/types';
import { buildFeed } from '../rss';


const contentType = ContentType.post;
const contentType = ContentType.course;

/** @type {import('./$types').RequestHandler} */
export const GET = async () => {
const contentItems = (await listContent<Content>({
contentItems: await getContentTypeDirectory<Content>(ContentType.course),
contentItems: await getContentTypeDirectory<Content>(contentType, undefined, true),
limit: 10000
})).content

const authorItems = (await listContent<Author>({
contentItems: await getContentTypeDirectory<Author>(ContentType.author),
limit: 10000
})).content

//xml rss feed response
return new Response(
buildFeed({
contentType, contents: (await listContent({ contentItems, limit: 10000 })).content
contentType, contents: contentItems, authorItems
}).rss2(),
{
headers: {
Expand Down
31 changes: 31 additions & 0 deletions apps/codingcatdev/src/routes/(rss)/feed-podcasts.json/+server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { getContentTypeDirectory, listContent } from '$lib/server/content';
import { ContentType, type Content, type Author } from '$lib/types';
import { buildFeed } from '../rss';


const contentType = ContentType.podcast;

/** @type {import('./$types').RequestHandler} */
export const GET = async () => {
const contentItems = (await listContent<Content>({
contentItems: await getContentTypeDirectory<Content>(contentType, undefined, true),
limit: 10000
})).content

const authorItems = (await listContent<Author>({
contentItems: await getContentTypeDirectory<Author>(ContentType.author),
limit: 10000
})).content

//xml rss feed response
return new Response(
buildFeed({
contentType, contents: contentItems, authorItems
}).json1(),
{
headers: {
'content-type': 'application/json', 'cache-control': 'max-age=0, s-maxage=3600',
},
}
)
}
13 changes: 9 additions & 4 deletions apps/codingcatdev/src/routes/(rss)/feed-podcasts.xml/+server.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import { getContentTypeDirectory, listContent } from '$lib/server/content';
import { ContentType, type Content } from '$lib/types';
import { ContentType, type Content, type Author } from '$lib/types';
import { buildFeed } from '../rss';


const contentType = ContentType.post;
const contentType = ContentType.podcast;

/** @type {import('./$types').RequestHandler} */
export const GET = async () => {
const contentItems = (await listContent<Content>({
contentItems: await getContentTypeDirectory<Content>(ContentType.podcast),
contentItems: await getContentTypeDirectory<Content>(contentType, undefined, true),
limit: 10000
})).content

const authorItems = (await listContent<Author>({
contentItems: await getContentTypeDirectory<Author>(ContentType.author),
limit: 10000
})).content

//xml rss feed response
return new Response(
buildFeed({
contentType, contents: (await listContent({ contentItems, limit: 10000 })).content
contentType, contents: contentItems, authorItems
}).rss2(),
{
headers: {
Expand Down
25 changes: 16 additions & 9 deletions apps/codingcatdev/src/routes/(rss)/rss.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import type { Content, ContentType } from "$lib/types";
import { Feed } from 'feed';
import type { Content, ContentType, Author } from "$lib/types";
import { Feed, type Author as FeedAuthor } from 'feed';

const site = 'https://codingcat.dev';


export const buildFeed = ({
contents,
contentType,
authorItems,
}: {
contents: Content[];
contentType: ContentType;
authorItems: Author[];
}) => {
const feed = new Feed({
title: `${site} - ${contentType} feed`,
Expand All @@ -32,22 +34,27 @@ export const buildFeed = ({
});

for (const content of contents) {

const authors = content?.authors?.map((authorSlug) => {
return authorItems.filter(a => a.slug === authorSlug).map(a => {
return {
name: a.name,
link: `${site}/author/${a.slug}`,
}
})?.at(0) as FeedAuthor;
}).filter(a => a !== undefined)
feed.addItem({
title: content.title || '',
content: content.html,
link: `${site}/${contentType}/${content.slug}`,
description: `${content.excerpt}`,
image: content.cover || feed.items.at(0)?.image,
date: content.start || new Date(),
author: content?.authors ? content.authors?.map((author) => {
return {
name: author.name,
link: `${site}/authors/${author.slug}`,
};
})
author: authors ? authors
: [{
name: 'Alex Patterson',
email: 'alex@codingcat.dev',
link: `${site}`,
link: `${site}/author/alex-patterson`,
}],
});
}
Expand Down