pantsdown 2.2.3 → 2.2.5
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/.oxfmtrc.json +17 -0
- package/CLAUDE.md +1 -0
- package/README.md +44 -26
- package/bun.lock +1504 -0
- package/eslint.config.ts +46 -0
- package/package.json +47 -43
- package/src/css/styles.css +581 -581
- package/src/lexer.ts +419 -419
- package/src/pantsdown.ts +39 -38
- package/src/parser.ts +223 -239
- package/src/renderer.ts +237 -245
- package/src/rules/block.ts +99 -101
- package/src/rules/inline.ts +92 -92
- package/src/rules/utils.ts +14 -14
- package/src/tokenizer.ts +911 -919
- package/src/types.ts +262 -244
- package/src/utils.ts +278 -241
- package/tsconfig.json +2 -1
- package/.prettierignore +0 -1
- package/src/css/styles.css.d.ts +0 -4
package/src/renderer.ts
CHANGED
|
@@ -1,258 +1,250 @@
|
|
|
1
1
|
import GithubSlugger from "github-slugger";
|
|
2
2
|
import hljs from "highlight.js";
|
|
3
|
-
import
|
|
3
|
+
import { renderToString } from "katex";
|
|
4
4
|
import { type Pantsdown } from "./pantsdown.ts";
|
|
5
5
|
import { inline } from "./rules/inline.ts";
|
|
6
6
|
import { type HTMLAttrs, type SourceMap, type Tokens } from "./types.ts";
|
|
7
7
|
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
addGithubImageStyles,
|
|
9
|
+
cleanUrl,
|
|
10
|
+
escape,
|
|
11
|
+
fixHtmlLocalImageHref,
|
|
12
|
+
fixLocalImageHref,
|
|
13
|
+
getHtmlElementText,
|
|
14
|
+
injectHtmlAttributes,
|
|
14
15
|
} from "./utils.ts";
|
|
15
16
|
|
|
16
17
|
/**
|
|
17
18
|
* An object containing functions to render tokens to HTML.
|
|
18
19
|
*/
|
|
19
20
|
export class Renderer {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
throwOnError: false,
|
|
250
|
-
output: "html",
|
|
251
|
-
trust: false,
|
|
252
|
-
});
|
|
253
|
-
return `<span class="katex-inline">${rendered}</span>`;
|
|
254
|
-
} catch {
|
|
255
|
-
return `<span class="katex-inline katex-error"><code>${escape(latex)}</code></span>`;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
21
|
+
private pantsdown: Pantsdown;
|
|
22
|
+
slugger = new GithubSlugger();
|
|
23
|
+
|
|
24
|
+
constructor(pantsdown: Pantsdown) {
|
|
25
|
+
this.pantsdown = pantsdown;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
code(code: string, infostring: string | undefined, sourceMap: SourceMap): string {
|
|
29
|
+
const lang = /^\S*/.exec(infostring ?? "")?.[0];
|
|
30
|
+
code = code.replace(/\n$/, "");
|
|
31
|
+
|
|
32
|
+
const attrs: HTMLAttrs = [];
|
|
33
|
+
|
|
34
|
+
if (lang === "mermaid") {
|
|
35
|
+
attrs.push(["class", "mermaid-container mermaid"]);
|
|
36
|
+
const result = `<pre style="position: relative;">` + code + "\n" + `</pre>`;
|
|
37
|
+
return injectHtmlAttributes(result, attrs, sourceMap);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (lang === "math") {
|
|
41
|
+
return this.latexBlock(code, sourceMap);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const language = lang && hljs.getLanguage(lang) ? lang : "plaintext";
|
|
45
|
+
code = hljs.highlight(code + "\n", { language }).value;
|
|
46
|
+
code = `<code class="hljs language-${escape(language)}">${code}</code>`;
|
|
47
|
+
|
|
48
|
+
const result = `<pre style="position: relative;">` + code + `</pre>`;
|
|
49
|
+
return injectHtmlAttributes(result, attrs, sourceMap);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
alert(body: string, token: Tokens["Alert"]): string {
|
|
53
|
+
const result = `<div><span>${token.icon + token.variant}</span>${body}</div>\n`;
|
|
54
|
+
return injectHtmlAttributes(result, [
|
|
55
|
+
["class", `markdown-alert markdown-alert-${token.variant.toLowerCase()}`],
|
|
56
|
+
]);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
blockquote(quote: string): string {
|
|
60
|
+
return `<blockquote>\n${quote}</blockquote>\n`;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
html(html: string, _block: boolean, sourceMap?: SourceMap): string {
|
|
64
|
+
let result = fixHtmlLocalImageHref(html, this.pantsdown.config);
|
|
65
|
+
result = addGithubImageStyles(result);
|
|
66
|
+
|
|
67
|
+
const attrs: HTMLAttrs = [];
|
|
68
|
+
|
|
69
|
+
if (this.pantsdown.config.renderer.detailsTagDefaultOpen) {
|
|
70
|
+
const tag = inline.tag.exec(html);
|
|
71
|
+
if (tag?.[0] === "<details>") {
|
|
72
|
+
attrs.push(["open", ""]);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return injectHtmlAttributes(result, attrs, sourceMap);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
heading(text: string, level: number, sourceMap: SourceMap): string {
|
|
80
|
+
const elementText = getHtmlElementText(text);
|
|
81
|
+
const slug = this.slugger.slug(elementText);
|
|
82
|
+
let result = `<h${level}>`;
|
|
83
|
+
// span with negative top to add some offset when scrolling to #slug
|
|
84
|
+
result += `<span style="position: absolute; top: -50px;" id="${slug}"></span>`;
|
|
85
|
+
result += `${text}<a class="anchor octicon-link" href="#${slug}"></a>`;
|
|
86
|
+
result += `</h${level}>\n`;
|
|
87
|
+
return injectHtmlAttributes(result, [["style", "position: relative;"]], sourceMap);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
hr(sourceMap: SourceMap): string {
|
|
91
|
+
return injectHtmlAttributes(`<hr>\n`, [], sourceMap);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
list(body: string, ordered: boolean, start: number | "", classes: string[] = []): string {
|
|
95
|
+
const type = ordered ? "ol" : "ul";
|
|
96
|
+
const attrs: HTMLAttrs = [["class", classes.join(" ")]];
|
|
97
|
+
if (ordered && start && start !== 1) {
|
|
98
|
+
attrs.push(["start", String(start)]);
|
|
99
|
+
}
|
|
100
|
+
const result = `<${type}>\n${body}</${type}>\n`;
|
|
101
|
+
return injectHtmlAttributes(result, attrs);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
listitem(text: string, task: boolean, _checked: boolean, sourceMap: SourceMap): string {
|
|
105
|
+
const attrs: [string, string][] = [];
|
|
106
|
+
if (task) attrs.push(["class", "task-list-item"]);
|
|
107
|
+
const result = `<li>${text}</li>\n`;
|
|
108
|
+
return injectHtmlAttributes(result, attrs, sourceMap);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
checkbox(checked: boolean, classes: string[] = []): string {
|
|
112
|
+
const result = "<input>";
|
|
113
|
+
const attrs: HTMLAttrs = [
|
|
114
|
+
["disabled", ""],
|
|
115
|
+
["type", "checkbox"],
|
|
116
|
+
["class", classes.join(" ")],
|
|
117
|
+
];
|
|
118
|
+
if (checked) attrs.push(["checked", ""]);
|
|
119
|
+
return injectHtmlAttributes(result, attrs);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
paragraph(text: string, sourceMap: SourceMap): string {
|
|
123
|
+
const result = `<p>${text}</p>\n`;
|
|
124
|
+
return injectHtmlAttributes(result, [], sourceMap);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
table(header: string, body: string): string {
|
|
128
|
+
if (body) body = `<tbody>${body}</tbody>`;
|
|
129
|
+
return "<table>\n" + "<thead>\n" + header + "</thead>\n" + body + "</table>\n";
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
tablerow(content: string, sourceMapStart: number | undefined): string {
|
|
133
|
+
const sourceMap: SourceMap = sourceMapStart ? [sourceMapStart, sourceMapStart] : undefined;
|
|
134
|
+
return injectHtmlAttributes(`<tr>\n${content}</tr>\n`, [], sourceMap);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
tablecell(
|
|
138
|
+
content: string,
|
|
139
|
+
flags: {
|
|
140
|
+
header: boolean;
|
|
141
|
+
align: "center" | "left" | "right" | null;
|
|
142
|
+
},
|
|
143
|
+
): string {
|
|
144
|
+
const type = flags.header ? "th" : "td";
|
|
145
|
+
const attrs: HTMLAttrs = [];
|
|
146
|
+
if (flags.align) attrs.push(["align", flags.align]);
|
|
147
|
+
const result = `<${type}>` + content + `</${type}>\n`;
|
|
148
|
+
return injectHtmlAttributes(result, attrs);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* span level renderer
|
|
153
|
+
*/
|
|
154
|
+
strong(text: string): string {
|
|
155
|
+
return `<strong>${text}</strong>`;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
em(text: string): string {
|
|
159
|
+
return `<em>${text}</em>`;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
codespan(text: string): string {
|
|
163
|
+
return `<code>${text}</code>`;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
br(): string {
|
|
167
|
+
return "<br>";
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
del(text: string): string {
|
|
171
|
+
return `<del>${text}</del>`;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
link(href: string, title: string | null | undefined, text: string): string {
|
|
175
|
+
const cleanHref = cleanUrl(href);
|
|
176
|
+
if (cleanHref === null) {
|
|
177
|
+
return text;
|
|
178
|
+
}
|
|
179
|
+
const attrs: HTMLAttrs = [["href", cleanHref]];
|
|
180
|
+
if (title) attrs.push(["title", title]);
|
|
181
|
+
return injectHtmlAttributes(`<a>${text}</a>`, attrs);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
image(href: string, title: string | null, text: string): string {
|
|
185
|
+
const cleanHref = cleanUrl(href);
|
|
186
|
+
if (cleanHref === null) {
|
|
187
|
+
return text;
|
|
188
|
+
}
|
|
189
|
+
const attrs: HTMLAttrs = [
|
|
190
|
+
["src", fixLocalImageHref(cleanHref, this.pantsdown.config)],
|
|
191
|
+
["alt", text],
|
|
192
|
+
];
|
|
193
|
+
if (title) attrs.push(["title", title]);
|
|
194
|
+
return injectHtmlAttributes("<img>", attrs);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
footnoteRef(token: Tokens["FootnoteRef"]) {
|
|
198
|
+
const encodedLabel = encodeURIComponent(token.label);
|
|
199
|
+
|
|
200
|
+
return `<sup><a id="footnote-ref-${encodedLabel}" href="#${
|
|
201
|
+
"footnote-" + encodedLabel
|
|
202
|
+
}" data-footnote-ref aria-describedby="footnote-label">${token.label}</a></sup>`;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
footnotes(token: Tokens["Footnotes"], body: string) {
|
|
206
|
+
if (!token.items.length) return "";
|
|
207
|
+
|
|
208
|
+
let footnotesHTML = '<section class="footnotes" data-footnotes>\n';
|
|
209
|
+
footnotesHTML += `<ol>\n${body}\n</ol>\n`;
|
|
210
|
+
footnotesHTML += "</section>\n";
|
|
211
|
+
|
|
212
|
+
return footnotesHTML;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
text(text: string): string {
|
|
216
|
+
return text;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
latexBlock(latex: string, sourceMap: SourceMap): string {
|
|
220
|
+
try {
|
|
221
|
+
const rendered = renderToString(latex, {
|
|
222
|
+
displayMode: true,
|
|
223
|
+
throwOnError: false,
|
|
224
|
+
output: "html",
|
|
225
|
+
trust: false,
|
|
226
|
+
});
|
|
227
|
+
return injectHtmlAttributes(`<div class="katex-block">${rendered}</div>\n`, [], sourceMap);
|
|
228
|
+
} catch {
|
|
229
|
+
return injectHtmlAttributes(
|
|
230
|
+
`<div class="katex-block katex-error"><code>${escape(latex)}</code></div>\n`,
|
|
231
|
+
[],
|
|
232
|
+
sourceMap,
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
latexInline(latex: string): string {
|
|
238
|
+
try {
|
|
239
|
+
const rendered = renderToString(latex, {
|
|
240
|
+
displayMode: false,
|
|
241
|
+
throwOnError: false,
|
|
242
|
+
output: "html",
|
|
243
|
+
trust: false,
|
|
244
|
+
});
|
|
245
|
+
return `<span class="katex-inline">${rendered}</span>`;
|
|
246
|
+
} catch {
|
|
247
|
+
return `<span class="katex-inline katex-error"><code>${escape(latex)}</code></span>`;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
258
250
|
}
|