markdown-pdf-export 0.0.0-stage → 2.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 +21 -0
- package/README.md +91 -2
- package/cli.mjs +695 -0
- package/default.css +230 -0
- package/export.mjs +1369 -0
- package/lib/asset-resolver.mjs +493 -0
- package/lib/config-merger.mjs +488 -0
- package/lib/doctor-report.mjs +52 -0
- package/lib/editor-setup.mjs +280 -0
- package/lib/frontmatter-parser.mjs +595 -0
- package/lib/html-generator.mjs +837 -0
- package/lib/looks.mjs +150 -0
- package/lib/placeholder-resolver.mjs +490 -0
- package/lib/schema-validator.mjs +725 -0
- package/lib/settings-loader.mjs +167 -0
- package/lib/settings-schema.mjs +192 -0
- package/lib/studio-server.mjs +490 -0
- package/lib/types.mjs +334 -0
- package/looks/academic.css +8 -0
- package/looks/compact.css +5 -0
- package/looks/report.css +8 -0
- package/looks/technical.css +11 -0
- package/mcp.mjs +263 -0
- package/package.json +97 -4
- package/schema/settings.schema.json +768 -0
- package/studio/app.mjs +589 -0
- package/studio/index.html +113 -0
- package/studio/model.mjs +316 -0
- package/studio/sample.md +53 -0
- package/studio/style.css +497 -0
- package/templates/academic.json +26 -0
- package/templates/corporate.json +24 -0
- package/templates/minimal.json +6 -0
- package/templates/simple.json +12 -0
- package/templates/two-logo.json +27 -0
- package/tool-schemas.mjs +215 -0
- package/vendor/katex-inline.css +1 -0
package/lib/types.mjs
ADDED
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview JSDoc type definitions for structured header/footer configuration.
|
|
3
|
+
*
|
|
4
|
+
* This module defines the types for the declarative header/footer system that
|
|
5
|
+
* replaces raw HTML templates with a structured configuration.
|
|
6
|
+
*
|
|
7
|
+
* @module types
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
11
|
+
// Element Types
|
|
12
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Text element - displays static or dynamic text with placeholders.
|
|
16
|
+
*
|
|
17
|
+
* @typedef {Object} TextElement
|
|
18
|
+
* @property {'text'} type - Element type identifier
|
|
19
|
+
* @property {string} content - Text content, supports placeholders like {page}, {date}, {title}
|
|
20
|
+
* @property {string} [font_size] - CSS font-size, e.g., "10px", "9pt"
|
|
21
|
+
* @property {string|number} [font_weight] - CSS font-weight, e.g., "bold", "normal", 600
|
|
22
|
+
* @property {'normal'|'italic'|'oblique'} [font_style] - CSS font-style
|
|
23
|
+
* @property {string} [color] - CSS color, e.g., "#333", "gray", "rgb(100,100,100)"
|
|
24
|
+
* @property {string} [style] - Additional inline CSS styles
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Image element - displays an image from a file path.
|
|
29
|
+
*
|
|
30
|
+
* @typedef {Object} ImageElement
|
|
31
|
+
* @property {'image'} type - Element type identifier
|
|
32
|
+
* @property {string} src - Path to image file (relative to Markdown file), supports SVG, PNG, JPG
|
|
33
|
+
* @property {string} [height] - CSS height, e.g., "20px", "5mm"
|
|
34
|
+
* @property {string} [width] - CSS width (usually omit to preserve aspect ratio)
|
|
35
|
+
* @property {string} [alt] - Alt text for accessibility
|
|
36
|
+
* @property {string} [style] - Additional inline CSS styles
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Page number element - displays the current page number.
|
|
41
|
+
*
|
|
42
|
+
* @typedef {Object} PageNumberElement
|
|
43
|
+
* @property {'page_number'} type - Element type identifier
|
|
44
|
+
* @property {string} [format] - Format string, e.g., "Page {page}" (default is just the number)
|
|
45
|
+
* @property {string} [font_size] - CSS font-size
|
|
46
|
+
* @property {string} [color] - CSS color
|
|
47
|
+
* @property {string} [style] - Additional inline CSS styles
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Total pages element - displays the total number of pages.
|
|
52
|
+
*
|
|
53
|
+
* @typedef {Object} TotalPagesElement
|
|
54
|
+
* @property {'total_pages'} type - Element type identifier
|
|
55
|
+
* @property {string} [format] - Format string, e.g., "{pages} pages"
|
|
56
|
+
* @property {string} [font_size] - CSS font-size
|
|
57
|
+
* @property {string} [color] - CSS color
|
|
58
|
+
* @property {string} [style] - Additional inline CSS styles
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Date element - displays a formatted date/time.
|
|
63
|
+
*
|
|
64
|
+
* @typedef {Object} DateElement
|
|
65
|
+
* @property {'date'} type - Element type identifier
|
|
66
|
+
* @property {string} [format] - date-fns format string, e.g., "yyyy-MM-dd", "MMMM d, yyyy", "MM/dd/yyyy"
|
|
67
|
+
* @property {string} [font_size] - CSS font-size
|
|
68
|
+
* @property {string} [color] - CSS color
|
|
69
|
+
* @property {string} [style] - Additional inline CSS styles
|
|
70
|
+
*/
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Title element - displays the document title.
|
|
74
|
+
*
|
|
75
|
+
* @typedef {Object} TitleElement
|
|
76
|
+
* @property {'title'} type - Element type identifier
|
|
77
|
+
* @property {string} [font_size] - CSS font-size
|
|
78
|
+
* @property {string|number} [font_weight] - CSS font-weight
|
|
79
|
+
* @property {string} [color] - CSS color
|
|
80
|
+
* @property {string} [fallback] - Fallback text if no title is found
|
|
81
|
+
* @property {string} [style] - Additional inline CSS styles
|
|
82
|
+
*/
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Spacer element - adds flexible or fixed space between elements.
|
|
86
|
+
*
|
|
87
|
+
* @typedef {Object} SpacerElement
|
|
88
|
+
* @property {'spacer'} type - Element type identifier
|
|
89
|
+
* @property {string} [width] - Fixed width, e.g., "10mm", "20px". If omitted, uses flex: 1
|
|
90
|
+
*/
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Any element that can appear in a header/footer zone.
|
|
94
|
+
*
|
|
95
|
+
* @typedef {TextElement|ImageElement|PageNumberElement|TotalPagesElement|DateElement|TitleElement|SpacerElement} Element
|
|
96
|
+
*/
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* A zone can contain a single element or an array of elements.
|
|
100
|
+
*
|
|
101
|
+
* @typedef {Element|Element[]} Zone
|
|
102
|
+
*/
|
|
103
|
+
|
|
104
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
105
|
+
// Header/Footer Structure
|
|
106
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Structured configuration for a header or footer.
|
|
110
|
+
*
|
|
111
|
+
* @typedef {Object} HeaderFooterConfig
|
|
112
|
+
* @property {string} [height] - Height of the header/footer area, e.g., "15mm", "0.5in"
|
|
113
|
+
* @property {string} [padding] - CSS padding, e.g., "0 10mm"
|
|
114
|
+
* @property {string} [font_family] - CSS font-family for all text in this section
|
|
115
|
+
* @property {string} [font_size] - Default font-size for all elements
|
|
116
|
+
* @property {string} [color] - Default text color
|
|
117
|
+
* @property {string} [border_bottom] - For header: bottom border, e.g., "1px solid #ccc"
|
|
118
|
+
* @property {string} [border_top] - For footer: top border, e.g., "1px solid #ccc"
|
|
119
|
+
* @property {string} [background] - Background color or CSS background value
|
|
120
|
+
* @property {string} [style] - Additional inline CSS styles for the container
|
|
121
|
+
*
|
|
122
|
+
* @property {Zone} [left] - Left zone content (single element or array)
|
|
123
|
+
* @property {Zone} [center] - Center zone content (single element or array)
|
|
124
|
+
* @property {Zone} [right] - Right zone content (single element or array)
|
|
125
|
+
*
|
|
126
|
+
* // Shorthand properties (alternative to full element definitions)
|
|
127
|
+
* @property {string} [left_text] - Shorthand: text content for left zone
|
|
128
|
+
* @property {string} [left_image] - Shorthand: image path for left zone
|
|
129
|
+
* @property {string} [left_image_height] - Shorthand: height for left_image
|
|
130
|
+
* @property {string} [center_text] - Shorthand: text content for center zone
|
|
131
|
+
* @property {string} [center_image] - Shorthand: image path for center zone
|
|
132
|
+
* @property {string} [center_image_height] - Shorthand: height for center_image
|
|
133
|
+
* @property {string} [right_text] - Shorthand: text content for right zone
|
|
134
|
+
* @property {string} [right_image] - Shorthand: image path for right zone
|
|
135
|
+
* @property {string} [right_image_height] - Shorthand: height for right_image
|
|
136
|
+
*/
|
|
137
|
+
|
|
138
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
139
|
+
// Front Matter Configuration
|
|
140
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* PDF configuration block that can appear in front matter under `pdf:`.
|
|
144
|
+
*
|
|
145
|
+
* @typedef {Object} FrontMatterPdfConfig
|
|
146
|
+
* @property {HeaderFooterConfig} [header] - Header configuration
|
|
147
|
+
* @property {HeaderFooterConfig} [footer] - Footer configuration
|
|
148
|
+
* @property {boolean} [display_header_footer] - Enable/disable header and footer
|
|
149
|
+
*
|
|
150
|
+
* // Additional overridable settings from front matter
|
|
151
|
+
* @property {string} [page_format] - Paper size (A4, Letter, etc.)
|
|
152
|
+
* @property {'portrait'|'landscape'} [orientation] - Page orientation
|
|
153
|
+
* @property {number} [scale] - Zoom factor (0.1 - 2)
|
|
154
|
+
* @property {string} [page_ranges] - Pages to print, e.g., "1-5, 8"
|
|
155
|
+
* @property {boolean} [print_background] - Print background colors/images
|
|
156
|
+
* @property {MarginConfig} [margin] - Page margins
|
|
157
|
+
* @property {string} [font_family] - CSS font-family
|
|
158
|
+
* @property {string} [stylesheet_path] - Custom CSS file path
|
|
159
|
+
* @property {boolean} [include_default_styles] - Include built-in styles
|
|
160
|
+
* @property {boolean} [highlight] - Enable syntax highlighting
|
|
161
|
+
* @property {string} [highlight_style] - Highlight.js theme
|
|
162
|
+
* @property {boolean} [breaks] - Convert newlines to <br>
|
|
163
|
+
* @property {boolean} [emoji] - Render emoji shortcodes
|
|
164
|
+
*/
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Page margin configuration.
|
|
168
|
+
*
|
|
169
|
+
* @typedef {Object} MarginConfig
|
|
170
|
+
* @property {string} [top] - Top margin, e.g., "15mm", "0.5in"
|
|
171
|
+
* @property {string} [right] - Right margin
|
|
172
|
+
* @property {string} [bottom] - Bottom margin
|
|
173
|
+
* @property {string} [left] - Left margin
|
|
174
|
+
*/
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Front matter extracted from a Markdown document.
|
|
178
|
+
*
|
|
179
|
+
* @typedef {Object} ParsedFrontMatter
|
|
180
|
+
* @property {string} [title] - Document title
|
|
181
|
+
* @property {string} [author] - Document author
|
|
182
|
+
* @property {string} [date] - Document date
|
|
183
|
+
* @property {FrontMatterPdfConfig} [markdown-pdf] - PDF export configuration
|
|
184
|
+
* @property {Object.<string, any>} [custom] - Any other custom variables (accessible as {varname} placeholders)
|
|
185
|
+
*/
|
|
186
|
+
|
|
187
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
188
|
+
// Configuration Context
|
|
189
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Context passed to the HTML generator for placeholder resolution.
|
|
193
|
+
*
|
|
194
|
+
* @typedef {Object} RenderContext
|
|
195
|
+
* @property {string} title - Document title (from front matter, H1, or filename)
|
|
196
|
+
* @property {string} [author] - Document author from front matter
|
|
197
|
+
* @property {string} filename - Source filename without path
|
|
198
|
+
* @property {string} inputPath - Full path to the input Markdown file
|
|
199
|
+
* @property {Date} now - Current date/time for date placeholders
|
|
200
|
+
* @property {Object.<string, string>} [customVariables] - Custom front matter variables
|
|
201
|
+
*/
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Result from asset resolution (image path to data URI).
|
|
205
|
+
*
|
|
206
|
+
* @typedef {Object} ResolvedAsset
|
|
207
|
+
* @property {string} dataUri - Base64 data URI
|
|
208
|
+
* @property {string} mimeType - MIME type (image/svg+xml, image/png, image/jpeg)
|
|
209
|
+
* @property {number} size - Original file size in bytes
|
|
210
|
+
* @property {boolean} oversized - True if size exceeds warning threshold
|
|
211
|
+
*/
|
|
212
|
+
|
|
213
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
214
|
+
// Validation
|
|
215
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Validation error for structured configuration.
|
|
219
|
+
*
|
|
220
|
+
* @typedef {Object} ValidationError
|
|
221
|
+
* @property {string} path - JSON path to the invalid property, e.g., "header.left[0].type"
|
|
222
|
+
* @property {string} message - Human-readable error message
|
|
223
|
+
* @property {any} [value] - The invalid value (if applicable)
|
|
224
|
+
*/
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Result of validating a structured configuration.
|
|
228
|
+
*
|
|
229
|
+
* @typedef {Object} ValidationResult
|
|
230
|
+
* @property {boolean} valid - True if configuration is valid
|
|
231
|
+
* @property {ValidationError[]} errors - Array of validation errors (empty if valid)
|
|
232
|
+
* @property {ValidationError[]} warnings - Array of validation warnings (non-fatal issues)
|
|
233
|
+
*/
|
|
234
|
+
|
|
235
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
236
|
+
// Constants
|
|
237
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Valid element types.
|
|
241
|
+
* @type {readonly string[]}
|
|
242
|
+
*/
|
|
243
|
+
export const ELEMENT_TYPES = Object.freeze([
|
|
244
|
+
"text",
|
|
245
|
+
"image",
|
|
246
|
+
"page_number",
|
|
247
|
+
"total_pages",
|
|
248
|
+
"date",
|
|
249
|
+
"title",
|
|
250
|
+
"spacer",
|
|
251
|
+
]);
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Supported image formats with their MIME types.
|
|
255
|
+
* @type {Readonly<Object.<string, string>>}
|
|
256
|
+
*/
|
|
257
|
+
export const IMAGE_FORMATS = Object.freeze({
|
|
258
|
+
".svg": "image/svg+xml",
|
|
259
|
+
".png": "image/png",
|
|
260
|
+
".jpg": "image/jpeg",
|
|
261
|
+
".jpeg": "image/jpeg",
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Default image size warning threshold (500KB).
|
|
266
|
+
* @type {number}
|
|
267
|
+
*/
|
|
268
|
+
export const IMAGE_SIZE_WARNING_THRESHOLD = 500 * 1024;
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Placeholder pattern for matching {name} and {name:format} syntax.
|
|
272
|
+
* @type {RegExp}
|
|
273
|
+
*/
|
|
274
|
+
export const PLACEHOLDER_PATTERN = /\{(\w+)(?::([^}]+))?\}/g;
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Built-in placeholders that have special handling.
|
|
278
|
+
* @type {readonly string[]}
|
|
279
|
+
*/
|
|
280
|
+
export const BUILTIN_PLACEHOLDERS = Object.freeze([
|
|
281
|
+
"page",
|
|
282
|
+
"pages",
|
|
283
|
+
"date",
|
|
284
|
+
"time",
|
|
285
|
+
"datetime",
|
|
286
|
+
"title",
|
|
287
|
+
"filename",
|
|
288
|
+
"author",
|
|
289
|
+
]);
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Shorthand property mappings to full element definitions.
|
|
293
|
+
* Maps shorthand keys to their zone and element type.
|
|
294
|
+
*
|
|
295
|
+
* @type {Readonly<Object.<string, {zone: string, type: string, property: string}>>}
|
|
296
|
+
*/
|
|
297
|
+
export const SHORTHAND_MAPPINGS = Object.freeze({
|
|
298
|
+
left_text: { zone: "left", type: "text", property: "content" },
|
|
299
|
+
left_image: { zone: "left", type: "image", property: "src" },
|
|
300
|
+
left_image_height: { zone: "left", type: "image", property: "height" },
|
|
301
|
+
center_text: { zone: "center", type: "text", property: "content" },
|
|
302
|
+
center_image: { zone: "center", type: "image", property: "src" },
|
|
303
|
+
center_image_height: { zone: "center", type: "image", property: "height" },
|
|
304
|
+
right_text: { zone: "right", type: "text", property: "content" },
|
|
305
|
+
right_image: { zone: "right", type: "image", property: "src" },
|
|
306
|
+
right_image_height: { zone: "right", type: "image", property: "height" },
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Default header configuration when using structured config but no explicit header.
|
|
311
|
+
* @type {null}
|
|
312
|
+
*/
|
|
313
|
+
export const DEFAULT_HEADER = null;
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Default footer configuration when using structured config.
|
|
317
|
+
* Shows centered page number.
|
|
318
|
+
*
|
|
319
|
+
* @type {HeaderFooterConfig}
|
|
320
|
+
*/
|
|
321
|
+
export const DEFAULT_FOOTER = Object.freeze({
|
|
322
|
+
height: "10mm",
|
|
323
|
+
center: {
|
|
324
|
+
type: "text",
|
|
325
|
+
content: "{page} / {pages}",
|
|
326
|
+
font_size: "9px",
|
|
327
|
+
},
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* VitePress documentation URL.
|
|
332
|
+
* @type {string}
|
|
333
|
+
*/
|
|
334
|
+
export const DOCS_URL = "https://matinfo.github.io/markdown-pdf-export/";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/* Look: academic — serif body, justified text, minimal colour. */
|
|
2
|
+
:root { font-family: "Times New Roman", Times, Georgia, serif; font-size: 13px; line-height: 1.65; color: #111; }
|
|
3
|
+
main p, main li { text-align: justify; hyphens: auto; }
|
|
4
|
+
h1, h2, h3, h4 { color: #111; border-bottom: none; }
|
|
5
|
+
h1 { text-align: center; font-size: 1.8em; }
|
|
6
|
+
a { color: #111; text-decoration: underline; }
|
|
7
|
+
table { border-collapse: collapse; margin: 1em auto; }
|
|
8
|
+
th, td { border: none; border-top: 1px solid #111; border-bottom: 1px solid #111; }
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/* Look: compact — dense, small type, fits more per page. */
|
|
2
|
+
:root { font-size: 11.5px; line-height: 1.45; }
|
|
3
|
+
h1 { font-size: 1.6em; } h2 { font-size: 1.3em; } h3 { font-size: 1.1em; }
|
|
4
|
+
h1, h2, h3, h4 { margin: 1em 0 0.35em; }
|
|
5
|
+
p, ul, ol, pre, table, blockquote { margin-top: 0; margin-bottom: 0.7em; }
|
package/looks/report.css
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/* Look: report — serif headings, restrained rules, roomy paragraphs. */
|
|
2
|
+
:root { line-height: 1.7; }
|
|
3
|
+
h1, h2, h3 { font-family: Georgia, "Times New Roman", serif; letter-spacing: -0.01em; }
|
|
4
|
+
h1 { border-bottom: 2px solid #24292e; }
|
|
5
|
+
h2 { border-bottom: 1px solid #d0d7de; }
|
|
6
|
+
table { border-collapse: collapse; }
|
|
7
|
+
th { background: #f6f8fa; }
|
|
8
|
+
blockquote { border-left: 4px solid #0969da; color: #57606a; }
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/* Look: technical — for documents with code: framed code blocks, blue accents. */
|
|
2
|
+
:root { font-size: 13px; }
|
|
3
|
+
h1 { border-bottom: 3px solid #0969da; }
|
|
4
|
+
h2 { border-bottom: none; color: #0550ae; }
|
|
5
|
+
pre.hljs, pre:not(.hljs) {
|
|
6
|
+
border: 1px solid #d0d7de;
|
|
7
|
+
border-left: 3px solid #0969da;
|
|
8
|
+
border-radius: 4px;
|
|
9
|
+
}
|
|
10
|
+
:not(pre) > code { color: #0550ae; background: #eef4fb; }
|
|
11
|
+
th { background: #eef4fb; }
|
package/mcp.mjs
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Markdown PDF MCP server (stdio).
|
|
3
|
+
*
|
|
4
|
+
* Thin transport around export.mjs: framing, JSON-RPC dispatch, two tools.
|
|
5
|
+
* Settings: user file (~/.config/markdown-pdf-export/settings.json) or the
|
|
6
|
+
* MARKDOWN_PDF_SETTINGS env var, plus the nearest .markdown-pdf.json — see
|
|
7
|
+
* lib/settings-loader.mjs.
|
|
8
|
+
*/
|
|
9
|
+
import process from "node:process";
|
|
10
|
+
import { fileURLToPath } from "node:url";
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
SERVER_NAME,
|
|
14
|
+
SERVER_VERSION,
|
|
15
|
+
debugLog,
|
|
16
|
+
exportMarkdownPdf,
|
|
17
|
+
doctorMarkdownPdf,
|
|
18
|
+
} from "./export.mjs";
|
|
19
|
+
import { TOOLS } from "./tool-schemas.mjs";
|
|
20
|
+
|
|
21
|
+
debugLog(`server starting — pid=${process.pid} node=${process.execPath}`);
|
|
22
|
+
debugLog(`argv=${JSON.stringify(process.argv)}`);
|
|
23
|
+
debugLog(`cwd=${process.cwd()}`);
|
|
24
|
+
debugLog(`__filename=${fileURLToPath(import.meta.url)}`);
|
|
25
|
+
|
|
26
|
+
// ── MCP stdio framing ─────────────────────────────────────────────────────────
|
|
27
|
+
//
|
|
28
|
+
// The MCP specification (2024-11-05) defines the stdio transport as
|
|
29
|
+
// newline-delimited JSON (NDJSON): one JSON object per line, separated
|
|
30
|
+
// by '\n'. Some older clients/tooling still use LSP-style
|
|
31
|
+
// "Content-Length" framing. We auto-detect the framing on the very
|
|
32
|
+
// first non-whitespace byte of stdin and stick with that mode for the
|
|
33
|
+
// rest of the session.
|
|
34
|
+
// ───────────────────────────────────────────────────────────────────────────────
|
|
35
|
+
|
|
36
|
+
let buffer = Buffer.alloc(0);
|
|
37
|
+
/** @type {"ndjson"|"lsp"|null} */
|
|
38
|
+
let framingMode = null;
|
|
39
|
+
|
|
40
|
+
process.stdin.on("data", (chunk) => {
|
|
41
|
+
debugLog(`stdin data: ${chunk.length} bytes`);
|
|
42
|
+
buffer = Buffer.concat([buffer, chunk]);
|
|
43
|
+
processIncomingMessages();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
process.stdin.on("end", () => {
|
|
47
|
+
debugLog("stdin ended — exiting");
|
|
48
|
+
process.exit(0);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
function processIncomingMessages() {
|
|
52
|
+
// Auto-detect framing on first meaningful byte.
|
|
53
|
+
if (!framingMode) {
|
|
54
|
+
for (let i = 0; i < buffer.length; i++) {
|
|
55
|
+
const byte = buffer[i];
|
|
56
|
+
// Skip whitespace / newlines
|
|
57
|
+
if (byte === 0x20 || byte === 0x09 || byte === 0x0a || byte === 0x0d)
|
|
58
|
+
continue;
|
|
59
|
+
// '{' → newline-delimited JSON; anything else → LSP Content-Length
|
|
60
|
+
framingMode = byte === 0x7b ? "ndjson" : "lsp";
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
if (!framingMode) return;
|
|
64
|
+
debugLog(`framing mode detected: ${framingMode}`);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (framingMode === "ndjson") {
|
|
68
|
+
processNdjsonMessages();
|
|
69
|
+
} else {
|
|
70
|
+
processLspMessages();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// ── NDJSON framing (MCP spec) ─────────────────────────────────────────────────
|
|
75
|
+
function processNdjsonMessages() {
|
|
76
|
+
while (true) {
|
|
77
|
+
const newlineIndex = buffer.indexOf(0x0a); // '\n'
|
|
78
|
+
if (newlineIndex === -1) return;
|
|
79
|
+
|
|
80
|
+
const line = buffer.subarray(0, newlineIndex).toString("utf8").trim();
|
|
81
|
+
buffer = buffer.subarray(newlineIndex + 1);
|
|
82
|
+
|
|
83
|
+
if (line.length === 0) continue;
|
|
84
|
+
|
|
85
|
+
let message;
|
|
86
|
+
try {
|
|
87
|
+
message = JSON.parse(line);
|
|
88
|
+
} catch (error) {
|
|
89
|
+
debugLog(
|
|
90
|
+
`JSON parse error: ${error instanceof Error ? error.message : String(error)}`,
|
|
91
|
+
);
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
handleMessage(message).catch((error) => {
|
|
96
|
+
const messageText =
|
|
97
|
+
error instanceof Error ? error.message : String(error);
|
|
98
|
+
debugLog(`handleMessage error: ${messageText}`);
|
|
99
|
+
console.error(messageText);
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// ── LSP Content-Length framing (legacy / tooling compat) ──────────────────────
|
|
105
|
+
function processLspMessages() {
|
|
106
|
+
while (true) {
|
|
107
|
+
let headerEnd = buffer.indexOf("\r\n\r\n");
|
|
108
|
+
let separatorLength = 4;
|
|
109
|
+
|
|
110
|
+
// Also accept \n\n (some tools omit \r)
|
|
111
|
+
if (headerEnd === -1) {
|
|
112
|
+
headerEnd = buffer.indexOf("\n\n");
|
|
113
|
+
separatorLength = 2;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (headerEnd === -1) return;
|
|
117
|
+
|
|
118
|
+
const headerText = buffer.subarray(0, headerEnd).toString("utf8");
|
|
119
|
+
const match = headerText.match(/Content-Length:\s*(\d+)/i);
|
|
120
|
+
|
|
121
|
+
if (!match) {
|
|
122
|
+
debugLog("Missing Content-Length header — discarding buffer");
|
|
123
|
+
buffer = Buffer.alloc(0);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const contentLength = Number.parseInt(match[1], 10);
|
|
128
|
+
const messageStart = headerEnd + separatorLength;
|
|
129
|
+
const messageEnd = messageStart + contentLength;
|
|
130
|
+
|
|
131
|
+
if (buffer.length < messageEnd) return;
|
|
132
|
+
|
|
133
|
+
const rawMessage = buffer
|
|
134
|
+
.subarray(messageStart, messageEnd)
|
|
135
|
+
.toString("utf8");
|
|
136
|
+
buffer = buffer.subarray(messageEnd);
|
|
137
|
+
|
|
138
|
+
let message;
|
|
139
|
+
try {
|
|
140
|
+
message = JSON.parse(rawMessage);
|
|
141
|
+
} catch (error) {
|
|
142
|
+
debugLog(
|
|
143
|
+
`JSON parse error: ${error instanceof Error ? error.message : String(error)}`,
|
|
144
|
+
);
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
handleMessage(message).catch((error) => {
|
|
149
|
+
const messageText =
|
|
150
|
+
error instanceof Error ? error.message : String(error);
|
|
151
|
+
debugLog(`handleMessage error: ${messageText}`);
|
|
152
|
+
console.error(messageText);
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// ── Write outgoing messages ───────────────────────────────────────────────────
|
|
158
|
+
function writeMessage(message) {
|
|
159
|
+
const json = JSON.stringify(message);
|
|
160
|
+
if (framingMode === "lsp") {
|
|
161
|
+
// LSP-style Content-Length framing
|
|
162
|
+
process.stdout.write(
|
|
163
|
+
`Content-Length: ${Buffer.byteLength(json, "utf8")}\r\n\r\n${json}`,
|
|
164
|
+
);
|
|
165
|
+
} else {
|
|
166
|
+
// MCP spec: newline-delimited JSON
|
|
167
|
+
process.stdout.write(json + "\n");
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function respond(id, result) {
|
|
172
|
+
writeMessage({ jsonrpc: "2.0", id, result });
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function respondError(id, code, message) {
|
|
176
|
+
writeMessage({
|
|
177
|
+
jsonrpc: "2.0",
|
|
178
|
+
id,
|
|
179
|
+
error: { code, message },
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// ── MCP dispatch ──────────────────────────────────────────────────────────────
|
|
184
|
+
async function handleMessage(message) {
|
|
185
|
+
if (!message.method) {
|
|
186
|
+
debugLog(`received message without method: ${JSON.stringify(message)}`);
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
debugLog(
|
|
191
|
+
`handling method=${message.method} id=${message.id ?? "(notification)"}`,
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
switch (message.method) {
|
|
195
|
+
case "initialize":
|
|
196
|
+
respond(message.id, {
|
|
197
|
+
protocolVersion: "2024-11-05",
|
|
198
|
+
capabilities: { tools: {} },
|
|
199
|
+
serverInfo: { name: SERVER_NAME, version: SERVER_VERSION },
|
|
200
|
+
});
|
|
201
|
+
return;
|
|
202
|
+
|
|
203
|
+
case "notifications/initialized":
|
|
204
|
+
return;
|
|
205
|
+
|
|
206
|
+
case "ping":
|
|
207
|
+
respond(message.id, {});
|
|
208
|
+
return;
|
|
209
|
+
|
|
210
|
+
case "tools/list":
|
|
211
|
+
respond(message.id, { tools: TOOLS });
|
|
212
|
+
return;
|
|
213
|
+
|
|
214
|
+
case "tools/call": {
|
|
215
|
+
const result = await callTool(
|
|
216
|
+
message.params?.name,
|
|
217
|
+
message.params?.arguments ?? {},
|
|
218
|
+
);
|
|
219
|
+
respond(message.id, result);
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
default:
|
|
224
|
+
if (message.id !== undefined) {
|
|
225
|
+
respondError(message.id, -32601, `Method not found: ${message.method}`);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// ── Tool dispatch ─────────────────────────────────────────────────────────────
|
|
231
|
+
async function callTool(name, args) {
|
|
232
|
+
try {
|
|
233
|
+
switch (name) {
|
|
234
|
+
case "export_markdown_pdf":
|
|
235
|
+
return toolResult(await exportMarkdownPdf(args));
|
|
236
|
+
|
|
237
|
+
case "doctor_markdown_pdf":
|
|
238
|
+
return toolResult(await doctorMarkdownPdf(args));
|
|
239
|
+
|
|
240
|
+
default:
|
|
241
|
+
throw new Error(`Unknown tool: ${name}`);
|
|
242
|
+
}
|
|
243
|
+
} catch (error) {
|
|
244
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
245
|
+
debugLog(`callTool(${name}) error: ${message}`);
|
|
246
|
+
return toolError(message);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function toolResult(data) {
|
|
251
|
+
return {
|
|
252
|
+
content: [{ type: "text", text: JSON.stringify(data, null, 2) }],
|
|
253
|
+
structuredContent: data,
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function toolError(message) {
|
|
258
|
+
return {
|
|
259
|
+
content: [{ type: "text", text: message }],
|
|
260
|
+
structuredContent: { error: message },
|
|
261
|
+
isError: true,
|
|
262
|
+
};
|
|
263
|
+
}
|