mdts 0.2.0 → 0.3.1
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 +35 -5
- package/dist/cli.js +5 -1
- package/dist/frontend/bundle.js +5453 -1132
- package/dist/server/routes/outline.js +7 -2
- package/dist/server/server.js +58 -7
- package/package.json +7 -2
- package/public/welcome.md +31 -0
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { Router } from 'express';
|
|
2
2
|
import fs from 'fs';
|
|
3
|
-
import path from 'path';
|
|
4
3
|
import MarkdownIt from 'markdown-it';
|
|
4
|
+
import path, { dirname } from 'path';
|
|
5
|
+
import { fileURLToPath } from 'url';
|
|
5
6
|
const md = new MarkdownIt();
|
|
7
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
8
|
+
const __dirname = dirname(__filename);
|
|
6
9
|
const slugify = (text) => {
|
|
7
10
|
return text
|
|
8
11
|
.toLowerCase()
|
|
@@ -36,7 +39,9 @@ export const outlineRouter = (directory) => {
|
|
|
36
39
|
return res.status(400).send('filePath query parameter is required.');
|
|
37
40
|
}
|
|
38
41
|
try {
|
|
39
|
-
const absolutePath =
|
|
42
|
+
const absolutePath = filePath === 'mdts-welcome-markdown.md'
|
|
43
|
+
? path.join(__dirname, '../../../public/welcome.md')
|
|
44
|
+
: path.join(directory, filePath);
|
|
40
45
|
const outline = getMarkdownOutline(absolutePath);
|
|
41
46
|
res.json(outline);
|
|
42
47
|
}
|
package/dist/server/server.js
CHANGED
|
@@ -1,23 +1,74 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import chokidar from 'chokidar';
|
|
1
11
|
import express from 'express';
|
|
2
|
-
import
|
|
12
|
+
import { promises as fs } from 'fs';
|
|
13
|
+
import path, { dirname } from 'path';
|
|
3
14
|
import { fileURLToPath } from 'url';
|
|
4
|
-
import {
|
|
15
|
+
import { WebSocketServer } from 'ws';
|
|
5
16
|
import { fileTreeRouter } from './routes/filetree.js';
|
|
6
17
|
import { outlineRouter } from './routes/outline.js';
|
|
7
18
|
const __filename = fileURLToPath(import.meta.url);
|
|
8
19
|
const __dirname = dirname(__filename);
|
|
9
20
|
export const serve = (directory, port) => {
|
|
10
21
|
const app = express();
|
|
22
|
+
// Mount library static files
|
|
11
23
|
app.use(express.static(path.join(__dirname, '../../public')));
|
|
12
24
|
app.use(express.static(path.join(__dirname, '../../dist/frontend')));
|
|
13
|
-
|
|
25
|
+
// Define API
|
|
14
26
|
app.use('/api/filetree', fileTreeRouter(directory));
|
|
27
|
+
app.get('/api/markdown/mdts-welcome-markdown.md', (req, res) => {
|
|
28
|
+
res.setHeader('Content-Type', 'text/plain');
|
|
29
|
+
res.sendFile(path.join(__dirname, '../../public/welcome.md'));
|
|
30
|
+
});
|
|
31
|
+
app.use('/api/markdown', express.static(directory));
|
|
15
32
|
app.use('/api/outline', outlineRouter(directory));
|
|
16
33
|
// Catch-all route to serve index.html for any other requests
|
|
17
|
-
app.get('*', (req, res) => {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
34
|
+
app.get('*', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
|
|
35
|
+
const filePath = path.join(directory, req.path);
|
|
36
|
+
let isDirectory = false;
|
|
37
|
+
try {
|
|
38
|
+
const stats = yield fs.stat(filePath);
|
|
39
|
+
isDirectory = stats.isDirectory();
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
// File or directory does not exist, proceed as if it's a file
|
|
43
|
+
}
|
|
44
|
+
if (isDirectory || req.path.toLowerCase().endsWith('.md') || req.path.toLowerCase().endsWith('.markdown')) {
|
|
45
|
+
return res.sendFile(path.join(__dirname, '../../public/index.html'));
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
return res.sendFile(req.path, { root: directory });
|
|
49
|
+
}
|
|
50
|
+
}));
|
|
51
|
+
const server = app.listen(port, () => {
|
|
21
52
|
console.log(`🚀 Server listening at http://localhost:${port}`);
|
|
22
53
|
});
|
|
54
|
+
const wss = new WebSocketServer({ server });
|
|
55
|
+
const watcher = chokidar.watch(directory, { ignored: /node_modules/, ignoreInitial: true });
|
|
56
|
+
watcher.on('change', (filePath) => {
|
|
57
|
+
console.log(`🔃 File changed: ${filePath}, reloading...`);
|
|
58
|
+
wss.clients.forEach((client) => {
|
|
59
|
+
client.send(JSON.stringify({ type: 'reload-content' }));
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
watcher.on('add', (filePath) => {
|
|
63
|
+
console.log(`🌲 File added: ${filePath}, reloading tree...`);
|
|
64
|
+
wss.clients.forEach((client) => {
|
|
65
|
+
client.send(JSON.stringify({ type: 'reload-tree' }));
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
watcher.on('unlink', (filePath) => {
|
|
69
|
+
console.log('🌲 File removed: ${filePath}, reloading tree...');
|
|
70
|
+
wss.clients.forEach((client) => {
|
|
71
|
+
client.send(JSON.stringify({ type: 'reload-tree' }));
|
|
72
|
+
});
|
|
73
|
+
});
|
|
23
74
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mdts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "A markdown preview server.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -48,6 +48,7 @@
|
|
|
48
48
|
"@types/markdown-it": "^14.1.2",
|
|
49
49
|
"@types/react": "^19.1.8",
|
|
50
50
|
"@types/react-dom": "^19.1.6",
|
|
51
|
+
"@types/ws": "^8.18.1",
|
|
51
52
|
"babel-loader": "^10.0.0",
|
|
52
53
|
"css-loader": "^7.1.2",
|
|
53
54
|
"html-webpack-plugin": "^5.6.3",
|
|
@@ -60,6 +61,7 @@
|
|
|
60
61
|
"dependencies": {
|
|
61
62
|
"@mui/icons-material": "^7.2.0",
|
|
62
63
|
"@mui/x-tree-view": "^8.8.0",
|
|
64
|
+
"chokidar": "^4.0.3",
|
|
63
65
|
"commander": "^14.0.0",
|
|
64
66
|
"express": "^4.19.2",
|
|
65
67
|
"markdown-it": "^14.1.0",
|
|
@@ -68,6 +70,9 @@
|
|
|
68
70
|
"react-dom": "^19.1.0",
|
|
69
71
|
"react-markdown": "^10.1.0",
|
|
70
72
|
"remark-gfm": "^4.0.1",
|
|
71
|
-
"remark-slug": "^7.0.1"
|
|
73
|
+
"remark-slug": "^7.0.1",
|
|
74
|
+
"rehype-raw": "^7.0.0",
|
|
75
|
+
"react-syntax-highlighter": "^15.6.1",
|
|
76
|
+
"ws": "^8.18.3"
|
|
72
77
|
}
|
|
73
78
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
<h1 align="center">
|
|
2
|
+
<img src="logo.svg" alt="mdts" width="400">
|
|
3
|
+
</h1>
|
|
4
|
+
|
|
5
|
+
mdts (Markdown Tree Server) is a simple and efficient tool for serving and viewing markdown files with an interactive file tree and outline. It's designed to help you navigate and read your markdown-based documentation or notes with ease.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
* **Markdown Rendering:** Renders markdown files into clean, readable HTML.
|
|
10
|
+
* **File Tree Navigation:** Browse your markdown files and directories through an intuitive file tree.
|
|
11
|
+
* **Outline View:** Quickly jump to sections within a markdown file using the generated outline.
|
|
12
|
+
* **Live Reload:** Automatically reloads content when markdown files changes.
|
|
13
|
+
* **Dark Mode:** Supports dark mode for comfortable reading.
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
To start the mdts server, run:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
mdts <path_to_your_markdown_directory>
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Then, open your browser and navigate to `http://localhost:<port>` (default port is 8521).
|
|
24
|
+
|
|
25
|
+
## About This Page
|
|
26
|
+
|
|
27
|
+
This is a dynamically generated welcome page. You can replace it with your own `README.md` or any other markdown file by navigating to its path in the URL.
|
|
28
|
+
|
|
29
|
+
## Links
|
|
30
|
+
|
|
31
|
+
* [GitHub Repository](https://github.com/unhappychoice/mdts)
|