Repository navigation
Expand file tree
/
Copy pathhashnode.ts
More file actions
138 lines (124 loc) · 3.42 KB
/
Copy pathhashnode.ts
File metadata and controls
138 lines (124 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import type { Loader } from 'astro/loaders';
const ENDPOINT = 'https://gql.hashnode.com';
const POSTS_QUERY = /* GraphQL */ `
query PublicationPosts($host: String!, $first: Int!, $after: String) {
publication(host: $host) {
posts(first: $first, after: $after) {
edges {
node {
id
slug
title
subtitle
brief
publishedAt
updatedAt
coverImage {
url
}
tags {
name
slug
}
content {
markdown
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
`;
type HashnodePost = {
id: string;
slug: string;
title: string;
subtitle: string | null;
brief: string | null;
publishedAt: string;
updatedAt: string | null;
coverImage: { url: string } | null;
tags: { name: string; slug: string }[] | null;
content: { markdown: string } | null;
};
type Options = {
host: string;
pageSize?: number;
};
export function hashnodeLoader({ host, pageSize = 20 }: Options): Loader {
return {
name: 'hashnode',
async load({ store, logger, parseData, generateDigest, renderMarkdown }) {
store.clear();
let after: string | null = null;
let page = 0;
let total = 0;
try {
while (true) {
page++;
const res = await fetch(ENDPOINT, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
query: POSTS_QUERY,
variables: { host, first: pageSize, after },
}),
});
if (!res.ok) {
throw new Error(`Hashnode API ${res.status}: ${await res.text()}`);
}
const json = (await res.json()) as {
data?: { publication: { posts: { edges: { node: HashnodePost }[]; pageInfo: { hasNextPage: boolean; endCursor: string | null } } } | null };
errors?: { message: string }[];
};
if (json.errors?.length) {
throw new Error(`Hashnode GraphQL errors: ${json.errors.map((e) => e.message).join('; ')}`);
}
const publication = json.data?.publication;
if (!publication) {
logger.warn(`No Hashnode publication found for host "${host}"`);
return;
}
const { edges, pageInfo } = publication.posts;
for (const { node } of edges) {
const body = node.content?.markdown ?? '';
const data = await parseData({
id: node.slug,
data: {
title: node.title,
description: node.subtitle?.trim() || node.brief?.trim() || '',
pubDate: new Date(node.publishedAt),
updatedDate: node.updatedAt ? new Date(node.updatedAt) : undefined,
heroImage: node.coverImage?.url,
tags: node.tags?.map((t) => t.name) ?? [],
hashnodeId: node.id,
},
});
const rendered = await renderMarkdown(body);
store.set({
id: node.slug,
data,
body,
rendered,
digest: generateDigest({ data, body }),
});
total++;
}
if (!pageInfo.hasNextPage) break;
after = pageInfo.endCursor;
}
logger.info(`Loaded ${total} Hashnode post${total === 1 ? '' : 's'} from "${host}" (${page} page${page === 1 ? '' : 's'})`);
} catch (err) {
// Don't fail the whole site build when the blog backend is flaky.
// The blog collection will be empty for this build; the next
// successful sync repopulates it.
const msg = err instanceof Error ? err.message : String(err);
logger.warn(`Hashnode load failed (${msg}). Skipping blog content for this build.`);
}
},
};
}