Skip to content

Commit e6005b8

Browse files
Merge pull request #248 from MaddyGuthridge/maddy-author-info
Include author info in more places
2 parents 77e21bb + b1888b3 commit e6005b8

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

src/lib/itemData.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import itemId, { type ItemId } from './itemId';
66
import type { ItemData } from './server/data/item';
7+
import type { AuthorInfo } from './server/data/item/item';
78

89
/**
910
* Return a descendant of the given item data, using the given relative ItemId
@@ -15,3 +16,16 @@ export function getDescendant(data: ItemData, id: ItemId) {
1516
return getDescendant(data.children[itemId.head(id)], itemId.tail(id));
1617
}
1718
}
19+
20+
/**
21+
* Return the author of an item. This also checks parent items for author info.
22+
*/
23+
export function getItemAuthor(item: ItemId, data: ItemData): AuthorInfo | null {
24+
for (const parent of itemId.ancestors(item)) {
25+
const p = getDescendant(data, parent);
26+
if (p.info.author) {
27+
return p.info.author;
28+
}
29+
}
30+
return null;
31+
}

src/lib/itemId.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ export function itemParent(itemId: ItemId): ItemId {
5050
return itemIdFromComponents(itemIdComponents(itemId).slice(0, -1));
5151
}
5252

53-
/** Returns all ancestors of the given item ID, including itself */
53+
/**
54+
* Returns all ancestors of the given item ID, starting with itself.
55+
*/
5456
export function itemAncestors(itemId: ItemId): ItemId[] {
5557
const ancestors = [itemId];
5658
while (itemId !== ROOT) {

src/routes/[...item]/feed.atom/+server.ts

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import itemId, { type ItemId } from '$lib/itemId';
22
import consts from '$lib/consts';
3-
import { getItemInfo, itemExists } from '$lib/server/data/item';
3+
import { getItemData, getItemInfo, itemExists } from '$lib/server/data/item';
44
import { itemFileUrl } from '$lib/urls';
55
import { error } from '@sveltejs/kit';
66
import { create } from 'xmlbuilder2';
7-
import type { AuthorInfo } from '$lib/server/data/item/item';
7+
import type { ItemData } from '$lib/server/data/item/item';
88
import { dev, version } from '$app/environment';
99
import { getConfig } from '$lib/server/data/config';
1010
import type { XMLBuilder } from 'xmlbuilder2/lib/interfaces';
1111
import { unixTime, unixToIsoTime } from '$lib/util';
12+
import { getItemAuthor } from '$lib/itemData';
1213

1314
type Request = import('./$types').RequestEvent;
1415

@@ -40,6 +41,8 @@ export async function GET(req: Request) {
4041
error(404, `Item ${item} does not have an Atom feed`);
4142
}
4243

44+
const portfolio = await getItemData(itemId.ROOT);
45+
4346
const baseUrl = `${req.url.protocol}//${req.url.host}`;
4447
const itemUrl = `${baseUrl}${item === '/' ? '' : item}`;
4548

@@ -65,7 +68,7 @@ export async function GET(req: Request) {
6568
// Add basic info
6669
root.ele('title').txt(info.feed.title);
6770
root.ele('id').txt(itemUrl);
68-
await addAuthorInfo(item, root);
71+
addAuthorInfo(item, root, portfolio);
6972

7073
// Feed subtitle -- use SEO description if possible
7174
root.ele('subtitle').txt(info.seo.description ?? info.description);
@@ -117,8 +120,7 @@ export async function GET(req: Request) {
117120
xmlChild.ele('summary').txt(childInfo.seo.description ?? childInfo.description);
118121

119122
// Author info
120-
// eslint-disable-next-line no-await-in-loop
121-
await addAuthorInfo(childId, xmlChild);
123+
addAuthorInfo(childId, xmlChild, portfolio);
122124

123125
// Content: link to site
124126
// xmlChild.ele('content').att('src', childUrl).att('type', consts.MIME_TYPES.HTML);
@@ -145,8 +147,8 @@ export async function GET(req: Request) {
145147
);
146148
}
147149

148-
async function addAuthorInfo(item: ItemId, node: XMLBuilder) {
149-
const authorInfo = await getFeedAuthor(item);
150+
function addAuthorInfo(item: ItemId, node: XMLBuilder, portfolio: ItemData) {
151+
const authorInfo = getItemAuthor(item, portfolio);
150152
if (authorInfo) {
151153
const xmlAuthor = node.ele('author');
152154
if (authorInfo.name) {
@@ -160,16 +162,3 @@ async function addAuthorInfo(item: ItemId, node: XMLBuilder) {
160162
}
161163
}
162164
}
163-
164-
async function getFeedAuthor(item: ItemId): Promise<AuthorInfo | null> {
165-
for (const ancestor of itemId.ancestors(item)) {
166-
// Really should look this info up in parallel
167-
// At least it'll be better once I cache the data again...
168-
// eslint-disable-next-line no-await-in-loop
169-
const info = await getItemInfo(ancestor);
170-
if (info.author) {
171-
return info.author;
172-
}
173-
}
174-
return null;
175-
}

0 commit comments

Comments
 (0)