@mgks/docmd 0.2.4 → 0.2.6

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.
@@ -6,6 +6,7 @@ const { loadConfig } = require('../core/config-loader');
6
6
  const { createMarkdownItInstance, processMarkdownFile, findMarkdownFiles } = require('../core/file-processor');
7
7
  const { generateHtmlPage, generateNavigationHtml } = require('../core/html-generator');
8
8
  const { renderIcon, clearWarnedIcons } = require('../core/icon-renderer');
9
+ const { findPageNeighbors } = require('../core/navigation-helper');
9
10
  const { generateSitemap } = require('../plugins/sitemap');
10
11
  const { version } = require('../../package.json');
11
12
  const matter = require('gray-matter');
@@ -218,8 +219,12 @@ async function buildSite(configPath, options = { isDev: false, preserve: false,
218
219
 
219
220
  const finalOutputHtmlPath = path.join(OUTPUT_DIR, outputHtmlPath);
220
221
 
221
- const depth = outputHtmlPath.split(path.sep).length - 1;
222
- const relativePathToRoot = depth > 0 ? '../'.repeat(depth) : './';
222
+ let relativePathToRoot = path.relative(path.dirname(finalOutputHtmlPath), OUTPUT_DIR);
223
+ if (relativePathToRoot === '') {
224
+ relativePathToRoot = './';
225
+ } else {
226
+ relativePathToRoot = relativePathToRoot.replace(/\\/g, '/') + '/';
227
+ }
223
228
 
224
229
  let normalizedPath = path.relative(SRC_DIR, filePath).replace(/\\/g, '/');
225
230
  if (path.basename(normalizedPath) === 'index.md') {
@@ -247,74 +252,20 @@ async function buildSite(configPath, options = { isDev: false, preserve: false,
247
252
  config
248
253
  );
249
254
 
250
- let prevPage = null;
251
- let nextPage = null;
252
- let currentPageIndex = -1;
253
-
254
- const flatNavigation = [];
255
-
256
- function createNormalizedPath(item) {
257
- if (!item.path) return null;
258
- return item.path.startsWith('/') ? item.path : '/' + item.path;
259
- }
260
-
261
- function extractNavigationItems(items) {
262
- if (!items || !Array.isArray(items)) return;
263
-
264
- for (const item of items) {
265
- if (item.external) continue;
266
-
267
- if (item.path) {
268
- let normalizedItemPath = createNormalizedPath(item);
269
- if (item.children && !normalizedItemPath.endsWith('/')) {
270
- normalizedItemPath += '/';
271
- }
272
- flatNavigation.push({
273
- title: item.title,
274
- path: normalizedItemPath,
275
- });
276
- }
277
-
278
- if (item.children && Array.isArray(item.children)) {
279
- extractNavigationItems(item.children);
280
- }
281
- }
282
- }
283
-
284
- extractNavigationItems(config.navigation);
285
-
286
- currentPageIndex = flatNavigation.findIndex(item => {
287
- const itemPath = item.path;
288
- const currentPagePath = normalizedPath;
289
- if (itemPath === currentPagePath) {
290
- return true;
291
- }
292
- if (itemPath.endsWith('/') && itemPath.slice(0, -1) === currentPagePath) {
293
- return true;
294
- }
295
- if (currentPagePath.endsWith('/') && currentPagePath.slice(0, -1) === itemPath) {
296
- return true;
297
- }
298
- return false;
299
- });
300
-
301
- if (currentPageIndex >= 0) {
302
- if (currentPageIndex > 0) prevPage = flatNavigation[currentPageIndex - 1];
303
- if (currentPageIndex < flatNavigation.length - 1) nextPage = flatNavigation[currentPageIndex + 1];
304
- }
305
-
255
+ // Find previous and next pages for navigation
256
+ const { prevPage, nextPage } = findPageNeighbors(config.navigation, normalizedPath);
257
+
306
258
  if (prevPage) {
307
- const cleanPath = prevPage.path.startsWith('/') ? prevPage.path.substring(1) : prevPage.path;
308
- prevPage.url = relativePathToRoot + (cleanPath.endsWith('/') ? cleanPath : cleanPath + '/');
309
- if (prevPage.path === '/') prevPage.url = relativePathToRoot;
259
+ const cleanPath = prevPage.path.substring(1);
260
+ prevPage.url = relativePathToRoot + cleanPath;
310
261
  }
311
262
 
312
263
  if (nextPage) {
313
- const cleanPath = nextPage.path.startsWith('/') ? nextPage.path.substring(1) : nextPage.path;
314
- nextPage.url = relativePathToRoot + (cleanPath.endsWith('/') ? cleanPath : cleanPath + '/');
315
- if (nextPage.path === '/') nextPage.url = relativePathToRoot;
264
+ const cleanPath = nextPage.path.substring(1);
265
+ nextPage.url = relativePathToRoot + cleanPath;
316
266
  }
317
267
 
268
+ // Log navigation paths for debugging
318
269
  const pageDataForTemplate = {
319
270
  content: htmlContent,
320
271
  pageTitle: pageFrontmatter.title || 'Untitled',
@@ -28,7 +28,7 @@ function formatPathForDisplay(absolutePath, cwd) {
28
28
  return relativePath;
29
29
  }
30
30
 
31
- async function startDevServer(configPathOption, options = { preserve: false }) {
31
+ async function startDevServer(configPathOption, options = { preserve: false, port: undefined }) {
32
32
  let config = await loadConfig(configPathOption); // Load initial config
33
33
  const CWD = process.cwd(); // Current Working Directory where user runs `docmd dev`
34
34
 
@@ -264,9 +264,9 @@ async function startDevServer(configPathOption, options = { preserve: false }) {
264
264
  watcher.on('error', error => console.error(`Watcher error: ${error}`));
265
265
 
266
266
  // Try different ports if the default port is in use
267
- const PORT = process.env.PORT || 3000;
267
+ const PORT = options.port || process.env.PORT || 3000;
268
268
  const MAX_PORT_ATTEMPTS = 10;
269
- let currentPort = PORT;
269
+ let currentPort = parseInt(PORT, 10);
270
270
 
271
271
  // Function to try starting the server on different ports
272
272
  function tryStartServer(port, attempt = 1) {
@@ -4,7 +4,7 @@ const fs = require('fs-extra');
4
4
  const path = require('path');
5
5
  const readline = require('readline');
6
6
 
7
- const defaultConfigContent = `// config.js: basic config for docmd
7
+ const defaultConfigContent = `// docmd.config.js: basic config for docmd
8
8
  module.exports = {
9
9
  // Core Site Metadata
10
10
  siteTitle: 'docmd',
@@ -108,6 +108,8 @@ module.exports = {
108
108
  { title: 'GitHub', path: 'https://github.com/mgks/docmd', icon: 'github', external: true },
109
109
  { title: 'Support the Project', path: 'https://github.com/sponsors/mgks', icon: 'heart', external: true },
110
110
  ],
111
+
112
+ pageNavigation: true, // Enable previous / next page navigation at the bottom of each page
111
113
 
112
114
  // Sponsor Ribbon Configuration
113
115
  Sponsor: {
@@ -139,7 +141,7 @@ Start writing your Markdown content here.
139
141
  async function initProject() {
140
142
  const baseDir = process.cwd();
141
143
  const docsDir = path.join(baseDir, 'docs');
142
- const configFile = path.join(baseDir, 'config.js');
144
+ const configFile = path.join(baseDir, 'docmd.config.js');
143
145
  const indexMdFile = path.join(docsDir, 'index.md');
144
146
  const assetsDir = path.join(baseDir, 'assets');
145
147
  const assetsCssDir = path.join(assetsDir, 'css');
@@ -154,6 +156,11 @@ async function initProject() {
154
156
 
155
157
  // Check each file individually
156
158
  if (await fs.pathExists(configFile)) {
159
+ existingFiles.push('docmd.config.js');
160
+ }
161
+ // Check for the config.js as well to warn the user
162
+ const oldConfigFile = path.join(baseDir, 'config.js');
163
+ if (await fs.pathExists(oldConfigFile)) {
157
164
  existingFiles.push('config.js');
158
165
  }
159
166
 
@@ -230,14 +237,11 @@ async function initProject() {
230
237
  }
231
238
 
232
239
  // Write config file if it doesn't exist or user confirmed override
233
- if (!await fs.pathExists(configFile)) {
234
- await fs.writeFile(configFile, defaultConfigContent, 'utf8');
235
- console.log('📄 Created `config.js`');
236
- } else if (shouldOverride) {
240
+ if (!await fs.pathExists(configFile) || shouldOverride) {
237
241
  await fs.writeFile(configFile, defaultConfigContent, 'utf8');
238
- console.log('📄 Updated `config.js`');
242
+ console.log(`📄 ${shouldOverride ? 'Updated' : 'Created'} \`docmd.config.js\``);
239
243
  } else {
240
- console.log('⏭️ Skipped existing `config.js`');
244
+ console.log('⏭️ Skipped existing `docmd.config.js`');
241
245
  }
242
246
 
243
247
  // Write index.md file if it doesn't exist or user confirmed override
@@ -14,12 +14,13 @@ async function loadConfig(configPath) {
14
14
  const config = require(absoluteConfigPath);
15
15
 
16
16
  // Basic validation and defaults
17
- if (!config.siteTitle) throw new Error('`siteTitle` is missing in config.js');
17
+ if (!config.siteTitle) throw new Error('`siteTitle` is missing in config file');
18
18
  config.srcDir = config.srcDir || 'docs';
19
19
  config.outputDir = config.outputDir || 'site';
20
20
  config.theme = config.theme || {};
21
21
  config.theme.defaultMode = config.theme.defaultMode || 'light';
22
22
  config.navigation = config.navigation || [{ title: 'Home', path: '/' }];
23
+ config.pageNavigation = config.pageNavigation ?? true;
23
24
 
24
25
  return config;
25
26
  } catch (e) {
@@ -34,6 +34,11 @@ function createMarkdownItInstance(config) {
34
34
  // Conditionally enable highlighting
35
35
  if (config.theme?.codeHighlight !== false) {
36
36
  mdOptions.highlight = function (str, lang) {
37
+ // Handle mermaid diagrams
38
+ if (lang === 'mermaid') {
39
+ return `<pre class="mermaid">${new MarkdownIt().utils.escapeHtml(str)}</pre>`;
40
+ }
41
+
37
42
  if (lang && hljs.getLanguage(lang)) {
38
43
  try {
39
44
  return `<pre class="hljs"><code>${hljs.highlight(str, { language: lang, ignoreIllegals: true }).value}</code></pre>`;
@@ -41,6 +46,14 @@ function createMarkdownItInstance(config) {
41
46
  }
42
47
  return `<pre class="hljs"><code>${new MarkdownIt().utils.escapeHtml(str)}</code></pre>`;
43
48
  };
49
+ } else {
50
+ // Even if highlighting is disabled, we need to handle mermaid
51
+ mdOptions.highlight = function (str, lang) {
52
+ if (lang === 'mermaid') {
53
+ return `<pre class="mermaid">${new MarkdownIt().utils.escapeHtml(str)}</pre>`;
54
+ }
55
+ return `<pre><code>${new MarkdownIt().utils.escapeHtml(str)}</code></pre>`;
56
+ };
44
57
  }
45
58
 
46
59
  const md = new MarkdownIt(mdOptions);
@@ -464,6 +477,11 @@ function enhancedTabsRule(state, startLine, endLine, silent) {
464
477
  typographer: true,
465
478
  breaks: true,
466
479
  highlight: function (str, lang) {
480
+ // Handle mermaid diagrams in tabs
481
+ if (lang === 'mermaid') {
482
+ return '<pre class="mermaid">' + MarkdownIt.utils.escapeHtml(str) + '</pre>';
483
+ }
484
+
467
485
  if (lang && hljs.getLanguage(lang)) {
468
486
  try {
469
487
  return '<pre class="hljs"><code>' +
@@ -1,4 +1,4 @@
1
- //
1
+ // Source file from the docmd project — https://github.com/mgks/docmd
2
2
 
3
3
  const ejs = require('ejs');
4
4
  const path = require('path');
@@ -124,7 +124,7 @@ async function generateHtmlPage(templateData) {
124
124
  prevPage,
125
125
  nextPage,
126
126
  currentPagePath,
127
- headings: headings || [],
127
+ headings: frontmatter.toc !== false ? (headings || []) : [],
128
128
  isActivePage,
129
129
  frontmatter,
130
130
  config: config,
@@ -0,0 +1,21 @@
1
+ // Source file from the docmd project — https://github.com/mgks/docmd
2
+
3
+ const chalk = require('chalk');
4
+
5
+ const { version } = require('../../package.json');
6
+
7
+ const printBanner = () => {
8
+ const logo = `
9
+
10
+ ${chalk.blue(' _ _ ')}
11
+ ${chalk.blue(' _| |___ ___ _____ _| |')}
12
+ ${chalk.blue(' | . | . | _| | . |')}
13
+ ${chalk.blue(' |___|___|___|_|_|_|___|')}
14
+ `;
15
+
16
+ console.log(logo);
17
+ console.log(` ${chalk.dim(`v${version}`)}`);
18
+ console.log(`\n`);
19
+ };
20
+
21
+ module.exports = { printBanner };
@@ -0,0 +1,62 @@
1
+ // Source file from the docmd project — https://github.com/mgks/docmd
2
+
3
+ /**
4
+ * Flattens the navigation tree and finds the previous and next pages relative to the current page.
5
+ * @param {Array} navItems - The navigation array from config.
6
+ * @param {string} currentPagePath - The normalized path of the current page.
7
+ * @returns {{prevPage: object|null, nextPage: object|null}}
8
+ */
9
+ function findPageNeighbors(navItems, currentPagePath) {
10
+ const flatNavigation = [];
11
+
12
+ // Recursive function to flatten the navigation tree
13
+ function extractNavigationItems(items) {
14
+ if (!items || !Array.isArray(items)) return;
15
+
16
+ for (const item of items) {
17
+ if (item.external || !item.path || item.path === '#') {
18
+ // If it's a category with no path but has children, recurse into them
19
+ if (item.children && Array.isArray(item.children)) {
20
+ extractNavigationItems(item.children);
21
+ }
22
+ continue; // Skip external links and parent items without a direct path
23
+ }
24
+
25
+ let normalizedItemPath = item.path;
26
+
27
+ // Ensure it starts with a slash
28
+ if (!normalizedItemPath.startsWith('/')) {
29
+ normalizedItemPath = '/' + normalizedItemPath;
30
+ }
31
+
32
+ // Ensure it ends with a slash (unless it's the root path)
33
+ if (normalizedItemPath.length > 1 && !normalizedItemPath.endsWith('/')) {
34
+ normalizedItemPath += '/';
35
+ }
36
+
37
+ flatNavigation.push({
38
+ title: item.title,
39
+ path: normalizedItemPath,
40
+ });
41
+
42
+ if (item.children && Array.isArray(item.children)) {
43
+ extractNavigationItems(item.children);
44
+ }
45
+ }
46
+ }
47
+
48
+ extractNavigationItems(navItems);
49
+
50
+ const currentPageIndex = flatNavigation.findIndex(item => item.path === currentPagePath);
51
+
52
+ if (currentPageIndex === -1) {
53
+ return { prevPage: null, nextPage: null };
54
+ }
55
+
56
+ const prevPage = currentPageIndex > 0 ? flatNavigation[currentPageIndex - 1] : null;
57
+ const nextPage = currentPageIndex < flatNavigation.length - 1 ? flatNavigation[currentPageIndex + 1] : null;
58
+
59
+ return { prevPage, nextPage };
60
+ }
61
+
62
+ module.exports = { findPageNeighbors };
@@ -79,7 +79,7 @@
79
79
  <div class="main-content">
80
80
  <%- content %>
81
81
 
82
- <% if (prevPage || nextPage) { %>
82
+ <% if (config.pageNavigation && (prevPage || nextPage)) { %>
83
83
  <div class="page-navigation">
84
84
  <% if (prevPage) { %>
85
85
  <a href="<%= prevPage.url %>" class="prev-page">
@@ -129,6 +129,10 @@
129
129
  </div>
130
130
 
131
131
  <script src="<%= relativePathToRoot %>assets/js/docmd-main.js"></script>
132
+
133
+ <!-- Mermaid.js for diagram rendering -->
134
+ <script src="https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.min.js"></script>
135
+ <script src="<%= relativePathToRoot %>assets/js/docmd-mermaid.js"></script>
132
136
 
133
137
  <% (customJsFiles || []).forEach(jsFile => { %>
134
138
  <script src="<%= relativePathToRoot %><%- jsFile.startsWith('/') ? jsFile.substring(1) : jsFile %>"></script>
@@ -15,7 +15,7 @@ function decodeHtmlEntities(html) {
15
15
  const shouldShowToc = typeof isActivePage !== 'undefined' ? isActivePage :
16
16
  (typeof navigationHtml !== 'undefined' && navigationHtml && navigationHtml.includes('class="active"'));
17
17
 
18
- if (shouldShowToc) {
18
+ if (shouldShowToc && !frontmatter?.toc || frontmatter?.toc !== 'false') {
19
19
  // If direct headings aren't available, we'll try to extract them from the content
20
20
  let tocHeadings = [];
21
21
  if (headings && headings.length > 0) {