@mdream/vite 0.12.0 → 0.12.2

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/dist/index.d.mts CHANGED
@@ -5,7 +5,7 @@ import { Plugin } from "vite";
5
5
  interface ViteHtmlToMarkdownOptions {
6
6
  /**
7
7
  * Glob patterns to include HTML files for processing
8
- * @default ['**\/*.html']
8
+ * @default ['*.html', '**\/*.html']
9
9
  */
10
10
  include?: string[];
11
11
  /**
@@ -27,18 +27,13 @@ interface ViteHtmlToMarkdownOptions {
27
27
  * Options to pass to mdream's htmlToMarkdown function
28
28
  */
29
29
  mdreamOptions?: HTMLToMarkdownOptions;
30
- /**
31
- * Whether to preserve directory structure in output
32
- * @default true
33
- */
34
- preserveStructure?: boolean;
35
30
  /**
36
31
  * Custom cache TTL in milliseconds for production
37
32
  * @default 3600000 (1 hour)
38
33
  */
39
34
  cacheTTL?: number;
40
35
  /**
41
- * Whether to log conversion activities
36
+ * Enable verbose logging for debugging
42
37
  * @default false
43
38
  */
44
39
  verbose?: boolean;
package/dist/index.mjs CHANGED
@@ -4,14 +4,13 @@ import { htmlToMarkdown } from "mdream";
4
4
 
5
5
  //#region src/plugin.ts
6
6
  const DEFAULT_OPTIONS = {
7
- include: ["**/*.html"],
7
+ include: ["*.html", "**/*.html"],
8
8
  exclude: ["**/node_modules/**"],
9
9
  outputDir: "",
10
10
  cacheEnabled: true,
11
11
  mdreamOptions: {},
12
- preserveStructure: true,
13
12
  cacheTTL: 36e5,
14
- verbose: true
13
+ verbose: false
15
14
  };
16
15
  function viteHtmlToMarkdownPlugin(userOptions = {}) {
17
16
  const options = {
@@ -109,46 +108,53 @@ function viteHtmlToMarkdownPlugin(userOptions = {}) {
109
108
  });
110
109
  }
111
110
  function shouldServeMarkdown(acceptHeader, secFetchDest) {
112
- const accept = acceptHeader || "";
113
111
  if (secFetchDest === "document") return false;
114
- if (accept.includes("text/html")) return false;
115
- return accept.includes("*/*") || accept.includes("text/markdown");
112
+ const accept = acceptHeader || "";
113
+ const hasHtml = accept.includes("text/html");
114
+ if (hasHtml) return false;
115
+ const hasWildcard = accept.includes("*/*");
116
+ return hasWildcard || accept.includes("text/markdown");
117
+ }
118
+ function createMarkdownMiddleware(getServer, getOutDir, cacheControl) {
119
+ return async (req, res, next) => {
120
+ const path$1 = new URL(req.url || "", "http://localhost").pathname;
121
+ const hasMarkdownExtension = path$1.endsWith(".md");
122
+ const clientPrefersMarkdown = shouldServeMarkdown(req.headers.accept, req.headers["sec-fetch-dest"]);
123
+ if (path$1.startsWith("/api") || path$1.startsWith("/_") || path$1.endsWith(".html")) return next();
124
+ if (!hasMarkdownExtension && !clientPrefersMarkdown) return next();
125
+ const url = req.url;
126
+ try {
127
+ const result = await handleMarkdownRequest(url, getServer(), getOutDir());
128
+ res.setHeader("Content-Type", "text/markdown; charset=utf-8");
129
+ res.setHeader("Cache-Control", cacheControl);
130
+ res.setHeader("X-Markdown-Source", result.source);
131
+ res.setHeader("X-Markdown-Cached", result.cached.toString());
132
+ res.end(result.content);
133
+ log(`Served ${url} from ${result.source} (cached: ${result.cached})`);
134
+ } catch (error) {
135
+ const message = error instanceof Error ? error.message : String(error);
136
+ log(`Error serving ${url}: ${message}`);
137
+ res.statusCode = 404;
138
+ res.end(`HTML content not found for ${url}`);
139
+ }
140
+ };
116
141
  }
117
142
  return {
118
143
  name: "vite-html-to-markdown",
119
144
  configureServer(server) {
120
- server.middlewares.use(async (req, res, next) => {
121
- const hasMarkdownExtension = req.url?.endsWith(".md");
122
- const clientPrefersMarkdown = shouldServeMarkdown(req.headers.accept, req.headers["sec-fetch-dest"]);
123
- if (!hasMarkdownExtension && !clientPrefersMarkdown) return next();
124
- const url = hasMarkdownExtension ? req.url : req.url;
125
- try {
126
- const result = await handleMarkdownRequest(url, server);
127
- res.setHeader("Content-Type", "text/markdown; charset=utf-8");
128
- res.setHeader("Cache-Control", "no-cache");
129
- res.setHeader("X-Markdown-Source", result.source);
130
- res.setHeader("X-Markdown-Cached", result.cached.toString());
131
- res.end(result.content);
132
- log(`Served ${url} from ${result.source} (cached: ${result.cached})`);
133
- } catch (error) {
134
- const message = error instanceof Error ? error.message : String(error);
135
- log(`Error serving ${url}: ${message}`);
136
- res.statusCode = 404;
137
- res.end(`HTML content not found for ${url}`);
138
- }
139
- });
145
+ server.middlewares.use(createMarkdownMiddleware(() => server, () => void 0, "no-cache"));
140
146
  },
141
- generateBundle(outputOptions, bundle) {
147
+ generateBundle(_outputOptions, bundle) {
142
148
  const htmlFiles = Object.entries(bundle).filter(([fileName, file]) => {
143
149
  return fileName.endsWith(".html") && file.type === "asset" && matchesPattern(fileName, options.include) && !matchesPattern(fileName, options.exclude);
144
150
  });
145
- log(`Processing ${htmlFiles.length} HTML files for markdown generation`);
151
+ if (htmlFiles.length > 0) log(`Processing ${htmlFiles.length} HTML files for markdown generation`);
146
152
  for (const [fileName, htmlFile] of htmlFiles) try {
147
153
  if (htmlFile.type !== "asset" || !("source" in htmlFile)) continue;
148
154
  const htmlContent = htmlFile.source;
149
155
  const markdownContent = htmlToMarkdown(htmlContent, options.mdreamOptions);
150
156
  const markdownFileName = fileName.replace(".html", ".md");
151
- const outputPath = options.preserveStructure ? `${options.outputDir}/${markdownFileName}` : `${options.outputDir}/${path.basename(markdownFileName)}`;
157
+ const outputPath = options.outputDir ? `${options.outputDir}/${markdownFileName}` : markdownFileName;
152
158
  this.emitFile({
153
159
  type: "asset",
154
160
  fileName: outputPath,
@@ -161,27 +167,7 @@ function viteHtmlToMarkdownPlugin(userOptions = {}) {
161
167
  }
162
168
  },
163
169
  configurePreviewServer(server) {
164
- server.middlewares.use(async (req, res, next) => {
165
- const hasMarkdownExtension = req.url?.endsWith(".md");
166
- const clientPrefersMarkdown = shouldServeMarkdown(req.headers.accept, req.headers["sec-fetch-dest"]);
167
- if (!hasMarkdownExtension && !clientPrefersMarkdown) return next();
168
- const url = hasMarkdownExtension ? req.url : req.url;
169
- try {
170
- const outDir = server.config.build?.outDir || "dist";
171
- const result = await handleMarkdownRequest(url, null, outDir);
172
- res.setHeader("Content-Type", "text/markdown; charset=utf-8");
173
- res.setHeader("Cache-Control", "public, max-age=3600");
174
- res.setHeader("X-Markdown-Source", result.source);
175
- res.setHeader("X-Markdown-Cached", result.cached.toString());
176
- res.end(result.content);
177
- log(`Served ${url} from ${result.source} (cached: ${result.cached})`);
178
- } catch (error) {
179
- const message = error instanceof Error ? error.message : String(error);
180
- log(`Error in preview server for ${url}: ${message}`);
181
- res.statusCode = 404;
182
- res.end(`HTML content not found for ${url}`);
183
- }
184
- });
170
+ server.middlewares.use(createMarkdownMiddleware(() => null, () => server.config.build?.outDir || "dist", "public, max-age=3600"));
185
171
  }
186
172
  };
187
173
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mdream/vite",
3
3
  "type": "module",
4
- "version": "0.12.0",
4
+ "version": "0.12.2",
5
5
  "description": "Vite plugin for HTML to Markdown conversion with on-demand generation",
6
6
  "author": {
7
7
  "name": "Harlan Wilton",
@@ -43,10 +43,10 @@
43
43
  "vite": "^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0"
44
44
  },
45
45
  "dependencies": {
46
- "mdream": "0.12.0"
46
+ "mdream": "0.12.2"
47
47
  },
48
48
  "devDependencies": {
49
- "@types/node": "^24.6.1",
49
+ "@types/node": "^24.6.2",
50
50
  "vite": "^7.1.7"
51
51
  },
52
52
  "scripts": {