-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexternal-references.ts
48 lines (40 loc) · 1.41 KB
/
external-references.ts
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
import { join } from 'path';
import type { Article } from '../data/content-types';
import { getAllContentFromDirectory } from './content-loaders/getAllContentFromDirectory';
import { getContentBySlug } from './content-loaders/getContentBySlug';
import { getContentSlugsForTag } from './tags';
// directory reference to `src/content/external-references`
const externalReferencesDirectory = join(
process.cwd(),
'src',
'data',
'external-references'
);
const EXTERNAL_REFERENCES_CONTENT_TYPE = 'article';
export const getExternalReferenceBySlug = async (slug: string) => {
const reference = await getContentBySlug(
slug,
externalReferencesDirectory,
EXTERNAL_REFERENCES_CONTENT_TYPE
);
return reference;
};
export const getAllExternalReferences = async () => {
let articles = (await getAllContentFromDirectory(
externalReferencesDirectory,
EXTERNAL_REFERENCES_CONTENT_TYPE
)) as Article[];
// filter out articles that don't have a slug
articles = articles?.filter((article) => article.frontmatter?.slug);
return articles;
};
export const getAllExternalReferencesByTag = async (tag: string) => {
try {
const refs = await getAllExternalReferences();
const slugsForTag = await getContentSlugsForTag(tag);
return refs.filter((article) => slugsForTag.includes(article.slug));
} catch (error) {
console.error('Error getting external references by tag:', error);
return [];
}
};