markdown-pdf-export 0.0.0-stage → 2.0.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/LICENSE +21 -0
- package/README.md +92 -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 +106 -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
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Settings file discovery and reading.
|
|
3
|
+
*
|
|
4
|
+
* Layers (lowest to highest priority), resolved by export.mjs:
|
|
5
|
+
* 1. built-in defaults
|
|
6
|
+
* 2. user file ($XDG_CONFIG_HOME/markdown-pdf-export/settings.json,
|
|
7
|
+
* %APPDATA%\markdown-pdf-export\settings.json on Windows)
|
|
8
|
+
* — or MARKDOWN_PDF_SETTINGS / --settings, which replace it
|
|
9
|
+
* 3. project file (nearest .markdown-pdf.json walking up from the Markdown file)
|
|
10
|
+
*
|
|
11
|
+
* Relative paths written inside a settings file resolve against that file's
|
|
12
|
+
* directory, so a global logo needs no absolute path.
|
|
13
|
+
*/
|
|
14
|
+
import fs from "node:fs";
|
|
15
|
+
import os from "node:os";
|
|
16
|
+
import path from "node:path";
|
|
17
|
+
import { applyPresets } from "./looks.mjs";
|
|
18
|
+
|
|
19
|
+
export const APP_DIR_NAME = "markdown-pdf-export";
|
|
20
|
+
export const USER_SETTINGS_FILENAME = "settings.json";
|
|
21
|
+
export const PROJECT_SETTINGS_FILENAME = ".markdown-pdf.json";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Default location of the user settings file.
|
|
25
|
+
* @param {NodeJS.ProcessEnv} [env]
|
|
26
|
+
* @param {string} [platform]
|
|
27
|
+
* @param {string} [home]
|
|
28
|
+
* @returns {string}
|
|
29
|
+
*/
|
|
30
|
+
export function userSettingsPath(
|
|
31
|
+
env = process.env,
|
|
32
|
+
platform = process.platform,
|
|
33
|
+
home = os.homedir(),
|
|
34
|
+
) {
|
|
35
|
+
if (platform === "win32") {
|
|
36
|
+
const appData = env.APPDATA || path.join(home, "AppData", "Roaming");
|
|
37
|
+
return path.join(appData, APP_DIR_NAME, USER_SETTINGS_FILENAME);
|
|
38
|
+
}
|
|
39
|
+
const base = env.XDG_CONFIG_HOME
|
|
40
|
+
? env.XDG_CONFIG_HOME
|
|
41
|
+
: path.join(home, ".config");
|
|
42
|
+
return path.join(base, APP_DIR_NAME, USER_SETTINGS_FILENAME);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Find the nearest project settings file, walking up from `startDir`.
|
|
47
|
+
* @param {string} startDir
|
|
48
|
+
* @returns {string|null}
|
|
49
|
+
*/
|
|
50
|
+
export function findProjectSettingsFile(startDir) {
|
|
51
|
+
let dir = path.resolve(startDir);
|
|
52
|
+
for (;;) {
|
|
53
|
+
const candidate = path.join(dir, PROJECT_SETTINGS_FILENAME);
|
|
54
|
+
try {
|
|
55
|
+
if (fs.statSync(candidate).isFile()) return candidate;
|
|
56
|
+
} catch {
|
|
57
|
+
// not here — keep walking
|
|
58
|
+
}
|
|
59
|
+
const parent = path.dirname(dir);
|
|
60
|
+
if (parent === dir) return null;
|
|
61
|
+
dir = parent;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const isPlainObject = (v) =>
|
|
66
|
+
v !== null && typeof v === "object" && !Array.isArray(v);
|
|
67
|
+
|
|
68
|
+
/** Paths we must not touch: absolute, ~, @/ asset alias, URLs, data URIs. */
|
|
69
|
+
function isRewritable(value) {
|
|
70
|
+
if (typeof value !== "string" || value.trim() === "") return false;
|
|
71
|
+
if (/^(~|@[\\/]|[a-z][a-z0-9+.-]*:)/i.test(value)) return false; // ~, @/, scheme:, C:
|
|
72
|
+
return !path.isAbsolute(value);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const IMAGE_SHORTHANDS = new Set(["left_image", "center_image", "right_image"]);
|
|
76
|
+
|
|
77
|
+
function rewriteImages(node, dir) {
|
|
78
|
+
if (Array.isArray(node)) {
|
|
79
|
+
for (const item of node) rewriteImages(item, dir);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (!isPlainObject(node)) return;
|
|
83
|
+
if (node.type === "image" && isRewritable(node.src)) {
|
|
84
|
+
node.src = path.resolve(dir, node.src);
|
|
85
|
+
}
|
|
86
|
+
for (const [key, value] of Object.entries(node)) {
|
|
87
|
+
if (IMAGE_SHORTHANDS.has(key) && isRewritable(value)) {
|
|
88
|
+
node[key] = path.resolve(dir, value);
|
|
89
|
+
} else if (typeof value === "object") {
|
|
90
|
+
rewriteImages(value, dir);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Resolve relative asset paths in parsed settings against `dir`.
|
|
97
|
+
* Returns a new object; the input is not mutated.
|
|
98
|
+
* @param {object} data
|
|
99
|
+
* @param {string} dir
|
|
100
|
+
*/
|
|
101
|
+
export function resolveSettingsPaths(data, dir) {
|
|
102
|
+
if (!isPlainObject(data)) return data;
|
|
103
|
+
const out = structuredClone(data);
|
|
104
|
+
for (const key of ["stylesheet_path", "assets_directory"]) {
|
|
105
|
+
if (isRewritable(out[key])) out[key] = path.resolve(dir, out[key]);
|
|
106
|
+
}
|
|
107
|
+
rewriteImages(out.header, dir);
|
|
108
|
+
rewriteImages(out.footer, dir);
|
|
109
|
+
return out;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Parsed settings → what the engine consumes: presets expanded, relative
|
|
114
|
+
* paths resolved against `dir`.
|
|
115
|
+
* @param {object} parsed
|
|
116
|
+
* @param {string} dir
|
|
117
|
+
* @returns {{data: object, warnings: string[]}}
|
|
118
|
+
*/
|
|
119
|
+
export function prepareSettings(parsed, dir) {
|
|
120
|
+
const { data, warnings } = applyPresets(parsed);
|
|
121
|
+
return { data: resolveSettingsPaths(data, dir), warnings };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Read and parse a settings file.
|
|
126
|
+
* @param {string} file
|
|
127
|
+
* @returns {{path: string, data: object|null, error: string|null}}
|
|
128
|
+
*/
|
|
129
|
+
export function readSettingsFile(file) {
|
|
130
|
+
const abs = path.resolve(file);
|
|
131
|
+
let text;
|
|
132
|
+
try {
|
|
133
|
+
text = fs.readFileSync(abs, "utf8");
|
|
134
|
+
} catch (error) {
|
|
135
|
+
return { path: abs, data: null, error: `cannot read: ${error.message}` };
|
|
136
|
+
}
|
|
137
|
+
try {
|
|
138
|
+
const parsed = JSON.parse(text.replace(/^\uFEFF/, "")); // PowerShell 5 writes a BOM
|
|
139
|
+
if (!isPlainObject(parsed)) {
|
|
140
|
+
return { path: abs, data: null, error: "top level must be a JSON object" };
|
|
141
|
+
}
|
|
142
|
+
const { data, warnings } = prepareSettings(parsed, path.dirname(abs));
|
|
143
|
+
for (const w of warnings) console.error(`[markdown-pdf] ⚠️ ${abs}: ${w}`);
|
|
144
|
+
return { path: abs, data, error: null };
|
|
145
|
+
} catch (error) {
|
|
146
|
+
return { path: abs, data: null, error: `invalid JSON: ${error.message}` };
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Deep-merge plain objects (arrays and scalars in `over` replace `base`).
|
|
152
|
+
* @param {object} base
|
|
153
|
+
* @param {object} over
|
|
154
|
+
*/
|
|
155
|
+
export function deepMerge(base, over) {
|
|
156
|
+
if (!isPlainObject(base)) return structuredClone(over);
|
|
157
|
+
if (!isPlainObject(over)) return structuredClone(base);
|
|
158
|
+
const out = structuredClone(base);
|
|
159
|
+
for (const [key, value] of Object.entries(over)) {
|
|
160
|
+
if (value === undefined) continue;
|
|
161
|
+
out[key] =
|
|
162
|
+
isPlainObject(value) && isPlainObject(out[key])
|
|
163
|
+
? deepMerge(out[key], value)
|
|
164
|
+
: structuredClone(value);
|
|
165
|
+
}
|
|
166
|
+
return out;
|
|
167
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview JSON Schema for settings files (user settings.json and project
|
|
3
|
+
* .markdown-pdf.json). Built from the MCP/CLI export schema so the three
|
|
4
|
+
* surfaces cannot drift; header/footer definitions mirror lib/types.mjs.
|
|
5
|
+
*
|
|
6
|
+
* Generated file: schema/settings.schema.json (scripts/build-settings-schema.mjs).
|
|
7
|
+
*/
|
|
8
|
+
import { EXPORT_INPUT_SCHEMA } from "../tool-schemas.mjs";
|
|
9
|
+
import { LOOKS, FONTS, TEMPLATES } from "./looks.mjs";
|
|
10
|
+
import { BUILTIN_PLACEHOLDERS, ELEMENT_TYPES, SHORTHAND_MAPPINGS } from "./types.mjs";
|
|
11
|
+
|
|
12
|
+
export const SETTINGS_SCHEMA_URL =
|
|
13
|
+
"https://matinfo.github.io/markdown-pdf-export/schema/settings.schema.json";
|
|
14
|
+
|
|
15
|
+
/** Export options that make no sense in a settings file. */
|
|
16
|
+
const CALL_ONLY = new Set(["input_path", "output_path"]);
|
|
17
|
+
|
|
18
|
+
const css = (description, examples) => ({ type: "string", description, examples });
|
|
19
|
+
|
|
20
|
+
const COMMON_ELEMENT = {
|
|
21
|
+
font_size: css("CSS font-size.", ["9px", "10pt"]),
|
|
22
|
+
color: css("CSS color.", ["#666", "gray"]),
|
|
23
|
+
style: css("Extra inline CSS, e.g. \"letter-spacing: 1px\"."),
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const ELEMENT_PROPS = {
|
|
27
|
+
text: {
|
|
28
|
+
content: {
|
|
29
|
+
type: "string",
|
|
30
|
+
description: `Text; supports placeholders ${BUILTIN_PLACEHOLDERS.map((p) => `{${p}}`).join(", ")}.`,
|
|
31
|
+
},
|
|
32
|
+
font_weight: { type: ["string", "number"], description: "CSS font-weight." },
|
|
33
|
+
font_style: { type: "string", enum: ["normal", "italic", "oblique"] },
|
|
34
|
+
...COMMON_ELEMENT,
|
|
35
|
+
},
|
|
36
|
+
image: {
|
|
37
|
+
src: {
|
|
38
|
+
type: "string",
|
|
39
|
+
description:
|
|
40
|
+
"SVG, PNG or JPG. Relative to the settings file; \"@/name.svg\" looks in assets_directory.",
|
|
41
|
+
},
|
|
42
|
+
height: css("CSS height.", ["12mm"]),
|
|
43
|
+
width: css("CSS width (omit to keep the aspect ratio)."),
|
|
44
|
+
alt: { type: "string" },
|
|
45
|
+
style: COMMON_ELEMENT.style,
|
|
46
|
+
},
|
|
47
|
+
page_number: {
|
|
48
|
+
format: { type: "string", description: "e.g. \"Page {page}\"." },
|
|
49
|
+
...COMMON_ELEMENT,
|
|
50
|
+
},
|
|
51
|
+
total_pages: {
|
|
52
|
+
format: { type: "string", description: "e.g. \"{pages} pages\"." },
|
|
53
|
+
...COMMON_ELEMENT,
|
|
54
|
+
},
|
|
55
|
+
date: {
|
|
56
|
+
format: { type: "string", description: "date-fns format, e.g. \"yyyy-MM-dd\"." },
|
|
57
|
+
...COMMON_ELEMENT,
|
|
58
|
+
},
|
|
59
|
+
title: {
|
|
60
|
+
font_weight: { type: ["string", "number"] },
|
|
61
|
+
fallback: { type: "string", description: "Used when the document has no title." },
|
|
62
|
+
...COMMON_ELEMENT,
|
|
63
|
+
},
|
|
64
|
+
spacer: {
|
|
65
|
+
width: css("Fixed width; omitted = flexible."),
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const REQUIRED = { text: ["content"], image: ["src"] };
|
|
70
|
+
|
|
71
|
+
function elementDefinitions() {
|
|
72
|
+
const defs = {};
|
|
73
|
+
for (const type of ELEMENT_TYPES) {
|
|
74
|
+
defs[`element_${type}`] = {
|
|
75
|
+
type: "object",
|
|
76
|
+
additionalProperties: false,
|
|
77
|
+
required: ["type", ...(REQUIRED[type] ?? [])],
|
|
78
|
+
properties: { type: { const: type }, ...ELEMENT_PROPS[type] },
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
return defs;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function headerFooterDefinition() {
|
|
85
|
+
const shorthands = {};
|
|
86
|
+
for (const [key, { zone, property }] of Object.entries(SHORTHAND_MAPPINGS)) {
|
|
87
|
+
shorthands[key] =
|
|
88
|
+
property === "height"
|
|
89
|
+
? css(`Shorthand: height of the ${zone} image.`, ["12mm"])
|
|
90
|
+
: key.endsWith("_image")
|
|
91
|
+
? { type: "string", description: `Shorthand: image in the ${zone} zone (relative to the settings file, or "@/name.svg").` }
|
|
92
|
+
: { type: "string", description: `Shorthand: text in the ${zone} zone; placeholders allowed, e.g. "Page {page} of {pages}".` };
|
|
93
|
+
}
|
|
94
|
+
const zone = {
|
|
95
|
+
description: "Element, array of elements, or null to clear the zone.",
|
|
96
|
+
anyOf: [
|
|
97
|
+
{ $ref: "#/definitions/element" },
|
|
98
|
+
{ type: "array", items: { $ref: "#/definitions/element" } },
|
|
99
|
+
{ type: "null" },
|
|
100
|
+
],
|
|
101
|
+
};
|
|
102
|
+
return {
|
|
103
|
+
type: ["object", "null"],
|
|
104
|
+
additionalProperties: false,
|
|
105
|
+
properties: {
|
|
106
|
+
height: css("Height of the area.", ["15mm"]),
|
|
107
|
+
padding: css("CSS padding.", ["0 15mm"]),
|
|
108
|
+
font_family: { type: "string" },
|
|
109
|
+
font_size: css("Default font-size.", ["9px"]),
|
|
110
|
+
font_style: { type: "string", enum: ["normal", "italic", "oblique"] },
|
|
111
|
+
font_weight: { type: ["string", "number"] },
|
|
112
|
+
color: css("Default text color.", ["#666"]),
|
|
113
|
+
background: { type: "string" },
|
|
114
|
+
border_bottom: css("Header only.", ["1px solid #ddd"]),
|
|
115
|
+
border_top: css("Footer only.", ["1px solid #eee"]),
|
|
116
|
+
style: COMMON_ELEMENT.style,
|
|
117
|
+
left: zone,
|
|
118
|
+
center: zone,
|
|
119
|
+
right: zone,
|
|
120
|
+
...shorthands,
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** @returns {object} draft-07 JSON Schema for a settings file */
|
|
126
|
+
export function buildSettingsSchema() {
|
|
127
|
+
const properties = {
|
|
128
|
+
$schema: { type: "string", description: "JSON Schema URL (for editor autocomplete)." },
|
|
129
|
+
};
|
|
130
|
+
for (const [name, def] of Object.entries(EXPORT_INPUT_SCHEMA.properties)) {
|
|
131
|
+
if (CALL_ONLY.has(name)) continue;
|
|
132
|
+
if (name === "header" || name === "footer") {
|
|
133
|
+
properties[name] = {
|
|
134
|
+
$ref: "#/definitions/header_footer",
|
|
135
|
+
description: name === "header" ? "Page header." : "Page footer.",
|
|
136
|
+
};
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
properties[name] = structuredClone(def);
|
|
140
|
+
if (name === "margin") properties[name].description = "Page margins.";
|
|
141
|
+
if (name === "stylesheet_path") {
|
|
142
|
+
properties[name].description =
|
|
143
|
+
"CSS file applied after the built-in stylesheet (relative to this settings file).";
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
properties.look = {
|
|
147
|
+
type: "string",
|
|
148
|
+
enum: Object.keys(LOOKS),
|
|
149
|
+
description: `Page style preset: ${Object.entries(LOOKS).map(([k, v]) => `${k} (${v.description})`).join(" ")} Explicit keys in this file override it.`,
|
|
150
|
+
};
|
|
151
|
+
properties.font = {
|
|
152
|
+
type: "string",
|
|
153
|
+
enum: Object.keys(FONTS),
|
|
154
|
+
description: "Font preset for the text. Use font_family for a custom stack.",
|
|
155
|
+
};
|
|
156
|
+
properties.heading_font = {
|
|
157
|
+
type: "string",
|
|
158
|
+
enum: Object.keys(FONTS),
|
|
159
|
+
description: "Font preset for titles (h1-h6). Use heading_font_family for a custom stack.",
|
|
160
|
+
};
|
|
161
|
+
properties.template = {
|
|
162
|
+
type: "string",
|
|
163
|
+
enum: TEMPLATES,
|
|
164
|
+
description: "Header/footer preset. Explicit header/footer keys in this file override it.",
|
|
165
|
+
};
|
|
166
|
+
properties.output_directory = {
|
|
167
|
+
type: "string",
|
|
168
|
+
description: "Folder for the PDFs, relative to each Markdown file. Default: next to it.",
|
|
169
|
+
};
|
|
170
|
+
properties.assets_directory = {
|
|
171
|
+
type: "string",
|
|
172
|
+
description: "Folder for \"@/name.svg\" image references (relative to this settings file).",
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
return {
|
|
176
|
+
$schema: "http://json-schema.org/draft-07/schema#",
|
|
177
|
+
$id: SETTINGS_SCHEMA_URL,
|
|
178
|
+
title: "markdown-pdf-export settings",
|
|
179
|
+
description:
|
|
180
|
+
"User settings (~/.config/markdown-pdf-export/settings.json) or project settings (.markdown-pdf.json).",
|
|
181
|
+
type: "object",
|
|
182
|
+
additionalProperties: false,
|
|
183
|
+
properties,
|
|
184
|
+
definitions: {
|
|
185
|
+
header_footer: headerFooterDefinition(),
|
|
186
|
+
element: {
|
|
187
|
+
oneOf: ELEMENT_TYPES.map((type) => ({ $ref: `#/definitions/element_${type}` })),
|
|
188
|
+
},
|
|
189
|
+
...elementDefinitions(),
|
|
190
|
+
},
|
|
191
|
+
};
|
|
192
|
+
}
|