md-explorer 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 md-explorer contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # md-explorer
2
+
3
+ A web-based file explorer for browsing and reading Markdown and HTML files. Point it at any directory and get a navigable file tree with rendered Markdown preview.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install -g md-explorer
9
+ ```
10
+
11
+ Or run directly with npx:
12
+
13
+ ```bash
14
+ npx md-explorer ./docs
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```bash
20
+ md-explorer [directory] [options]
21
+ ```
22
+
23
+ ### Arguments
24
+
25
+ | Argument | Description |
26
+ |-------------|------------------------------------------|
27
+ | `directory` | Directory to serve (default: current dir)|
28
+
29
+ ### Options
30
+
31
+ | Option | Description |
32
+ |------------------|--------------------------------------------------|
33
+ | `-p, --port PORT`| Port to listen on (default: 3000, or `PORT` env) |
34
+ | `-h, --help` | Show help message |
35
+
36
+ ## Examples
37
+
38
+ ```bash
39
+ # Serve current directory on default port
40
+ md-explorer
41
+
42
+ # Serve a specific directory
43
+ md-explorer ./docs
44
+
45
+ # Serve on a custom port
46
+ md-explorer ./docs --port 8080
47
+
48
+ # Use PORT environment variable
49
+ PORT=4000 md-explorer ~/notes
50
+ ```
51
+
52
+ ## Features
53
+
54
+ - Collapsible file tree sidebar with lazy-loaded subdirectories
55
+ - Rendered Markdown preview using [marked](https://github.com/markedjs/marked)
56
+ - Inline HTML/HTM file viewing via iframe
57
+ - Dot-files and `node_modules` are hidden automatically
58
+ - Path traversal protection on all API routes
59
+
60
+ ## License
61
+
62
+ MIT
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "md-explorer",
3
+ "version": "1.0.0",
4
+ "description": "Web-based file explorer for Markdown & HTML",
5
+ "bin": {
6
+ "md-explorer": "server.js"
7
+ },
8
+ "files": [
9
+ "server.js",
10
+ "public/"
11
+ ],
12
+ "scripts": {
13
+ "start": "node server.js"
14
+ },
15
+ "keywords": [
16
+ "markdown",
17
+ "explorer",
18
+ "file-browser",
19
+ "html",
20
+ "viewer",
21
+ "cli",
22
+ "server"
23
+ ],
24
+ "license": "MIT",
25
+ "engines": {
26
+ "node": ">=16"
27
+ },
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "https://github.com/iamthemovie/md-explorer.git"
31
+ },
32
+ "dependencies": {
33
+ "express": "^4.21.0",
34
+ "marked": "^15.0.0"
35
+ }
36
+ }
package/public/app.js ADDED
@@ -0,0 +1,119 @@
1
+ (function () {
2
+ const treeEl = document.getElementById('tree');
3
+ const contentEl = document.getElementById('content');
4
+ const collapseBtn = document.getElementById('collapse-btn');
5
+ const sidebar = document.getElementById('sidebar');
6
+ let activeLabel = null;
7
+
8
+ // --- Toggle sidebar ---
9
+ collapseBtn.addEventListener('click', () => {
10
+ sidebar.classList.toggle('collapsed');
11
+ });
12
+
13
+ // --- Fetch and render tree ---
14
+ async function fetchEntries(dirPath) {
15
+ const url = dirPath
16
+ ? '/api/tree?path=' + encodeURIComponent(dirPath)
17
+ : '/api/tree';
18
+ const res = await fetch(url);
19
+ return res.json();
20
+ }
21
+
22
+ async function loadTree() {
23
+ const entries = await fetchEntries('');
24
+ treeEl.innerHTML = '';
25
+ const ul = buildTreeUl(entries);
26
+ treeEl.appendChild(ul);
27
+ }
28
+
29
+ function buildTreeUl(nodes) {
30
+ const ul = document.createElement('ul');
31
+
32
+ for (const node of nodes) {
33
+ const li = document.createElement('li');
34
+
35
+ if (node.type === 'directory') {
36
+ const label = document.createElement('div');
37
+ label.className = 'tree-label';
38
+ label.innerHTML =
39
+ '<span class="tree-icon">&#9656;</span>' +
40
+ escapeHtml(node.name);
41
+
42
+ const childUl = document.createElement('ul');
43
+ childUl.className = 'tree-children';
44
+ let loaded = false;
45
+
46
+ label.addEventListener('click', async () => {
47
+ const opening = !childUl.classList.contains('open');
48
+ if (opening && !loaded) {
49
+ const children = await fetchEntries(node.path);
50
+ const freshUl = buildTreeUl(children);
51
+ while (freshUl.firstChild) childUl.appendChild(freshUl.firstChild);
52
+ loaded = true;
53
+ }
54
+ childUl.classList.toggle('open');
55
+ label.querySelector('.tree-icon').innerHTML =
56
+ childUl.classList.contains('open') ? '&#9662;' : '&#9656;';
57
+ });
58
+
59
+ li.appendChild(label);
60
+ li.appendChild(childUl);
61
+ } else {
62
+ const label = document.createElement('div');
63
+ label.className = 'tree-label';
64
+ const ext = node.name.split('.').pop().toLowerCase();
65
+ const icon = ext === 'md' ? '&#128196;' : '&#127760;';
66
+ label.innerHTML =
67
+ '<span class="tree-icon">' + icon + '</span>' +
68
+ escapeHtml(node.name);
69
+
70
+ label.addEventListener('click', () => handleFileClick(node, label));
71
+ li.appendChild(label);
72
+ }
73
+
74
+ ul.appendChild(li);
75
+ }
76
+
77
+ return ul;
78
+ }
79
+
80
+ // --- File click handlers ---
81
+ function handleFileClick(node, label) {
82
+ const ext = node.name.split('.').pop().toLowerCase();
83
+
84
+ if (ext === 'html' || ext === 'htm') {
85
+ window.open('/raw/' + node.path, '_blank');
86
+ } else if (ext === 'md') {
87
+ loadMarkdown(node.path, label);
88
+ }
89
+ }
90
+
91
+ async function loadMarkdown(filePath, label) {
92
+ if (activeLabel) activeLabel.classList.remove('active');
93
+ label.classList.add('active');
94
+ activeLabel = label;
95
+
96
+ try {
97
+ const res = await fetch('/api/file?path=' + encodeURIComponent(filePath));
98
+ if (!res.ok) {
99
+ contentEl.innerHTML = '<p class="placeholder">Error loading file.</p>';
100
+ return;
101
+ }
102
+ const data = await res.json();
103
+ document.title = data.title + ' — MarkdownX';
104
+ contentEl.innerHTML = data.html;
105
+ } catch {
106
+ contentEl.innerHTML = '<p class="placeholder">Error loading file.</p>';
107
+ }
108
+ }
109
+
110
+ // --- Utilities ---
111
+ function escapeHtml(str) {
112
+ const el = document.createElement('span');
113
+ el.textContent = str;
114
+ return el.innerHTML;
115
+ }
116
+
117
+ // --- Init ---
118
+ loadTree();
119
+ })();
@@ -0,0 +1,24 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>MarkdownX</title>
7
+ <link rel="stylesheet" href="style.css">
8
+ </head>
9
+ <body>
10
+ <div id="app">
11
+ <aside id="sidebar">
12
+ <div id="sidebar-header">
13
+ <strong>MarkdownX</strong>
14
+ </div>
15
+ <nav id="tree"></nav>
16
+ </aside>
17
+ <button id="collapse-btn" title="Toggle sidebar">&#x2630;</button>
18
+ <main id="content">
19
+ <p class="placeholder">Select a file from the sidebar.</p>
20
+ </main>
21
+ </div>
22
+ <script src="app.js"></script>
23
+ </body>
24
+ </html>
@@ -0,0 +1,194 @@
1
+ *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
2
+
3
+ body {
4
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
5
+ height: 100vh;
6
+ overflow: hidden;
7
+ color: #24292e;
8
+ background: #fff;
9
+ }
10
+
11
+ #app {
12
+ display: flex;
13
+ height: 100vh;
14
+ position: relative;
15
+ }
16
+
17
+ /* --- Sidebar --- */
18
+
19
+ #sidebar {
20
+ width: 280px;
21
+ min-width: 280px;
22
+ background: #f6f8fa;
23
+ border-right: 1px solid #d0d7de;
24
+ display: flex;
25
+ flex-direction: column;
26
+ overflow: hidden;
27
+ transition: min-width 0.15s, width 0.15s;
28
+ }
29
+
30
+ #sidebar.collapsed {
31
+ width: 0;
32
+ min-width: 0;
33
+ }
34
+
35
+ #sidebar-header {
36
+ display: flex;
37
+ align-items: center;
38
+ padding: 12px 16px;
39
+ border-bottom: 1px solid #d0d7de;
40
+ white-space: nowrap;
41
+ }
42
+
43
+ #collapse-btn {
44
+ position: absolute;
45
+ top: 8px;
46
+ left: 248px;
47
+ z-index: 10;
48
+ background: #f6f8fa;
49
+ border: 1px solid #d0d7de;
50
+ border-radius: 4px;
51
+ cursor: pointer;
52
+ font-size: 16px;
53
+ padding: 2px 6px;
54
+ color: #57606a;
55
+ transition: left 0.15s;
56
+ }
57
+
58
+ #sidebar.collapsed ~ #collapse-btn {
59
+ left: 8px;
60
+ }
61
+
62
+ #collapse-btn:hover { background: #e1e4e8; }
63
+
64
+ #tree {
65
+ overflow-y: auto;
66
+ flex: 1;
67
+ padding: 8px 0;
68
+ font-size: 14px;
69
+ }
70
+
71
+ /* --- Tree --- */
72
+
73
+ #tree ul {
74
+ list-style: none;
75
+ padding-left: 16px;
76
+ }
77
+
78
+ #tree > ul { padding-left: 8px; }
79
+
80
+ #tree li { user-select: none; }
81
+
82
+ .tree-label {
83
+ display: flex;
84
+ align-items: center;
85
+ gap: 6px;
86
+ padding: 3px 8px;
87
+ border-radius: 4px;
88
+ cursor: pointer;
89
+ white-space: nowrap;
90
+ overflow: hidden;
91
+ text-overflow: ellipsis;
92
+ }
93
+
94
+ .tree-label:hover { background: #e1e4e8; }
95
+
96
+ .tree-label.active {
97
+ background: #0969da;
98
+ color: #fff;
99
+ }
100
+
101
+ .tree-icon { flex-shrink: 0; width: 16px; text-align: center; font-size: 12px; }
102
+
103
+ .tree-children { display: none; }
104
+ .tree-children.open { display: block; }
105
+
106
+ /* --- Content --- */
107
+
108
+ #content {
109
+ flex: 1;
110
+ overflow-y: auto;
111
+ padding: 32px 48px;
112
+ max-width: 100%;
113
+ }
114
+
115
+ #content .placeholder {
116
+ color: #8b949e;
117
+ font-style: italic;
118
+ margin-top: 40vh;
119
+ text-align: center;
120
+ }
121
+
122
+ /* --- Markdown typography --- */
123
+
124
+ #content h1, #content h2, #content h3,
125
+ #content h4, #content h5, #content h6 {
126
+ margin-top: 24px;
127
+ margin-bottom: 16px;
128
+ font-weight: 600;
129
+ line-height: 1.25;
130
+ }
131
+
132
+ #content h1 { font-size: 2em; padding-bottom: 0.3em; border-bottom: 1px solid #d0d7de; }
133
+ #content h2 { font-size: 1.5em; padding-bottom: 0.3em; border-bottom: 1px solid #d0d7de; }
134
+ #content h3 { font-size: 1.25em; }
135
+
136
+ #content p { margin-bottom: 16px; line-height: 1.6; }
137
+
138
+ #content a { color: #0969da; text-decoration: none; }
139
+ #content a:hover { text-decoration: underline; }
140
+
141
+ #content ul, #content ol { padding-left: 2em; margin-bottom: 16px; }
142
+ #content li { margin-bottom: 4px; line-height: 1.6; }
143
+
144
+ #content blockquote {
145
+ padding: 0 1em;
146
+ color: #57606a;
147
+ border-left: 3px solid #d0d7de;
148
+ margin-bottom: 16px;
149
+ }
150
+
151
+ #content code {
152
+ background: #f6f8fa;
153
+ padding: 0.2em 0.4em;
154
+ border-radius: 3px;
155
+ font-size: 85%;
156
+ font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
157
+ }
158
+
159
+ #content pre {
160
+ background: #f6f8fa;
161
+ padding: 16px;
162
+ border-radius: 6px;
163
+ overflow-x: auto;
164
+ margin-bottom: 16px;
165
+ line-height: 1.45;
166
+ }
167
+
168
+ #content pre code {
169
+ background: none;
170
+ padding: 0;
171
+ font-size: 85%;
172
+ }
173
+
174
+ #content table {
175
+ border-collapse: collapse;
176
+ margin-bottom: 16px;
177
+ width: auto;
178
+ }
179
+
180
+ #content th, #content td {
181
+ border: 1px solid #d0d7de;
182
+ padding: 6px 13px;
183
+ }
184
+
185
+ #content th { background: #f6f8fa; font-weight: 600; }
186
+
187
+ #content img { max-width: 100%; }
188
+
189
+ #content hr {
190
+ height: 2px;
191
+ background: #d0d7de;
192
+ border: none;
193
+ margin: 24px 0;
194
+ }
package/server.js ADDED
@@ -0,0 +1,194 @@
1
+ #!/usr/bin/env node
2
+
3
+ const express = require('express');
4
+ const path = require('path');
5
+ const fs = require('fs');
6
+ const { marked } = require('marked');
7
+
8
+ // --- CLI argument parsing ---
9
+
10
+ function printHelp() {
11
+ console.log(`
12
+ Usage: md-explorer [directory] [options]
13
+
14
+ Serve a directory of Markdown & HTML files in a web-based explorer.
15
+
16
+ Arguments:
17
+ directory Directory to serve (default: current directory)
18
+
19
+ Options:
20
+ -p, --port PORT Port to listen on (default: 3000, or PORT env var)
21
+ -h, --help Show this help message
22
+ `);
23
+ process.exit(0);
24
+ }
25
+
26
+ function parseArgs(argv) {
27
+ const args = argv.slice(2);
28
+ let dir = '.';
29
+ let port = parseInt(process.env.PORT, 10) || 3000;
30
+
31
+ for (let i = 0; i < args.length; i++) {
32
+ const arg = args[i];
33
+ if (arg === '-h' || arg === '--help') {
34
+ printHelp();
35
+ } else if (arg === '-p' || arg === '--port') {
36
+ const val = args[++i];
37
+ if (!val || isNaN(parseInt(val, 10))) {
38
+ console.error(`Error: ${arg} requires a numeric port argument`);
39
+ process.exit(1);
40
+ }
41
+ port = parseInt(val, 10);
42
+ } else if (arg.startsWith('-')) {
43
+ console.error(`Unknown option: ${arg}\nRun md-explorer --help for usage.`);
44
+ process.exit(1);
45
+ } else {
46
+ dir = arg;
47
+ }
48
+ }
49
+
50
+ return { dir, port };
51
+ }
52
+
53
+ const { dir, port: PORT } = parseArgs(process.argv);
54
+ const ROOT = path.resolve(dir);
55
+
56
+ if (!fs.existsSync(ROOT) || !fs.statSync(ROOT).isDirectory()) {
57
+ console.error(`Error: "${ROOT}" is not a directory`);
58
+ process.exit(1);
59
+ }
60
+
61
+ const app = express();
62
+
63
+ const ALLOWED_EXT = new Set(['.md', '.html', '.htm']);
64
+ const SKIP_DIRS = new Set(['node_modules']);
65
+
66
+ // --- Path security ---
67
+
68
+ function safePath(userPath) {
69
+ const resolved = path.resolve(ROOT, userPath);
70
+ if (!resolved.startsWith(ROOT + path.sep) && resolved !== ROOT) {
71
+ return null;
72
+ }
73
+ return resolved;
74
+ }
75
+
76
+ // --- List single directory (non-recursive) ---
77
+
78
+ function listDir(dir) {
79
+ let entries;
80
+ try {
81
+ entries = fs.readdirSync(dir, { withFileTypes: true });
82
+ } catch {
83
+ return [];
84
+ }
85
+
86
+ const results = [];
87
+
88
+ for (const entry of entries) {
89
+ if (entry.name.startsWith('.')) continue;
90
+ if (SKIP_DIRS.has(entry.name)) continue;
91
+
92
+ const fullPath = path.join(dir, entry.name);
93
+ const relPath = path.relative(ROOT, fullPath);
94
+
95
+ if (entry.isDirectory()) {
96
+ results.push({
97
+ name: entry.name,
98
+ path: relPath,
99
+ type: 'directory',
100
+ });
101
+ } else if (ALLOWED_EXT.has(path.extname(entry.name).toLowerCase())) {
102
+ results.push({
103
+ name: entry.name,
104
+ path: relPath,
105
+ type: 'file',
106
+ });
107
+ }
108
+ }
109
+
110
+ // Sort: directories first, then alphabetical
111
+ results.sort((a, b) => {
112
+ if (a.type === b.type) return a.name.localeCompare(b.name);
113
+ return a.type === 'directory' ? -1 : 1;
114
+ });
115
+
116
+ return results;
117
+ }
118
+
119
+ // --- Static files ---
120
+
121
+ app.use(express.static(path.join(__dirname, 'public')));
122
+
123
+ // --- API: directory tree ---
124
+
125
+ app.get('/api/tree', (req, res) => {
126
+ const userPath = req.query.path || '';
127
+ const resolved = userPath ? safePath(userPath) : ROOT;
128
+ if (!resolved) return res.status(403).json({ error: 'Forbidden' });
129
+
130
+ const entries = listDir(resolved);
131
+ res.json(entries);
132
+ });
133
+
134
+ // --- API: render markdown file ---
135
+
136
+ app.get('/api/file', (req, res) => {
137
+ const userPath = req.query.path;
138
+ if (!userPath) return res.status(400).json({ error: 'Missing path parameter' });
139
+
140
+ const resolved = safePath(userPath);
141
+ if (!resolved) return res.status(403).json({ error: 'Forbidden' });
142
+
143
+ const ext = path.extname(resolved).toLowerCase();
144
+ if (ext !== '.md') return res.status(400).json({ error: 'Only .md files supported' });
145
+
146
+ let content;
147
+ try {
148
+ content = fs.readFileSync(resolved, 'utf-8');
149
+ } catch {
150
+ return res.status(404).json({ error: 'File not found' });
151
+ }
152
+
153
+ const html = marked(content);
154
+
155
+ // Extract title from first heading or filename
156
+ const titleMatch = content.match(/^#\s+(.+)$/m);
157
+ const title = titleMatch ? titleMatch[1] : path.basename(resolved, ext);
158
+
159
+ res.json({ html, title });
160
+ });
161
+
162
+ // --- Raw HTML file serving ---
163
+
164
+ app.get('/raw/*', (req, res) => {
165
+ // Express 4 wildcard: everything after /raw/
166
+ const userPath = req.params[0];
167
+ if (!userPath) return res.status(400).send('Missing path');
168
+
169
+ const resolved = safePath(userPath);
170
+ if (!resolved) return res.status(403).send('Forbidden');
171
+
172
+ const ext = path.extname(resolved).toLowerCase();
173
+ if (ext !== '.html' && ext !== '.htm') {
174
+ return res.status(400).send('Only .html/.htm files supported');
175
+ }
176
+
177
+ res.sendFile(resolved, (err) => {
178
+ if (err) res.status(404).send('File not found');
179
+ });
180
+ });
181
+
182
+ // --- Start ---
183
+
184
+ const server = app.listen(PORT, () => {
185
+ console.log(`md-explorer serving "${ROOT}" at http://localhost:${PORT}`);
186
+ });
187
+
188
+ server.on('error', (err) => {
189
+ if (err.code === 'EADDRINUSE') {
190
+ console.error(`Error: Port ${PORT} is already in use. Try a different port with --port.`);
191
+ process.exit(1);
192
+ }
193
+ throw err;
194
+ });