-
Notifications
You must be signed in to change notification settings - Fork 1
/
sourcebit.js
83 lines (77 loc) · 3.71 KB
/
sourcebit.js
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
const path = require('path');
const { flattenMarkdownData, cssClassesFromFilePath, cssClassesFromUrlPath, urlPathFromFilePath } = require('./src/utils/page-utils');
const isDev = process.env.NODE_ENV === 'development';
module.exports = {
plugins: [
/**
* The `sourcebit-source-filesystem` plugin reads content files from the provided `options.sources`,
* and generates array of objects that are passed to subsequent plugins.
*/
{
module: require('sourcebit-source-filesystem'),
options: {
watch: isDev,
sources: [
{ name: 'pages', path: path.join(__dirname, 'content/pages') },
{ name: 'data', path: path.join(__dirname, 'content/data') }
]
}
},
/**
* converts { __metadata, frontmatter, markdown }
* to { __metadata, ...frontmater, markdown_content: markdown }
*/
flattenMarkdownData(),
/**
* The `sourcebit-target-next` plugin receives objects generated by `sourcebit-source-filesystem` plugin,
* and generates new data that is consumed by Next.js `getStaticPaths` and `getStaticProps` methods.
* The generated data is cached and stored inside `.sourcebit-nextjs-cache.json` file.
*
* The generated data is an object with three properties:
* - objects: Array of objects representing all content files loaded by the `sourcebit-source-filesystem` plugin.
* - pages: Array of objects representing site pages props. This array is generated by the `pages()` method.
* - props: Object with common props that will be merged with props of every page. This object is generated by
* the `commonProps()` method.
*/
{
module: require('sourcebit-target-next'),
options: {
liveUpdate: isDev,
flattenAssetUrls: true,
commonProps: (objects) => {
const site = objects.find((page) => page.__metadata.id === 'content/data/config.json');
return { site };
},
pages: (objects) => {
const personObjects = objects.filter((object) => object.__metadata.relProjectPath?.startsWith('content/data/team/') && !!object.slug);
const personPages = personObjects.map((person) => {
const { __metadata, ...restProps } = person;
const urlPath = `/blog/author/${person.slug}`;
return {
__metadata: {
...__metadata,
urlPath,
pageCssClasses: cssClassesFromUrlPath(urlPath)
},
...restProps
};
});
const pageObjects = objects.filter((page) => page.__metadata.sourceName === 'pages');
const pages = pageObjects.map((page) => {
const { __metadata, ...restProps } = page;
const urlPath = urlPathFromFilePath(page.__metadata.relSourcePath);
return {
__metadata: {
...__metadata,
urlPath,
pageCssClasses: cssClassesFromFilePath(page.__metadata.relSourcePath)
},
...restProps
};
});
return [...pages, ...personPages];
}
}
}
]
};