-
Notifications
You must be signed in to change notification settings - Fork 11
/
content-collections.ts
60 lines (54 loc) · 1.57 KB
/
content-collections.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
53
54
55
56
57
58
59
60
import { defineCollection, defineConfig } from '@content-collections/core';
import { compileMarkdown } from '@content-collections/markdown';
import remarkGfm from 'remark-gfm';
import { JSDOM } from 'jsdom';
function wrapTables(html: string) {
const dom = new JSDOM(`<!DOCTYPE html>${html}`, {
contentType: 'text/html',
});
// Select all table elements
const tables = Array.from(dom.window.document.getElementsByTagName('table'));
// Wrap each table with a div
tables.forEach((table) => {
const wrapper = dom.window.document.createElement('div');
wrapper.setAttribute('class', 'table-wrapper');
const parent = table.parentNode;
if (parent) {
parent.replaceChild(wrapper, table);
wrapper.appendChild(table);
}
});
return dom.serialize();
}
const posts = defineCollection({
name: 'posts',
directory: 'content/posts',
include: '**/*.md',
schema: (z) => ({
title: z.string(),
date: z.string(),
authors: z.array(
z.object({
name: z.string(),
social_url: z.string().optional(),
profile_image_url: z.string().optional(),
}),
),
}),
transform: async (document, context) => {
// @ts-expect-error -- TODO: fix this
const html = await compileMarkdown(context, document, {
remarkPlugins: [remarkGfm],
rehypePlugins: [],
});
return {
...document,
slug: document._meta.fileName.split('.')[0],
url: `/posts/${document._meta.fileName.split('.')[0]}`,
html: wrapTables(html),
};
},
});
export default defineConfig({
collections: [posts],
});