@mdream/js 1.0.7 → 1.1.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/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
1
  import { C as TAG_H3, D as TEXT_NODE, E as TAG_H6, S as TAG_H2, T as TAG_H5, _ as TransformPlugin, a as ExtractedElement, b as NodeEventExit, c as MdreamOptions, d as NodeEvent, f as PluginContext, g as TextNode, h as TagOverride, i as EngineOptions, n as CleanOptions, o as FrontmatterConfig, p as SplitterOptions, r as ElementNode, s as MarkdownChunk, t as BuiltinPlugins, u as Node, v as ELEMENT_NODE, w as TAG_H4, x as TAG_H1, y as NodeEventEnter } from "./_chunks/types.mjs";
2
2
  import { n as streamHtmlToMarkdown, r as createPlugin, t as htmlToMarkdown } from "./_chunks/index.mjs";
3
3
  import { t as withMinimalPreset } from "./_chunks/minimal.mjs";
4
- export { BuiltinPlugins, CleanOptions, ELEMENT_NODE, ElementNode, EngineOptions, ExtractedElement, FrontmatterConfig, MarkdownChunk, MdreamOptions, Node, NodeEvent, NodeEventEnter, NodeEventExit, PluginContext, SplitterOptions, TAG_H1, TAG_H2, TAG_H3, TAG_H4, TAG_H5, TAG_H6, TEXT_NODE, TagOverride, TextNode, TransformPlugin, createPlugin, htmlToMarkdown, streamHtmlToMarkdown, withMinimalPreset };
4
+ export { type BuiltinPlugins, type CleanOptions, ELEMENT_NODE, type ElementNode, type EngineOptions, type ExtractedElement, type FrontmatterConfig, type MarkdownChunk, type MdreamOptions, type Node, type NodeEvent, NodeEventEnter, NodeEventExit, type PluginContext, type SplitterOptions, TAG_H1, TAG_H2, TAG_H3, TAG_H4, TAG_H5, TAG_H6, TEXT_NODE, type TagOverride, type TextNode, type TransformPlugin, createPlugin, htmlToMarkdown, streamHtmlToMarkdown, withMinimalPreset };
@@ -1,3 +1,4 @@
1
+ type ContentNegotiationResult = 'markdown' | 'html' | 'not-acceptable';
1
2
  interface AcceptEntry {
2
3
  type: string;
3
4
  q: number;
@@ -9,16 +10,27 @@ interface AcceptEntry {
9
10
  */
10
11
  declare function parseAcceptHeader(accept: string): AcceptEntry[];
11
12
  /**
12
- * Determine if a client prefers markdown over HTML using proper content negotiation.
13
+ * Perform RFC 7231 content negotiation for HTML vs Markdown.
13
14
  *
14
- * Uses Accept header quality weights and position ordering:
15
- * - If text/markdown or text/plain has higher quality than text/html -> markdown
16
- * - If same quality, earlier position in Accept header wins
17
- * - Bare wildcard does NOT trigger markdown (prevents breaking OG crawlers)
18
- * - sec-fetch-dest: document always returns false (browser navigation)
19
- *
20
- * @param acceptHeader - The HTTP Accept header value
21
- * @param secFetchDest - The Sec-Fetch-Dest header value
15
+ * Resolution rules:
16
+ * - `Sec-Fetch-Dest: document` always returns `'html'` (browser navigation).
17
+ * - Missing or empty Accept header returns `'html'` (server picks default).
18
+ * - q=0 entries are treated as explicit rejections and ignored for matching
19
+ * (but still count towards "something was listed").
20
+ * - `text/markdown` and `text/plain` are the markdown-capable types.
21
+ * - `text/html` and `application/xhtml+xml` are the html-capable types.
22
+ * - `*_/_*` and `text/*` are wildcards; they satisfy 406 but never on their
23
+ * own tip negotiation towards markdown (preserves OG crawler behavior).
24
+ * - If nothing in the Accept header can be served (no explicit match, no
25
+ * wildcard), returns `'not-acceptable'` so the caller can send 406.
26
+ * - Otherwise, compares best markdown entry vs best html-or-wildcard entry
27
+ * by q, then by position.
28
+ */
29
+ declare function negotiateContent(acceptHeader?: string, secFetchDest?: string): ContentNegotiationResult;
30
+ /**
31
+ * Determine if a client prefers markdown over HTML. Convenience wrapper over
32
+ * {@link negotiateContent}; treats `'not-acceptable'` the same as `'html'`
33
+ * (callers that want 406 semantics should use `negotiateContent` directly).
22
34
  */
23
35
  declare function shouldServeMarkdown(acceptHeader?: string, secFetchDest?: string): boolean;
24
- export { parseAcceptHeader, shouldServeMarkdown };
36
+ export { ContentNegotiationResult, negotiateContent, parseAcceptHeader, shouldServeMarkdown };
@@ -28,18 +28,25 @@ function parseAcceptHeader(accept) {
28
28
  }
29
29
  return entries;
30
30
  }
31
- function shouldServeMarkdown(acceptHeader, secFetchDest) {
32
- if (secFetchDest === "document") return false;
31
+ function negotiateContent(acceptHeader, secFetchDest) {
32
+ if (secFetchDest === "document") return "html";
33
33
  const accept = acceptHeader || "";
34
- if (!accept) return false;
35
- const parts = accept.split(",");
34
+ if (!accept) return "html";
36
35
  let bestMdQ = -1;
37
36
  let bestMdPos = -1;
38
- let htmlQ = -1;
39
- let htmlPos = -1;
37
+ let bestHtmlQ = -1;
38
+ let bestHtmlPos = -1;
39
+ let bestWildcardQ = -1;
40
+ let bestWildcardPos = -1;
41
+ let sawAnyEntry = false;
42
+ let sawAcceptable = false;
43
+ let rejectedMd = false;
44
+ let rejectedHtml = false;
45
+ const parts = accept.split(",");
40
46
  for (let i = 0; i < parts.length; i++) {
41
47
  const part = parts[i].trim();
42
48
  if (!part) continue;
49
+ sawAnyEntry = true;
43
50
  const semicolonIdx = part.indexOf(";");
44
51
  let type;
45
52
  let q = 1;
@@ -47,7 +54,14 @@ function shouldServeMarkdown(acceptHeader, secFetchDest) {
47
54
  else {
48
55
  type = part.slice(0, semicolonIdx).trim();
49
56
  const paramStr = part.slice(semicolonIdx + 1);
50
- const qIdx = paramStr.indexOf("q=");
57
+ let qIdx = -1;
58
+ for (let j = 0; j < paramStr.length - 1; j++) {
59
+ const c = paramStr.charCodeAt(j);
60
+ if ((c === 113 || c === 81) && paramStr.charCodeAt(j + 1) === 61) {
61
+ qIdx = j;
62
+ break;
63
+ }
64
+ }
51
65
  if (qIdx !== -1) {
52
66
  const qStart = qIdx + 2;
53
67
  let qEnd = qStart;
@@ -55,20 +69,53 @@ function shouldServeMarkdown(acceptHeader, secFetchDest) {
55
69
  q = +paramStr.slice(qStart, qEnd) || 0;
56
70
  }
57
71
  }
58
- if (type === "text/markdown" || type === "text/plain") {
59
- if (q > bestMdQ || q === bestMdQ && (bestMdPos === -1 || i < bestMdPos)) {
72
+ const normalized = type.toLowerCase();
73
+ if (normalized === "text/markdown" || normalized === "text/plain") {
74
+ if (q === 0) {
75
+ rejectedMd = true;
76
+ continue;
77
+ }
78
+ sawAcceptable = true;
79
+ if (q > bestMdQ || q === bestMdQ && bestMdPos === -1) {
60
80
  bestMdQ = q;
61
81
  bestMdPos = i;
62
82
  }
63
- } else if (type === "text/html") {
64
- htmlQ = q;
65
- htmlPos = i;
83
+ } else if (normalized === "text/html" || normalized === "application/xhtml+xml") {
84
+ if (q === 0) {
85
+ rejectedHtml = true;
86
+ continue;
87
+ }
88
+ sawAcceptable = true;
89
+ if (q > bestHtmlQ || q === bestHtmlQ && bestHtmlPos === -1) {
90
+ bestHtmlQ = q;
91
+ bestHtmlPos = i;
92
+ }
93
+ } else if (normalized === "*/*" || normalized === "text/*") {
94
+ if (q === 0) continue;
95
+ sawAcceptable = true;
96
+ if (q > bestWildcardQ || q === bestWildcardQ && bestWildcardPos === -1) {
97
+ bestWildcardQ = q;
98
+ bestWildcardPos = i;
99
+ }
66
100
  }
67
101
  }
68
- if (bestMdPos === -1) return false;
69
- if (htmlPos === -1) return true;
70
- if (bestMdQ > htmlQ) return true;
71
- if (bestMdQ === htmlQ && bestMdPos < htmlPos) return true;
72
- return false;
102
+ if (sawAnyEntry && !sawAcceptable) return "not-acceptable";
103
+ if (bestMdPos === -1 && !rejectedMd && bestWildcardPos !== -1) {
104
+ bestMdQ = bestWildcardQ;
105
+ bestMdPos = bestWildcardPos;
106
+ }
107
+ if (bestHtmlPos === -1 && !rejectedHtml && bestWildcardPos !== -1) {
108
+ bestHtmlQ = bestWildcardQ;
109
+ bestHtmlPos = bestWildcardPos;
110
+ }
111
+ if (bestMdPos === -1 && bestHtmlPos === -1) return "not-acceptable";
112
+ if (bestMdPos === -1) return "html";
113
+ if (bestHtmlPos === -1) return "markdown";
114
+ if (bestMdQ > bestHtmlQ) return "markdown";
115
+ if (bestMdQ === bestHtmlQ && bestMdPos < bestHtmlPos) return "markdown";
116
+ return "html";
117
+ }
118
+ function shouldServeMarkdown(acceptHeader, secFetchDest) {
119
+ return negotiateContent(acceptHeader, secFetchDest) === "markdown";
73
120
  }
74
- export { parseAcceptHeader, shouldServeMarkdown };
121
+ export { negotiateContent, parseAcceptHeader, shouldServeMarkdown };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mdream/js",
3
3
  "type": "module",
4
- "version": "1.0.7",
4
+ "version": "1.1.1",
5
5
  "description": "JavaScript HTML-to-Markdown engine for mdream. Escape hatch for hooks and edge runtimes.",
6
6
  "author": {
7
7
  "name": "Harlan Wilton",