host-mdx 2.1.1 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -11
- package/index.js +20 -17
- package/mdx-to-html.js +9 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
# host-mdx
|
|
2
|
-
[](https://www.npmjs.com/package/host-mdx)
|
|
3
|
-
A cli tool to create and serve a static html website from a given mdx directory
|
|
4
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/host-mdx)\
|
|
4
|
+
A cli tool to create and serve a static html website from a given mdx directory
|
|
5
5
|
|
|
6
6
|
## 🛠️ Usage
|
|
7
|
+
|
|
7
8
|
```
|
|
8
9
|
host-mdx [options]
|
|
9
10
|
|
|
@@ -16,35 +17,45 @@ Options:
|
|
|
16
17
|
--track-changes, -t Tracks any changes made & auto reloads
|
|
17
18
|
--verobse, -v Shows additional log messages
|
|
18
19
|
```
|
|
19
|
-
|
|
20
|
-
> If `--input-path` is not provided it will default to `./` i.e. current working directory
|
|
21
|
-
> If `--output-path` is not provided a temp folder will be created automatically & deleted upon exit
|
|
22
20
|
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
> If `--input-path` is not provided it will default to `./` i.e. current working directory\
|
|
22
|
+
> If `--output-path` is not provided a temp folder will be created automatically & deleted upon exit
|
|
25
23
|
|
|
24
|
+
You can add a file by the name `.hostmdxignore` at the root of your project to filter out which files/folders to skip while generating html
|
|
25
|
+
(similar to [.gitignore](https://git-scm.com/docs/gitignore))
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
You can also add a file by the name `host-mdx.js` at the root of your input folder as a config file with access to the following:
|
|
28
28
|
|
|
29
29
|
```js
|
|
30
30
|
onSiteCreateStart(inputPath, outputPath)
|
|
31
31
|
onSiteCreateEnd(inputPath, outputPath, wasInterrupted)
|
|
32
|
-
onFileCreateStart(
|
|
33
|
-
onFileCreateEnd(
|
|
32
|
+
onFileCreateStart(inputPath, outputPath, inFilePath, outFilePath)
|
|
33
|
+
onFileCreateEnd(inputPath, outputPath, inFilePath, outFilePath, result)
|
|
34
34
|
modBundleMDXSettings(inputPath, outputPath, settings)
|
|
35
|
+
modGlobalArgs(inputPath, outputPath, globalArgs)
|
|
35
36
|
toTriggerRecreate(event, path)
|
|
36
37
|
```
|
|
37
38
|
|
|
38
39
|
> **Note:** Any changes made to `host-mdx.js` or any new package added requires complete restart otherwise changes will not reflect due to [this bug](https://github.com/nodejs/node/issues/49442)
|
|
39
40
|
|
|
41
|
+
Default global variables you can use inside any .mdx files:
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
hostmdxCwd
|
|
45
|
+
hostmdxInputPath
|
|
46
|
+
hostmdxOutputPath
|
|
47
|
+
```
|
|
40
48
|
|
|
41
49
|
## 📖 Example
|
|
50
|
+
|
|
42
51
|
Command:
|
|
52
|
+
|
|
43
53
|
```bash
|
|
44
54
|
npx host-mdx --input-path="path/to/my-website-template" --output-path="path/to/my-website" --port=3113 -t
|
|
45
55
|
```
|
|
46
56
|
|
|
47
57
|
Input Directory:
|
|
58
|
+
|
|
48
59
|
```
|
|
49
60
|
my-website-template/
|
|
50
61
|
├─ index.mdx
|
|
@@ -68,6 +79,7 @@ my-website-template/
|
|
|
68
79
|
```
|
|
69
80
|
|
|
70
81
|
`.hostmdxignore` file content:
|
|
82
|
+
|
|
71
83
|
```sh
|
|
72
84
|
*.jsx
|
|
73
85
|
blog/page2/
|
|
@@ -76,6 +88,7 @@ static/temp.jpg
|
|
|
76
88
|
```
|
|
77
89
|
|
|
78
90
|
`host-mdx.js` file content:
|
|
91
|
+
|
|
79
92
|
```js
|
|
80
93
|
export function onSiteCreateStart(inputPath, outputPath) {
|
|
81
94
|
console.log("onSiteCreateStart", inputPath, outputPath)
|
|
@@ -110,6 +123,7 @@ export function toTriggerRecreate(event, path) {
|
|
|
110
123
|
```
|
|
111
124
|
|
|
112
125
|
Output Directory:
|
|
126
|
+
|
|
113
127
|
```
|
|
114
128
|
my-website/
|
|
115
129
|
├─ index.html
|
|
@@ -127,6 +141,6 @@ my-website/
|
|
|
127
141
|
|
|
128
142
|
The site will now be visible in the browser at `localhost:3113`
|
|
129
143
|
|
|
130
|
-
|
|
131
144
|
## 🔑 License
|
|
145
|
+
|
|
132
146
|
MIT © [Manas Ravindra Makde](https://manasmakde.github.io/)
|
package/index.js
CHANGED
|
@@ -141,7 +141,7 @@ async function createSite(inputPath, outputPath) {
|
|
|
141
141
|
|
|
142
142
|
// Broadcast site creation started
|
|
143
143
|
log("Creating site...")
|
|
144
|
-
configs?.onSiteCreateStart?.(inputPath, outputPath)
|
|
144
|
+
await configs?.onSiteCreateStart?.(inputPath, outputPath)
|
|
145
145
|
|
|
146
146
|
|
|
147
147
|
// Remove html folder if it already exists
|
|
@@ -182,9 +182,9 @@ async function createSite(inputPath, outputPath) {
|
|
|
182
182
|
// Make dir
|
|
183
183
|
if (isDir) {
|
|
184
184
|
log(`${currentPath} ---> ${absToOutput}`, true)
|
|
185
|
-
configs?.onFileCreateStart?.(currentPath, absToOutput)
|
|
185
|
+
await configs?.onFileCreateStart?.(inputPath, outputPath, currentPath, absToOutput)
|
|
186
186
|
fs.mkdirSync(absToOutput, { recursive: true });
|
|
187
|
-
configs?.onFileCreateEnd?.(currentPath, absToOutput)
|
|
187
|
+
await configs?.onFileCreateEnd?.(inputPath, outputPath, currentPath, absToOutput, undefined)
|
|
188
188
|
}
|
|
189
189
|
// Make html file from mdx
|
|
190
190
|
else if (!isDir && isMdx) {
|
|
@@ -192,7 +192,7 @@ async function createSite(inputPath, outputPath) {
|
|
|
192
192
|
// Broadcast file creation started
|
|
193
193
|
let absHtmlPath = path.format({ ...path.parse(absToOutput), base: '', ext: '.html' })
|
|
194
194
|
log(`${currentPath} ---> ${absHtmlPath}`, true)
|
|
195
|
-
configs?.onFileCreateStart?.(currentPath, absHtmlPath)
|
|
195
|
+
await configs?.onFileCreateStart?.(inputPath, outputPath, currentPath, absHtmlPath, undefined)
|
|
196
196
|
|
|
197
197
|
|
|
198
198
|
// convert mdx code into html & paste into file
|
|
@@ -203,19 +203,21 @@ async function createSite(inputPath, outputPath) {
|
|
|
203
203
|
hostmdxInputPath: inputPath,
|
|
204
204
|
hostmdxOutputPath: outputPath
|
|
205
205
|
};
|
|
206
|
-
|
|
207
|
-
|
|
206
|
+
globalArgs = await configs?.modGlobalArgs?.(inputPath, outputPath, globalArgs) ?? globalArgs;
|
|
207
|
+
let result = await mdxToHtml(mdxCode, parentDir, globalArgs, async (settings) => { return await configs?.modBundleMDXSettings?.(inputPath, outputPath, settings) ?? settings });
|
|
208
|
+
let htmlCode = result.html;
|
|
209
|
+
createFile(absHtmlPath, `<!DOCTYPE html>${htmlCode}`);
|
|
208
210
|
|
|
209
211
|
|
|
210
212
|
// Broadcast file creation ended
|
|
211
|
-
configs?.onFileCreateEnd?.(currentPath, absHtmlPath)
|
|
213
|
+
await configs?.onFileCreateEnd?.(inputPath, outputPath, currentPath, absHtmlPath, result)
|
|
212
214
|
}
|
|
213
215
|
// Copy paste file
|
|
214
216
|
else if (!isDir) {
|
|
215
217
|
log(`${currentPath} ---> ${absToOutput}`, true)
|
|
216
|
-
configs?.onFileCreateStart?.(currentPath, absToOutput)
|
|
218
|
+
await configs?.onFileCreateStart?.(inputPath, outputPath, currentPath, absToOutput)
|
|
217
219
|
fs.copyFileSync(currentPath, absToOutput)
|
|
218
|
-
configs?.onFileCreateEnd?.(currentPath, absToOutput)
|
|
220
|
+
await configs?.onFileCreateEnd?.(inputPath, outputPath, currentPath, absToOutput, undefined)
|
|
219
221
|
}
|
|
220
222
|
|
|
221
223
|
|
|
@@ -244,7 +246,7 @@ async function createSite(inputPath, outputPath) {
|
|
|
244
246
|
else {
|
|
245
247
|
log(`Created site at ${outputPath}`)
|
|
246
248
|
}
|
|
247
|
-
configs?.onSiteCreateEnd?.(inputPath, outputPath, isCreateSitePending)
|
|
249
|
+
await configs?.onSiteCreateEnd?.(inputPath, outputPath, isCreateSitePending)
|
|
248
250
|
|
|
249
251
|
|
|
250
252
|
// Reinvoke creation
|
|
@@ -305,7 +307,7 @@ function isSubPath(potentialParent, thePath) {
|
|
|
305
307
|
thePath[potentialParent.length] === path.sep ||
|
|
306
308
|
thePath[potentialParent.length] === undefined
|
|
307
309
|
);
|
|
308
|
-
}
|
|
310
|
+
}
|
|
309
311
|
async function filterArgs(rawArgs) {
|
|
310
312
|
// Assign to create
|
|
311
313
|
let toCreateOnly = rawArgs.includes(CREATE_FLAG) || rawArgs.includes(CREATE_SHORT_FLAG)
|
|
@@ -412,10 +414,10 @@ async function watchForChanges(pathTowatch, callback) {
|
|
|
412
414
|
ignoreInitial: true
|
|
413
415
|
}).on('all', callback);
|
|
414
416
|
}
|
|
415
|
-
function startServer(htmlDir, port) { // Starts server at given port
|
|
417
|
+
async function startServer(htmlDir, port) { // Starts server at given port
|
|
416
418
|
|
|
417
419
|
// Broadcast server starting
|
|
418
|
-
configs?.onHostStart?.(port)
|
|
420
|
+
await configs?.onHostStart?.(port)
|
|
419
421
|
|
|
420
422
|
|
|
421
423
|
// Start Server
|
|
@@ -446,7 +448,7 @@ function startServer(htmlDir, port) { // Starts server at given port
|
|
|
446
448
|
|
|
447
449
|
// Start listening
|
|
448
450
|
newApp.listen(port)
|
|
449
|
-
newApp.server.on("close", () => { configs?.onHostEnd?.(port) });
|
|
451
|
+
newApp.server.on("close", async () => { await configs?.onHostEnd?.(port) });
|
|
450
452
|
newApp.server.on("error", (e) => { log(`Failed to start server: ${e.message}`); throw e; });
|
|
451
453
|
log(`Server listening at ${port} ... (Press 'r' to manually reload, Press 'Ctrl+c' to exit)`)
|
|
452
454
|
|
|
@@ -480,6 +482,7 @@ async function Main() {
|
|
|
480
482
|
// Get config
|
|
481
483
|
let configFilePath = path.join(args.inputPath, `./${CONFIG_FILE_NAME}`)
|
|
482
484
|
if (fs.existsSync(configFilePath)) {
|
|
485
|
+
log(`Importing config file ${CONFIG_FILE_NAME}`);
|
|
483
486
|
configs = await import(pathToFileURL(configFilePath).href);
|
|
484
487
|
}
|
|
485
488
|
|
|
@@ -498,8 +501,8 @@ async function Main() {
|
|
|
498
501
|
|
|
499
502
|
// Watch for changes
|
|
500
503
|
if (args.toTrackChanges) {
|
|
501
|
-
watchForChanges(args.inputPath, (event, path) => {
|
|
502
|
-
if (typeof configs
|
|
504
|
+
watchForChanges(args.inputPath, async (event, path) => {
|
|
505
|
+
if (typeof configs?.toTriggerRecreate === 'function' && !(await configs?.toTriggerRecreate(event, path))) {
|
|
503
506
|
return;
|
|
504
507
|
}
|
|
505
508
|
|
|
@@ -510,7 +513,7 @@ async function Main() {
|
|
|
510
513
|
|
|
511
514
|
|
|
512
515
|
// Start server
|
|
513
|
-
app = startServer(args.outputPath, args.port);
|
|
516
|
+
app = await startServer(args.outputPath, args.port);
|
|
514
517
|
|
|
515
518
|
|
|
516
519
|
// Handle quit
|
package/mdx-to-html.js
CHANGED
|
@@ -27,10 +27,9 @@ const jsxBundlerConfig = {
|
|
|
27
27
|
|
|
28
28
|
|
|
29
29
|
// Methods
|
|
30
|
-
function
|
|
30
|
+
function getMDXExport(code, globals) {
|
|
31
31
|
const fn = new Function(...Object.keys(globals), code);
|
|
32
|
-
|
|
33
|
-
return mdxExport.default;
|
|
32
|
+
return fn(...Object.values(globals));
|
|
34
33
|
}
|
|
35
34
|
export async function mdxToHtml(mdxCode, baseUrl, globalArgs = {}, modSettingsCallback = undefined) {
|
|
36
35
|
|
|
@@ -55,14 +54,18 @@ export async function mdxToHtml(mdxCode, baseUrl, globalArgs = {}, modSettingsCa
|
|
|
55
54
|
|
|
56
55
|
// Modify settings
|
|
57
56
|
if (modSettingsCallback !== undefined) {
|
|
58
|
-
settings = modSettingsCallback(settings)
|
|
57
|
+
settings = await modSettingsCallback(settings)
|
|
59
58
|
}
|
|
60
59
|
|
|
61
60
|
|
|
62
61
|
// Generate html
|
|
63
62
|
const { code } = await bundleMDX(settings);
|
|
64
|
-
const
|
|
63
|
+
const Exports = getMDXExport(code, { Preact, PreactDOM, _jsx_runtime, require: nativeRequire, ...globalArgs });
|
|
64
|
+
const Component = Exports.default;
|
|
65
65
|
|
|
66
66
|
|
|
67
|
-
return
|
|
67
|
+
return {
|
|
68
|
+
html: renderToString(Preact.h(Component, {})),
|
|
69
|
+
exports: Exports
|
|
70
|
+
}
|
|
68
71
|
}
|