forked from heyform/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontents.ts
52 lines (43 loc) · 1.21 KB
/
contents.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
49
50
51
52
import type { MarkdownNode } from '@nuxt/content';
import { serverQueryContent } from '#content/server';
function extractTextFromAst(node: MarkdownNode, ignoredTags: string[] = []) {
let text = '';
// Get text from markdown AST
if (node.type === 'text') {
text += (node.value || '');
}
// Do not explore children
if (ignoredTags.includes(node.tag ?? '')) {
return ''
}
// Explore children
if (node.children?.length) {
text += node.children.map((child: any) => extractTextFromAst(child, ignoredTags)).filter(Boolean).join('');
}
return text;
}
export default cachedEventHandler(async (event) => {
const files = await serverQueryContent(event)
.sort({
_stem: 1,
})
.find();
const categories = files.filter(row => row._partial).map(row => ({
id: row._dir,
title: row.title,
}));
const list = files.filter(row => !row._partial).map(row => ({
id: row._path,
categoryId: row._dir,
title: row.title,
description: (row.description || '').trim(),
content: row.body ? extractTextFromAst(row.body, ['pre', 'code', 'iframe', 'img', 'video']) : '',
}));
return {
categories,
list,
};
}, {
maxAge: 60 * 60,
getKey: () => '/api/contents',
});