forked from unimal-jp/spear-doc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspear.config.mjs
105 lines (102 loc) · 3.28 KB
/
spear.config.mjs
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
import fs from 'node:fs/promises'
import fm from 'front-matter'
import { remark } from 'remark';
import html from 'remark-html';
import escapeHtml from 'escape-html';
export default {
"projectName": "test",
"generateSitemap": false,
"generateComponents": true,
"plugins": [
(() => {
const generateHashFromString = (str) => {
let hash = 0;
if (str.length === 0) return hash;
for (let i = 0; i < str.length; i++) {
const chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
}
const generateField = (fieldName, fieldValue) => {
return {
id: generateHashFromString(fieldValue),
type: "field",
attributes: {
identifier: fieldName,
inputType: fieldName === "body" ? "rich_text" : "text",
value: fieldValue
}
}
}
const generateContent = (fields, contentId, fileCreatedAt, fileUpdatedAt) => {
const fieldsData = fields.map(field => {
return generateField(field.key, field.value)
});
return {
id: contentId,
type: "content",
values: {},
attributes: {
contentAlias: contentId,
createdAt: fileCreatedAt,
nextContentId: null,
patternName: "a",
previousContentId: null,
publicUid: contentId,
publishedAt: fileUpdatedAt,
updatedAt: fileUpdatedAt,
fields: {
data: fieldsData
}
}
}
}
return {
"pluginName": "markdown-client",
"configuration": null,
"beforeBuild": async (state, option) => {
try {
state.jsGenerator.injectFakeApiClient({
analytics: {
pageView: (params) => {
console.log("Unimplemented");
},
},
getList: (contentTypeId, params) => {
console.log("Unimplemented");
},
// Generate content from markdown files
getContent: async (contentTypeId, contentId, params) => {
const fileStat = await fs.stat(`./data/${contentTypeId}/${contentId}.mdx`)
const file = await fs.readFile(`./data/${contentTypeId}/${contentId}.mdx`)
const {attributes, body} = fm(file.toString())
const bodyHtml = await remark().use(html).process(body);
const fields = [];
for (const key of Object.keys(attributes)) {
fields.push({
key,
value: attributes[key]
})
}
fields.push({
key: "body",
value: bodyHtml.toString()
})
return generateContent(fields, contentId, fileStat.birthtime, fileStat.mtime)
},
getContentPreview: (contentTypeId, contentId, previewToken) => {
console.log("Unimplemented");
}
})
} catch (e) {
console.error(e)
}
},
"afterBuild": null,
"bundle": null,
}
})(),
]
};