-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
76 lines (56 loc) · 2.43 KB
/
server.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
const { compileMdx } = require('./lib/compileMdx.js')
const { minifyJs } = require("./lib/minifyJS.js")
const { getFrontmatter } = require('./lib/getFrontmatter.js')
const { getFunctionConstructorArguments } = require('./lib/getFunctionConstructorArguments.js')
function getConfig(init) {
const config = {}
config.components = typeof init === 'object' && init !== null ? { ...init.components } : {}
config.path = 'path' in init ? String(opts.path) : ''
config.remarkPlugins = Array.isArray(init.remarkPlugins) ? [ ...init.remarkPlugins] : []
config.rehypePlugins = Array.isArray(init.rehypePlugins) ? [ ...init.rehypePlugins] : []
return config
}
/** @type {import('./server').createMDXContentFromString} */
async function createMDXContentFromString(/** @type {string} */ content, /** @type {{}} */ init = null) {
content = content == null ? '' : String(content)
init = typeof init === 'object' && init || {}
const config = getConfig(init)
const parsed = getFrontmatter(content)
config.data = parsed.data
config.content = parsed.content
const compiled = await compileMdx(config)
const minified = await minifyJs(compiled)
return Object.assign({}, parsed.data, { '#': getFunctionConstructorArguments(minified) })
}
/** @type {import('./server').createMDXContentFromFile} */
async function createMDXContentFromFile(/** @type {string} */ path, /** @type {{}} */ init = null) {
const { readFile } = require('fs').promises
const content = await readFile(path, 'utf8')
const result = createMDXContentFromString(content, init)
result.path = path
return result
}
function fromString(/** @type {string} */ content) {
return getFrontmatter(content)
}
async function fromFile(/** @type {string} */ path) {
const { readFile } = require('fs').promises
const content = await readFile(path, 'utf8')
const result = getFrontmatter(content)
result.path = path
return fromString(content)
}
function fromFileSync(/** @type {string} */ path) {
const { readFileSync } = require('fs')
const content = readFileSync(path, 'utf8')
const result = getFrontmatter(content)
result.path = path
return fromString(content)
}
exports.createMDXContent = createMDXContentFromString
exports.createMDXContentFromString = createMDXContentFromString
exports.createMDXContentFromFile = createMDXContentFromFile
exports.readMDXDataFrom = fromString
exports.readMDXDataFromString = fromString
exports.readMDXDataFromFile = fromFile
exports.readMDXDataFromFileSync = fromFileSync