Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

Commit 2d849e5

Browse files
committed
fix: sitemap / feed
1 parent dce8cef commit 2d849e5

File tree

4 files changed

+27
-49
lines changed

4 files changed

+27
-49
lines changed

packages/karbon/src/runtime/api/feed.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { gql } from '@apollo/client/core/index.js'
22
import { useStoripressClient } from '../composables/storipress-client'
3-
import type { TypesenseFilter } from '../composables/typesense-client'
4-
import { listArticles } from './article'
53

64
const GetDesk = gql`
75
query GetDesk($slug: String) {
@@ -23,8 +21,9 @@ const GetDesk = gql`
2321
}
2422
`
2523

26-
export function listFeedArticles(filter: TypesenseFilter = {}) {
27-
return listArticles(filter)
24+
export async function listFeedArticles() {
25+
const allArticles = (await $fetch('/_storipress/posts/__all.json')) ?? []
26+
return allArticles
2827
}
2928

3029
export async function getDeskWithSlug(slug: string) {

packages/karbon/src/runtime/api/normalize-article.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export interface RawArticleLike {
2424
plaintext: string
2525
plan: ArticlePlan
2626
authors: RawUserLike[]
27+
published_at: string
2728
}
2829

2930
export interface PaidContent {

packages/karbon/src/runtime/api/sitemap.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { identity } from 'remeda'
33
import { createStoripressClient } from '../composables/storipress-client'
44
import { storipressConfigCtx } from '../composables/storipress-base-client'
55
import type { ModuleRuntimeConfig } from '../types'
6-
import { PER_PAGE, getSearchQuery, useTypesenseClient } from '../composables/typesense-client'
76

87
const ListArticles = gql`
98
query ListArticles($page: Int!) {
@@ -130,25 +129,13 @@ export const payloadScopes: ResourceScope[] = [
130129
export async function getResources(runtimeConfig?: ModuleRuntimeConfig['storipress']) {
131130
runtimeConfig && storipressConfigCtx.set(runtimeConfig)
132131
const client = createStoripressClient()
133-
const typesenseClient = useTypesenseClient()
134132

135133
const result = await Promise.all(
136134
payloadScopes.map(async ({ payloadScope, query, queryKey, filter = identity }) => {
137135
let resources: any = []
138136
switch (payloadScope) {
139137
case 'posts': {
140-
const documents = typesenseClient?.collections('articles').documents()
141-
142-
let hasMore = true
143-
let page = 1
144-
while (hasMore) {
145-
const searchResult = await documents?.search(getSearchQuery(page), {})
146-
const currentPageArticles = searchResult?.hits?.map(({ document }) => document) ?? []
147-
resources.push(...currentPageArticles)
148-
149-
hasMore = searchResult.found > searchResult.page * PER_PAGE
150-
page = searchResult.page + 1
151-
}
138+
resources = (await $fetch('/_storipress/posts/__all.json')) ?? []
152139
break
153140
}
154141
default: {

packages/karbon/src/runtime/routes/atom-desk.xml.ts

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { defineEventHandler, sendNoContent, setHeader } from 'h3'
22
import { Feed } from 'feed'
33
import { encodePath, joinURL, withTrailingSlash } from 'ufo'
44
import path from 'pathe'
5-
import { getDeskWithSlug, listFeedArticles } from '@storipress/karbon/internal'
5+
import { getDeskWithSlug } from '@storipress/karbon/internal'
66
import type { Author } from '../composables/page-meta'
7+
import { listArticles } from '../api/article'
78
import { useRuntimeConfig } from '#imports'
89
import urls from '#sp-internal/storipress-urls.mjs'
910

@@ -37,47 +38,37 @@ export default defineEventHandler(async (e) => {
3738

3839
const deskIds: string[] = desk.desks?.map(({ id }: { id: string }) => id) ?? []
3940

40-
type Filter = Record<'desk' | 'desk_ids', string | string[]>
41-
const filter = {} as Filter
42-
if (deskIds.length !== 0) {
43-
filter.desk_ids = deskIds
44-
} else {
45-
filter.desk = desk.id
46-
}
47-
4841
const runtimeConfig = useRuntimeConfig()
49-
const articles = await listFeedArticles(filter)
42+
const articles = await listArticles({ desk_ids: deskIds })
5043

51-
const siteUrl = runtimeConfig.public.siteUrl
44+
const siteUrl = runtimeConfig.public.siteUrl as string
5245
const feed = new Feed({
53-
id: withTrailingSlash(runtimeConfig.public.siteUrl),
54-
link: withTrailingSlash(runtimeConfig.public.siteUrl),
55-
title: runtimeConfig.public.siteName,
56-
description: runtimeConfig.public.siteDescription,
46+
id: withTrailingSlash(runtimeConfig.public.siteUrl as string),
47+
link: withTrailingSlash(runtimeConfig.public.siteUrl as string),
48+
title: runtimeConfig.public.siteName as string,
49+
description: runtimeConfig.public.siteDescription as string,
5750
updated: new Date(),
5851
feedLinks: {
5952
atom: joinURL(siteUrl, `/atom/${fileName}`),
6053
},
6154
copyright: ${runtimeConfig.public.siteName} ${new Date().getFullYear()} All Rights Reserved`,
6255
})
6356

64-
articles
65-
.filter((article: TArticle) => article.published_at)
66-
.forEach((article: TArticle) => {
67-
const id = encodePath(urls.article.toURL(article, urls.article._context))
68-
feed.addItem({
69-
title: article.title,
70-
id: joinURL(siteUrl, id),
71-
link: joinURL(siteUrl, id),
72-
description: article.plaintext.slice(0, 120),
73-
date: new Date(article.published_at),
74-
author:
75-
article.author?.map((author) => ({
76-
name: author.name,
77-
})) || [],
78-
content: article.html,
79-
})
57+
articles.forEach((article) => {
58+
const id = encodePath(urls.article.toURL(article, urls.article._context))
59+
feed.addItem({
60+
title: article.title,
61+
id: joinURL(siteUrl, id),
62+
link: joinURL(siteUrl, id),
63+
description: article.plaintext.slice(0, 120),
64+
date: new Date(article.published_at),
65+
author:
66+
article.authors?.map((author) => ({
67+
name: author.name,
68+
})) || [],
69+
content: article.html,
8070
})
71+
})
8172

8273
return feed.atom1()
8374
})

0 commit comments

Comments
 (0)