@mdream/vite 0.12.1 → 0.12.3

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,57 @@ 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.startsWith("/@")) return next();
124
+ const lastSegment = path$1.split("/").pop() || "";
125
+ const hasExtension = lastSegment.includes(".");
126
+ const extension = hasExtension ? lastSegment.substring(lastSegment.lastIndexOf(".")) : "";
127
+ if (hasExtension && extension !== ".md") return next();
128
+ if (!hasMarkdownExtension && !clientPrefersMarkdown) return next();
129
+ const url = req.url;
130
+ try {
131
+ const result = await handleMarkdownRequest(url, getServer(), getOutDir());
132
+ res.setHeader("Content-Type", "text/markdown; charset=utf-8");
133
+ res.setHeader("Cache-Control", cacheControl);
134
+ res.setHeader("X-Markdown-Source", result.source);
135
+ res.setHeader("X-Markdown-Cached", result.cached.toString());
136
+ res.end(result.content);
137
+ log(`Served ${url} from ${result.source} (cached: ${result.cached})`);
138
+ } catch (error) {
139
+ const message = error instanceof Error ? error.message : String(error);
140
+ log(`Error serving ${url}: ${message}`);
141
+ res.statusCode = 404;
142
+ res.end(`HTML content not found for ${url}`);
143
+ }
144
+ };
116
145
  }
117
146
  return {
118
147
  name: "vite-html-to-markdown",
119
148
  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
- });
149
+ server.middlewares.use(createMarkdownMiddleware(() => server, () => void 0, "no-cache"));
140
150
  },
141
- generateBundle(outputOptions, bundle) {
151
+ generateBundle(_outputOptions, bundle) {
142
152
  const htmlFiles = Object.entries(bundle).filter(([fileName, file]) => {
143
153
  return fileName.endsWith(".html") && file.type === "asset" && matchesPattern(fileName, options.include) && !matchesPattern(fileName, options.exclude);
144
154
  });
145
- log(`Processing ${htmlFiles.length} HTML files for markdown generation`);
155
+ if (htmlFiles.length > 0) log(`Processing ${htmlFiles.length} HTML files for markdown generation`);
146
156
  for (const [fileName, htmlFile] of htmlFiles) try {
147
157
  if (htmlFile.type !== "asset" || !("source" in htmlFile)) continue;
148
158
  const htmlContent = htmlFile.source;
149
159
  const markdownContent = htmlToMarkdown(htmlContent, options.mdreamOptions);
150
160
  const markdownFileName = fileName.replace(".html", ".md");
151
- const outputPath = options.preserveStructure ? `${options.outputDir}/${markdownFileName}` : `${options.outputDir}/${path.basename(markdownFileName)}`;
161
+ const outputPath = options.outputDir ? `${options.outputDir}/${markdownFileName}` : markdownFileName;
152
162
  this.emitFile({
153
163
  type: "asset",
154
164
  fileName: outputPath,
@@ -161,27 +171,7 @@ function viteHtmlToMarkdownPlugin(userOptions = {}) {
161
171
  }
162
172
  },
163
173
  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
- });
174
+ server.middlewares.use(createMarkdownMiddleware(() => null, () => server.config.build?.outDir || "dist", "public, max-age=3600"));
185
175
  }
186
176
  };
187
177
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mdream/vite",
3
3
  "type": "module",
4
- "version": "0.12.1",
4
+ "version": "0.12.3",
5
5
  "description": "Vite plugin for HTML to Markdown conversion with on-demand generation",
6
6
  "author": {
7
7
  "name": "Harlan Wilton",
@@ -43,11 +43,11 @@
43
43
  "vite": "^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0"
44
44
  },
45
45
  "dependencies": {
46
- "mdream": "0.12.1"
46
+ "mdream": "0.12.3"
47
47
  },
48
48
  "devDependencies": {
49
49
  "@types/node": "^24.6.2",
50
- "vite": "^7.1.7"
50
+ "vite": "^7.1.8"
51
51
  },
52
52
  "scripts": {
53
53
  "build": "obuild",