@threepenny/md2pdf 2.1.1 → 2.1.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/README.md CHANGED
@@ -6,7 +6,7 @@ Convert Markdown to beautifully styled PDFs. This is the rendering engine and ma
6
6
  - **On a Mac and comfortable with a terminal?** Install the command-line tool below. It adds a Finder right-click action and a keyboard shortcut.
7
7
  - **Building something?** Use the renderer and styles as a dependency, to make PDFs ([Using the renderer](#using-the-renderer)) or to show Markdown in your own pages ([Embedding in a web page](#embedding-in-a-web-page)).
8
8
 
9
- Supports GitHub-flavored Markdown: tables, task lists, footnotes, emoji shortcodes, syntax-highlighted code, raw HTML, GitHub alerts and Obsidian callouts (`> [!NOTE]`, `> [!tip] Custom title`), Obsidian wikilinks and embeds (`[[Note|alias]]` prints as plain text, `![[image.png]]` as an image, and an embedded note as a placeholder naming it), YAML front matter (left out of the PDF; its `title:` names the document when there is no `#` heading), and page breaks (`<div class="page-break"></div>`). Seven print styles are included: `github`, `classic`, `academic`, `modern`, `report`, `resume` and `worksheet`.
9
+ Supports GitHub-flavored Markdown: tables, task lists, footnotes, emoji shortcodes, syntax-highlighted code, raw HTML, GitHub alerts and Obsidian callouts (`> [!NOTE]`, `> [!tip] Custom title`), Obsidian wikilinks and embeds (`[[Note|alias]]` prints as plain text, `![[image.png]]` as an image, and an embedded note as a placeholder naming it), YAML front matter (a leading `---` block of `key: value` properties, left out of the PDF; its `title:` names the document when there is no `#` heading. A leading `---` block of ordinary Markdown prints as Markdown after a divider), and page breaks (`<div class="page-break"></div>`). Seven print styles are included: `github`, `classic`, `academic`, `modern`, `report`, `resume` and `worksheet`.
10
10
 
11
11
  # Command-line tool (macOS)
12
12
 
@@ -122,7 +122,7 @@ A style that [bundles fonts](#adding-a-style) loads them relative to its stylesh
122
122
 
123
123
  | Export | What it is |
124
124
  | ------ | ---------- |
125
- | `@threepenny/md2pdf/render` | `createRenderer({ hljs, math?, html? })`, `buildDocument(...)`, `titleOf(markdown, fallback)` (the first `#` heading, else the front matter's `title:`, else the fallback), `escapeHtml(s)`, `cssString(s)`, `headingText(html)` |
125
+ | `@threepenny/md2pdf/render` | `createRenderer({ hljs, math?, html? })`, `buildDocument(...)`, `titleOf(markdown, fallback)` (the first `#` heading's text as it renders, without Markdown syntax, else the front matter's `title:`, else the fallback), `escapeHtml(s)`, `cssString(s)`, `headingText(html)` |
126
126
  | `@threepenny/md2pdf/styles` | `pageMargins`: each style's `@page` margin at default spacing, e.g. `pageMargins.classic` is `'1in 1.1in 0.9in'` |
127
127
  | `@threepenny/md2pdf/styles/<name>.css` | Each style's on-screen look, scoped to `.markdown-body`. For `github`, load `github-markdown-css/github-markdown-light.css` first |
128
128
  | `@threepenny/md2pdf/styles/print/<name>.css` | Each style's print rules: page margins, running headers and page numbers. Load it after the on-screen file, only in a page that holds nothing but the document |
package/lib/render.js CHANGED
@@ -10,12 +10,29 @@ import wikilinks from './wikilinks.js';
10
10
 
11
11
  // YAML front matter: a `---` line at the very start of the document, closed by another `---` line.
12
12
  const FRONT_MATTER = /^---[ \t]*\r?\n(?:([\s\S]*?)\r?\n)?---[ \t]*(?:\r?\n|$)/;
13
+ // A YAML property, `key: value`.
14
+ const YAML_KEY = /^[^\s#:-][^:]*:(?:[ \t]|$)/;
15
+ // Any other line properties can have: a `- item`, an indented continuation or a # comment.
16
+ const YAML_OTHER = /^(?:-(?:[ \t]|$)|[ \t]+\S|#)/;
17
+
18
+ // The front matter block at the start of `markdown` and the YAML inside it, or null. A `---…---` block
19
+ // that isn't YAML properties is Markdown after a divider, as AI answers write it. A # line could be a
20
+ // comment or a heading, and a - line a list, so the first other line must be a property.
21
+ function frontMatterOf(markdown) {
22
+ const [block, yaml = ''] = markdown.match(FRONT_MATTER) ?? [];
23
+ if (!block) return null;
24
+ const lines = yaml.split(/\r?\n/).filter((line) => line.trim());
25
+ const first = lines.find((line) => !line.trimStart().startsWith('#'));
26
+ const yamlShaped = lines.every((line) => YAML_KEY.test(line) || YAML_OTHER.test(line));
27
+ if (lines.length && !(yamlShaped && first && YAML_KEY.test(first))) return null;
28
+ return { block, yaml };
29
+ }
13
30
 
14
31
  // Leaves front matter (Obsidian, Jekyll, Hugo) out of the output instead of rendering it as a rule and a heading.
15
32
  function frontMatter(md) {
16
33
  md.block.ruler.before('table', 'front_matter', (state, startLine, endLine, silent) => {
17
34
  if (startLine !== 0) return false;
18
- const block = state.src.match(FRONT_MATTER)?.[0];
35
+ const block = frontMatterOf(state.src)?.block;
19
36
  if (!block) return false;
20
37
  if (!silent) state.line = block.replace(/\n$/, '').split('\n').length;
21
38
  return true;
@@ -56,20 +73,38 @@ export function createRenderer({ hljs, math, html = true }) {
56
73
  if (math) {
57
74
  md.use(math.texmath, {
58
75
  engine: math.katex,
59
- // ChatGPT writes \( \) and \[ \]; many others use $ and $$.
76
+ // LaTeX writes \( \) and \[ \]; AI assistants and most Markdown editors use $ and $$.
60
77
  delimiters: ['brackets', 'dollars', 'beg_end'],
61
78
  // Require a space before an opening $ so prices like "$5 and $10" stay text.
62
79
  outerSpace: true,
63
80
  katexOptions: { throwOnError: false, output: 'html' },
64
81
  });
82
+ // texmath's outerSpace takes only a space beside a $, so a formula at the start or end of a
83
+ // hard-wrapped line stays text. These run first and also take a line break.
84
+ for (const rule of math.texmath.rules.dollars.inline) {
85
+ md.inline.ruler.before('math_inline_double', `${rule.name}_wrapped`,
86
+ math.texmath.inline({ ...rule, rex: new RegExp(rule.rex), pre: dollarPre, post: dollarPost }));
87
+ }
65
88
  }
66
89
  return md;
67
90
  }
68
91
 
69
- // The document title: the first level-one heading, else the front matter's `title:`, else the fallback.
92
+ // Whether the $ at `at` opens or closes a formula: whitespace or the paragraph's edge is outside it,
93
+ // or after a closing $, punctuation that ends a clause.
94
+ const dollarPre = (src, _outerSpace, at) => at === 0 || /\s/.test(src[at - 1]);
95
+ const dollarPost = (src, _outerSpace, at) => at + 1 >= src.length || /[\s.,;]/.test(src[at + 1]);
96
+
97
+ let titleParser;
98
+
99
+ // The document title: the text of the first level-one heading as it renders, else the front matter's
100
+ // `title:`, else the fallback.
70
101
  export function titleOf(markdown, fallback) {
71
- const [block, yaml = ''] = markdown.match(FRONT_MATTER) ?? [''];
72
- const heading = markdown.slice(block.length).match(/^#\s+(.+)$/m)?.[1].trim();
102
+ titleParser ??= createRenderer({});
103
+ const env = {};
104
+ const tokens = titleParser.parse(markdown, env);
105
+ const at = tokens.findIndex((t) => t.type === 'heading_open' && t.tag === 'h1');
106
+ const heading = at < 0 ? '' : headingText(titleParser.renderer.render(tokens.slice(at, at + 3), titleParser.options, env));
107
+ const yaml = frontMatterOf(markdown)?.yaml ?? '';
73
108
  const title = yaml.match(/^title:[ \t]*(.+?)[ \t]*$/m)?.[1].replace(/^(["'])(.*)\1$/, '$2').trim();
74
109
  return heading || title || fallback;
75
110
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@threepenny/md2pdf",
3
- "version": "2.1.1",
3
+ "version": "2.1.2",
4
4
  "description": "Markdown to beautifully styled PDF: the renderer and macOS CLI behind md2pdf.me",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -40,5 +40,9 @@
40
40
  "markdown-it-emoji": "^3.1.0",
41
41
  "markdown-it-footnote": "^4.0.0",
42
42
  "markdown-it-task-lists": "^2.1.1"
43
+ },
44
+ "devDependencies": {
45
+ "katex": "^0.19.1",
46
+ "markdown-it-texmath": "^1.0.0"
43
47
  }
44
48
  }
@@ -31,6 +31,8 @@
31
31
  font-size: var(--md2pdf-font-size);
32
32
  line-height: calc(1 + 0.42 * var(--md2pdf-leading));
33
33
  color: var(--md2pdf-text);
34
+ /* A long web address wraps at the margin instead of making Chrome shrink the page to fit it. */
35
+ overflow-wrap: break-word;
34
36
  font-kerning: normal;
35
37
  counter-reset: h2 figure;
36
38
  -webkit-print-color-adjust: exact;
@@ -146,10 +148,19 @@
146
148
  .markdown-body .task-list-item {
147
149
  list-style-type: none;
148
150
  }
151
+ /* Boxes drawn here at the browser's size: its own are greyed out, as they're disabled. */
149
152
  .markdown-body .task-list-item input {
153
+ appearance: none;
154
+ width: 13px;
155
+ height: 13px;
150
156
  margin: 0 0.4em 0.2em -1.4em;
151
157
  vertical-align: middle;
152
- accent-color: var(--md2pdf-text);
158
+ border: 1px solid var(--md2pdf-text);
159
+ border-radius: 2px;
160
+ background: var(--md2pdf-background);
161
+ }
162
+ .markdown-body .task-list-item input:checked {
163
+ background: var(--md2pdf-text) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3.5 8.5l3 3 6-7' fill='none' stroke='%23fff' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / 100% no-repeat;
153
164
  }
154
165
 
155
166
  /* Quotes: set in from both sides, slightly smaller. */
@@ -368,6 +379,8 @@
368
379
  width: 25%;
369
380
  border-top: 0.6px solid var(--md2pdf-rule);
370
381
  margin-bottom: calc(0.7em * var(--md2pdf-space));
382
+ /* Keep the line on the page with the first footnote. */
383
+ break-after: avoid;
371
384
  }
372
385
  .markdown-body .footnotes ol {
373
386
  padding-left: 1.4em;
@@ -33,6 +33,8 @@
33
33
  font-size: var(--md2pdf-font-size);
34
34
  line-height: calc(1 + 0.55 * var(--md2pdf-leading));
35
35
  color: var(--md2pdf-text);
36
+ /* A long web address wraps at the margin instead of making Chrome shrink the page to fit it. */
37
+ overflow-wrap: break-word;
36
38
  font-variant-numeric: oldstyle-nums proportional-nums;
37
39
  font-kerning: normal;
38
40
  -webkit-print-color-adjust: exact;
@@ -71,6 +73,8 @@
71
73
  text-align: center;
72
74
  letter-spacing: 0.01em;
73
75
  margin: 0 0 calc(1.4em * var(--md2pdf-space));
76
+ /* The rule is a block inside the heading; without this a page can break between them. */
77
+ break-inside: avoid;
74
78
  }
75
79
  .markdown-body h1::after {
76
80
  content: "";
@@ -136,10 +140,19 @@
136
140
  .markdown-body .task-list-item {
137
141
  list-style-type: none;
138
142
  }
143
+ /* Boxes drawn here at the browser's size: its own are greyed out, as they're disabled. */
139
144
  .markdown-body .task-list-item input {
145
+ appearance: none;
146
+ width: 13px;
147
+ height: 13px;
140
148
  margin: 0 0.4em 0.2em -1.4em;
141
149
  vertical-align: middle;
142
- accent-color: var(--md2pdf-accent);
150
+ border: 1px solid var(--md2pdf-accent);
151
+ border-radius: 2px;
152
+ background: var(--md2pdf-background);
153
+ }
154
+ .markdown-body .task-list-item input:checked {
155
+ background: var(--md2pdf-accent) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3.5 8.5l3 3 6-7' fill='none' stroke='%23fff' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / 100% no-repeat;
143
156
  }
144
157
 
145
158
  /* Quotes */
@@ -255,7 +268,7 @@
255
268
  /* Muted syntax colors to suit the page. */
256
269
  .markdown-body .hljs-comment,
257
270
  .markdown-body .hljs-quote {
258
- color: #8a8279;
271
+ color: #746d66;
259
272
  font-style: italic;
260
273
  }
261
274
  .markdown-body .hljs-keyword,
@@ -358,6 +371,8 @@
358
371
  width: 30%;
359
372
  border-top: 0.75px solid var(--md2pdf-rule);
360
373
  margin-bottom: calc(0.8em * var(--md2pdf-space));
374
+ /* Keep the line on the page with the first footnote. */
375
+ break-after: avoid;
361
376
  }
362
377
  .markdown-body .footnotes ol {
363
378
  padding-left: 1.2em;
package/styles/github.css CHANGED
@@ -270,9 +270,19 @@
270
270
  .markdown-body .task-list-item {
271
271
  list-style-type: none;
272
272
  }
273
+ /* Boxes drawn here at the browser's size: its own are greyed out, as they're disabled. */
273
274
  .markdown-body .task-list-item input {
275
+ appearance: none;
276
+ width: 13px;
277
+ height: 13px;
274
278
  margin: 0 0.35em 0.25em -1.4em;
275
279
  vertical-align: middle;
280
+ border: 1px solid var(--md2pdf-accent);
281
+ border-radius: 2px;
282
+ background: var(--md2pdf-background);
283
+ }
284
+ .markdown-body .task-list-item input:checked {
285
+ background: var(--md2pdf-accent) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3.5 8.5l3 3 6-7' fill='none' stroke='%23fff' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / 100% no-repeat;
276
286
  }
277
287
 
278
288
  /* github-markdown-css colors GitHub's five alert types. Its flex title would drop the spaces around
package/styles/modern.css CHANGED
@@ -33,6 +33,8 @@
33
33
  font-size: var(--md2pdf-font-size);
34
34
  line-height: calc(1 + 0.65 * var(--md2pdf-leading));
35
35
  color: var(--md2pdf-text);
36
+ /* A long web address wraps at the margin instead of making Chrome shrink the page to fit it. */
37
+ overflow-wrap: break-word;
36
38
  -webkit-print-color-adjust: exact;
37
39
  print-color-adjust: exact;
38
40
  }
@@ -72,6 +74,8 @@
72
74
  .markdown-body h2 {
73
75
  font-size: calc(16 / 10.5 * var(--md2pdf-font-size));
74
76
  margin: calc(2em * var(--md2pdf-space)) 0 calc(0.7em * var(--md2pdf-space));
77
+ /* The bar is a block inside the heading; without this a page can break between them. */
78
+ break-inside: avoid;
75
79
  }
76
80
  .markdown-body h2::before {
77
81
  content: "";
@@ -127,10 +131,19 @@
127
131
  .markdown-body .task-list-item {
128
132
  list-style-type: none;
129
133
  }
134
+ /* Boxes drawn here at the browser's size: its own are greyed out, as they're disabled. */
130
135
  .markdown-body .task-list-item input {
136
+ appearance: none;
137
+ width: 13px;
138
+ height: 13px;
131
139
  margin: 0 0.5em 0.2em -1.5em;
132
140
  vertical-align: middle;
133
- accent-color: var(--md2pdf-accent);
141
+ border: 1px solid var(--md2pdf-accent);
142
+ border-radius: 2px;
143
+ background: var(--md2pdf-background);
144
+ }
145
+ .markdown-body .task-list-item input:checked {
146
+ background: var(--md2pdf-accent) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3.5 8.5l3 3 6-7' fill='none' stroke='%23fff' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / 100% no-repeat;
134
147
  }
135
148
 
136
149
  /* Quotes */
@@ -239,7 +252,7 @@
239
252
 
240
253
  .markdown-body .hljs-comment,
241
254
  .markdown-body .hljs-quote {
242
- color: #8790a3;
255
+ color: #69707f;
243
256
  font-style: italic;
244
257
  }
245
258
  .markdown-body .hljs-keyword,
@@ -24,3 +24,9 @@ body {
24
24
  margin: 0;
25
25
  padding: 0;
26
26
  }
27
+
28
+ /* github-markdown-css lets a wide table scroll sideways. Paper can't scroll, so show every column
29
+ instead of cutting off the ones past the margin. */
30
+ .markdown-body table {
31
+ overflow: visible;
32
+ }
package/styles/report.css CHANGED
@@ -34,6 +34,8 @@
34
34
  font-size: var(--md2pdf-font-size);
35
35
  line-height: calc(1 + 0.5 * var(--md2pdf-leading));
36
36
  color: var(--md2pdf-text);
37
+ /* A long web address wraps at the margin instead of making Chrome shrink the page to fit it. */
38
+ overflow-wrap: break-word;
37
39
  -webkit-print-color-adjust: exact;
38
40
  print-color-adjust: exact;
39
41
  }
@@ -123,10 +125,19 @@
123
125
  .markdown-body .task-list-item {
124
126
  list-style-type: none;
125
127
  }
128
+ /* Boxes drawn here at the browser's size: its own are greyed out, as they're disabled. */
126
129
  .markdown-body .task-list-item input {
130
+ appearance: none;
131
+ width: 13px;
132
+ height: 13px;
127
133
  margin: 0 0.4em 0.2em -1.4em;
128
134
  vertical-align: middle;
129
- accent-color: var(--md2pdf-accent);
135
+ border: 1px solid var(--md2pdf-accent);
136
+ border-radius: 2px;
137
+ background: var(--md2pdf-background);
138
+ }
139
+ .markdown-body .task-list-item input:checked {
140
+ background: var(--md2pdf-accent) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3.5 8.5l3 3 6-7' fill='none' stroke='%23fff' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / 100% no-repeat;
130
141
  }
131
142
 
132
143
  /* Quotes: a shaded callout. */
@@ -237,7 +248,7 @@
237
248
 
238
249
  .markdown-body .hljs-comment,
239
250
  .markdown-body .hljs-quote {
240
- color: #6a7686;
251
+ color: #667181;
241
252
  font-style: italic;
242
253
  }
243
254
  .markdown-body .hljs-keyword,
package/styles/resume.css CHANGED
@@ -33,6 +33,8 @@
33
33
  font-size: var(--md2pdf-font-size);
34
34
  line-height: calc(1 + 0.4 * var(--md2pdf-leading));
35
35
  color: var(--md2pdf-text);
36
+ /* A long web address wraps at the margin instead of making Chrome shrink the page to fit it. */
37
+ overflow-wrap: break-word;
36
38
  -webkit-print-color-adjust: exact;
37
39
  print-color-adjust: exact;
38
40
  }
@@ -137,10 +139,19 @@
137
139
  .markdown-body .task-list-item {
138
140
  list-style-type: none;
139
141
  }
142
+ /* Boxes drawn here at the browser's size: its own are greyed out, as they're disabled. */
140
143
  .markdown-body .task-list-item input {
144
+ appearance: none;
145
+ width: 13px;
146
+ height: 13px;
141
147
  margin: 0 0.4em 0.15em -1.2em;
142
148
  vertical-align: middle;
143
- accent-color: var(--md2pdf-accent);
149
+ border: 1px solid var(--md2pdf-accent);
150
+ border-radius: 2px;
151
+ background: var(--md2pdf-background);
152
+ }
153
+ .markdown-body .task-list-item input:checked {
154
+ background: var(--md2pdf-accent) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3.5 8.5l3 3 6-7' fill='none' stroke='%23fff' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / 100% no-repeat;
144
155
  }
145
156
 
146
157
  /* Quotes: e.g. a short summary or a reference. */
@@ -242,7 +253,7 @@
242
253
 
243
254
  .markdown-body .hljs-comment,
244
255
  .markdown-body .hljs-quote {
245
- color: #6e7781;
256
+ color: #69717b;
246
257
  font-style: italic;
247
258
  }
248
259
  .markdown-body .hljs-keyword,
@@ -71,6 +71,8 @@
71
71
  font-size: var(--md2pdf-font-size);
72
72
  line-height: calc(1 + 0.6 * var(--md2pdf-leading));
73
73
  color: var(--md2pdf-text);
74
+ /* A long web address wraps at the margin instead of making Chrome shrink the page to fit it. */
75
+ overflow-wrap: break-word;
74
76
  -webkit-print-color-adjust: exact;
75
77
  print-color-adjust: exact;
76
78
  }