pantsdown 1.7.1 → 1.8.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 +26 -0
- package/README.md +18 -6
- package/package.json +1 -1
- package/src/css/styles.css +48 -0
- package/src/parser.ts +5 -0
- package/src/renderer.ts +7 -0
- package/src/rules/block.ts +1 -5
- package/src/tokenizer.ts +35 -4
- package/src/types.ts +9 -0
- package/src/utils.ts +23 -0
package/LICENSE
CHANGED
|
@@ -22,6 +22,32 @@ THE SOFTWARE.
|
|
|
22
22
|
|
|
23
23
|
----------------------------------------------------
|
|
24
24
|
|
|
25
|
+
marked-extensions (https://github.com/bent10/marked-extensions)
|
|
26
|
+
|
|
27
|
+
The MIT License (MIT)
|
|
28
|
+
|
|
29
|
+
Copyright (c) 2023 Stilearning (https://stilearning.com)
|
|
30
|
+
|
|
31
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
32
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
33
|
+
in the Software without restriction, including without limitation the rights
|
|
34
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
35
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
36
|
+
furnished to do so, subject to the following conditions:
|
|
37
|
+
|
|
38
|
+
The above copyright notice and this permission notice shall be included in
|
|
39
|
+
all copies or substantial portions of the Software.
|
|
40
|
+
|
|
41
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
42
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
43
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
44
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
45
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
46
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
47
|
+
THE SOFTWARE.
|
|
48
|
+
|
|
49
|
+
----------------------------------------------------
|
|
50
|
+
|
|
25
51
|
highlight.js (https://github.com/highlightjs/highlight.js)
|
|
26
52
|
|
|
27
53
|
BSD 3-Clause License
|
package/README.md
CHANGED
|
@@ -11,9 +11,9 @@ some features developed specifically for [github-preview.nvim](https://github.co
|
|
|
11
11
|
If you need a feature that's supported by GitHub and is not already listed in the [Roadmap](#roadmap),
|
|
12
12
|
feel free to open an issue.
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
## [▶️ Demo](https://wallpants.github.io/pantsdown/)
|
|
15
15
|
|
|
16
|
-
## Installation
|
|
16
|
+
## 📦 Installation
|
|
17
17
|
|
|
18
18
|
🚨 Pantsdown is distributed only as a TypeScript module. This means you'll need a bundler to handle transpilation.
|
|
19
19
|
See below for usage examples.
|
|
@@ -25,7 +25,7 @@ bun install pantsdown
|
|
|
25
25
|
npm install pantsdown
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
-
## Usage
|
|
28
|
+
## 💻 Usage
|
|
29
29
|
|
|
30
30
|
### Styles
|
|
31
31
|
|
|
@@ -79,7 +79,7 @@ function App() {
|
|
|
79
79
|
if (container) container.innerHTML = html;
|
|
80
80
|
}, []);
|
|
81
81
|
|
|
82
|
-
//
|
|
82
|
+
// ⚠️ for styles to be applied, a parent element must have
|
|
83
83
|
// the classes "pantsdown light" or "pantsdown dark" added
|
|
84
84
|
return <div id="markdown-container" className="pantsdown light" />;
|
|
85
85
|
}
|
|
@@ -87,7 +87,7 @@ function App() {
|
|
|
87
87
|
export default App;
|
|
88
88
|
```
|
|
89
89
|
|
|
90
|
-
## Configuration
|
|
90
|
+
## ⚙️ Configuration
|
|
91
91
|
|
|
92
92
|
The Pantsdown constructor accepts an optional configuration object:
|
|
93
93
|
|
|
@@ -129,6 +129,18 @@ const html = pantsdown.parse(markdown);
|
|
|
129
129
|
console.log(html);
|
|
130
130
|
```
|
|
131
131
|
|
|
132
|
-
##
|
|
132
|
+
## 🤝 Acknowledgements
|
|
133
133
|
|
|
134
|
+
<a href="https://marked.js.org">
|
|
135
|
+
<img width="60px" height="60px" src="https://marked.js.org/img/logo-black.svg" align="right" />
|
|
136
|
+
</a>
|
|
137
|
+
|
|
138
|
+
Pantsdown is based on [Marked](https://github.com/markedjs/marked).
|
|
139
|
+
Without their hard work, Pantsdown would not exist.
|
|
140
|
+
|
|
141
|
+
## 🚀 Roadmap
|
|
142
|
+
|
|
143
|
+
- [ ] check if copy code button works with current vite docs
|
|
144
|
+
- [ ] showcase more in demo | list features
|
|
145
|
+
- [ ] [Alerts](https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#alerts)
|
|
134
146
|
- [ ] [Mermaid](https://mermaid.js.org/) usage docs.
|
package/package.json
CHANGED
package/src/css/styles.css
CHANGED
|
@@ -19,9 +19,11 @@
|
|
|
19
19
|
--color-neutral-muted: rgba(110, 118, 129, 0.4);
|
|
20
20
|
--color-accent-fg: #2f81f7;
|
|
21
21
|
--color-accent-emphasis: #1f6feb;
|
|
22
|
+
--color-fg-attention: #d29922;
|
|
22
23
|
--color-attention-subtle: rgba(187, 128, 9, 0.15);
|
|
23
24
|
--color-attention-muted: rgba(187, 128, 9, 0.4);
|
|
24
25
|
--color-danger-fg: #f85149;
|
|
26
|
+
--color-done-fg: #a371f7;
|
|
25
27
|
--color-icon-directory: #7d8590;
|
|
26
28
|
--color-icon-file: #7d8590;
|
|
27
29
|
--color-btn-bg: #21262d;
|
|
@@ -41,9 +43,11 @@
|
|
|
41
43
|
--color-neutral-muted: rgba(175, 184, 193, 0.2);
|
|
42
44
|
--color-accent-fg: #0969da;
|
|
43
45
|
--color-accent-emphasis: #0969da;
|
|
46
|
+
--color-fg-attention: #9a6700;
|
|
44
47
|
--color-attention-subtle: #fff8c5;
|
|
45
48
|
--color-attention-muted: rgba(212, 167, 44, 0.4);
|
|
46
49
|
--color-danger-fg: #d1242f;
|
|
50
|
+
--color-done-fg: #8250df;
|
|
47
51
|
--color-icon-directory: #54aeff;
|
|
48
52
|
--color-icon-file: #656d76;
|
|
49
53
|
--color-btn-bg: #f6f8fa;
|
|
@@ -64,6 +68,41 @@
|
|
|
64
68
|
word-wrap: break-word;
|
|
65
69
|
}
|
|
66
70
|
|
|
71
|
+
.pantsdown .markdown-alert {
|
|
72
|
+
border-left: 0.25em solid;
|
|
73
|
+
margin-bottom: 16px;
|
|
74
|
+
padding: 0 1em;
|
|
75
|
+
color: inherit;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
.pantsdown .markdown-alert span {
|
|
79
|
+
font-weight: 500;
|
|
80
|
+
display: flex;
|
|
81
|
+
align-items: center;
|
|
82
|
+
margin-bottom: 4px;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
.pantsdown .markdown-alert-note {
|
|
86
|
+
border-color: var(--color-accent-fg);
|
|
87
|
+
}
|
|
88
|
+
.pantsdown .markdown-alert-note span {
|
|
89
|
+
color: var(--color-accent-fg);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
.pantsdown .markdown-alert-important {
|
|
93
|
+
border-color: var(--color-done-fg);
|
|
94
|
+
}
|
|
95
|
+
.pantsdown .markdown-alert-important span {
|
|
96
|
+
color: var(--color-done-fg);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
.pantsdown .markdown-alert-warning {
|
|
100
|
+
border-color: var(--color-fg-attention);
|
|
101
|
+
}
|
|
102
|
+
.pantsdown .markdown-alert-warning span {
|
|
103
|
+
color: var(--color-fg-attention);
|
|
104
|
+
}
|
|
105
|
+
|
|
67
106
|
.pantsdown .copy-button {
|
|
68
107
|
position: absolute;
|
|
69
108
|
left: calc(100% - 36px);
|
|
@@ -113,6 +152,15 @@
|
|
|
113
152
|
outline: none;
|
|
114
153
|
}
|
|
115
154
|
|
|
155
|
+
.pantsdown h1 a,
|
|
156
|
+
.pantsdown h2 a,
|
|
157
|
+
.pantsdown h3 a,
|
|
158
|
+
.pantsdown h4 a,
|
|
159
|
+
.pantsdown h5 a,
|
|
160
|
+
.pantsdown h6 a {
|
|
161
|
+
text-decoration: none;
|
|
162
|
+
}
|
|
163
|
+
|
|
116
164
|
.pantsdown h1 .anchor.octicon-link:after,
|
|
117
165
|
.pantsdown h2 .anchor.octicon-link:after,
|
|
118
166
|
.pantsdown h3 .anchor.octicon-link:after,
|
package/src/parser.ts
CHANGED
|
@@ -75,6 +75,11 @@ export class Parser {
|
|
|
75
75
|
out += this.renderer.table(header, body);
|
|
76
76
|
continue;
|
|
77
77
|
}
|
|
78
|
+
case "alert": {
|
|
79
|
+
const body = this.parse(token.tokens);
|
|
80
|
+
out += this.renderer.alert(body, token);
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
78
83
|
case "blockquote": {
|
|
79
84
|
const body = this.parse(token.tokens);
|
|
80
85
|
out += this.renderer.blockquote(body);
|
package/src/renderer.ts
CHANGED
|
@@ -52,6 +52,13 @@ export class Renderer {
|
|
|
52
52
|
return injectHtmlAttributes(result, [], sourceMap);
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
+
alert(body: string, token: Tokens["Alert"]): string {
|
|
56
|
+
const result = `<div>\n${body}</div>\n`;
|
|
57
|
+
return injectHtmlAttributes(result, [
|
|
58
|
+
["class", `markdown-alert markdown-alert-${token.variant.toLowerCase()}`],
|
|
59
|
+
]);
|
|
60
|
+
}
|
|
61
|
+
|
|
55
62
|
blockquote(quote: string): string {
|
|
56
63
|
return `<blockquote>\n${quote}</blockquote>\n`;
|
|
57
64
|
}
|
package/src/rules/block.ts
CHANGED
|
@@ -111,10 +111,6 @@ const block_blockquote = edit(/^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/)
|
|
|
111
111
|
.replace("paragraph", block_paragraph)
|
|
112
112
|
.getRegex();
|
|
113
113
|
|
|
114
|
-
const block_footnote = edit(/^( {0,3}\[\^(\d+)\]:(?:[ \t]+|[\n]*?|$)?(paragraph|[^\n]*))+/)
|
|
115
|
-
.replace("paragraph", block_paragraph)
|
|
116
|
-
.getRegex();
|
|
117
|
-
|
|
118
114
|
export const block: Record<BlockRuleNames, RegExp> = {
|
|
119
115
|
newline: /^(?: *(?:\n|$))+/,
|
|
120
116
|
code: /^( {4}[^\n]+(?:\n(?: *(?:\n|$))*)?)+/,
|
|
@@ -130,6 +126,6 @@ export const block: Record<BlockRuleNames, RegExp> = {
|
|
|
130
126
|
table: block_table,
|
|
131
127
|
lheading: block_lheading,
|
|
132
128
|
paragraph: block_paragraph,
|
|
133
|
-
footnote:
|
|
129
|
+
footnote: /^\[\^([^\]\n]+)\]:(?:[ \t]+|[\n]*?|$)([^\[\n]*?(?:\n|$)(?:[\n]*?[ ]{4,}[^\[\n]*)*)/,
|
|
134
130
|
text: /^[^\n]+/,
|
|
135
131
|
};
|
package/src/tokenizer.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { block } from "./rules/block.ts";
|
|
|
3
3
|
import { inline } from "./rules/inline.ts";
|
|
4
4
|
import { type Links, type Tokens } from "./types.ts";
|
|
5
5
|
import {
|
|
6
|
+
ALERTS,
|
|
6
7
|
escape,
|
|
7
8
|
findClosingBracket,
|
|
8
9
|
indentCodeCompensation,
|
|
@@ -100,7 +101,7 @@ export class Tokenizer {
|
|
|
100
101
|
};
|
|
101
102
|
}
|
|
102
103
|
|
|
103
|
-
blockquote(src: string): Tokens["Blockquote"] | undefined {
|
|
104
|
+
blockquote(src: string): Tokens["Blockquote"] | Tokens["Alert"] | undefined {
|
|
104
105
|
const cap = block.blockquote.exec(src);
|
|
105
106
|
if (!cap) return undefined;
|
|
106
107
|
|
|
@@ -110,12 +111,42 @@ export class Tokenizer {
|
|
|
110
111
|
const tokens = this.lexer.blockTokens(text, []);
|
|
111
112
|
this.lexer.state.top = top;
|
|
112
113
|
this.lexer.line++;
|
|
113
|
-
|
|
114
|
+
|
|
115
|
+
const blockquoteToken: Tokens["Blockquote"] = {
|
|
114
116
|
type: "blockquote",
|
|
115
117
|
raw: cap[0],
|
|
116
118
|
tokens,
|
|
117
119
|
text,
|
|
118
120
|
};
|
|
121
|
+
|
|
122
|
+
const matchedVariant = ALERTS.find(({ regex }) => regex.test(blockquoteToken.text));
|
|
123
|
+
|
|
124
|
+
if (matchedVariant) {
|
|
125
|
+
const { variant, icon, regex } = matchedVariant;
|
|
126
|
+
|
|
127
|
+
const firstLine = blockquoteToken.tokens[0] as Tokens["Paragraph"];
|
|
128
|
+
const firstLineText = firstLine.raw.replace(regex, "");
|
|
129
|
+
firstLine.tokens = [
|
|
130
|
+
{
|
|
131
|
+
type: "text",
|
|
132
|
+
raw: firstLine.raw,
|
|
133
|
+
text: `<span>${icon + variant}</span>${firstLineText}`,
|
|
134
|
+
},
|
|
135
|
+
];
|
|
136
|
+
|
|
137
|
+
const alertToken: Tokens["Alert"] = {
|
|
138
|
+
...blockquoteToken,
|
|
139
|
+
type: "alert",
|
|
140
|
+
variant: variant as Tokens["Alert"]["variant"],
|
|
141
|
+
icon,
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
alertToken.tokens.splice(0, 1, firstLine);
|
|
145
|
+
|
|
146
|
+
return alertToken;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return blockquoteToken;
|
|
119
150
|
}
|
|
120
151
|
|
|
121
152
|
list(src: string): Tokens["List"] | undefined {
|
|
@@ -330,8 +361,8 @@ export class Tokenizer {
|
|
|
330
361
|
const cap = block.footnote.exec(src);
|
|
331
362
|
if (!cap) return undefined;
|
|
332
363
|
|
|
333
|
-
const label = cap[
|
|
334
|
-
let text = cap[
|
|
364
|
+
const label = cap[1] ?? "";
|
|
365
|
+
let text = cap[2] ? rtrim(cap[2].replace(/^ *[ \t]?/gm, ""), "\n") : "";
|
|
335
366
|
|
|
336
367
|
text += `<a href="#footnote-ref-${encodeURIComponent(
|
|
337
368
|
label,
|
package/src/types.ts
CHANGED
|
@@ -23,6 +23,14 @@ export interface Tokens extends Record<string, BaseToken> {
|
|
|
23
23
|
lang?: string | undefined;
|
|
24
24
|
sourceMap: SourceMap;
|
|
25
25
|
};
|
|
26
|
+
Alert: {
|
|
27
|
+
type: "alert";
|
|
28
|
+
raw: string;
|
|
29
|
+
text: string;
|
|
30
|
+
icon: string;
|
|
31
|
+
tokens: Token[];
|
|
32
|
+
variant: "Note" | "Important" | "Warning";
|
|
33
|
+
};
|
|
26
34
|
Heading: {
|
|
27
35
|
type: "heading";
|
|
28
36
|
raw: string;
|
|
@@ -203,6 +211,7 @@ export type Token =
|
|
|
203
211
|
| Tokens["Codespan"]
|
|
204
212
|
| Tokens["Br"]
|
|
205
213
|
| Tokens["Del"]
|
|
214
|
+
| Tokens["Alert"]
|
|
206
215
|
| Tokens["Footnote"]
|
|
207
216
|
| Tokens["FootnoteRef"]
|
|
208
217
|
| Tokens["Footnotes"];
|
package/src/utils.ts
CHANGED
|
@@ -98,6 +98,7 @@ export function fixLocalImageHref(
|
|
|
98
98
|
return href;
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
+
// TODO: do we need this?
|
|
101
102
|
if (href.startsWith("/")) {
|
|
102
103
|
// the URL is from root
|
|
103
104
|
return href;
|
|
@@ -275,3 +276,25 @@ export function indentCodeCompensation(raw: string, text: string) {
|
|
|
275
276
|
})
|
|
276
277
|
.join("\n");
|
|
277
278
|
}
|
|
279
|
+
|
|
280
|
+
function makeAlertRegex(type: string) {
|
|
281
|
+
return new RegExp(`^(?:\\[\\!${type.toUpperCase()}\\]|[\\*]{2}${type}[\\*]{2})[\s]*?\n?`);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export const ALERTS = [
|
|
285
|
+
{
|
|
286
|
+
variant: "Note",
|
|
287
|
+
regex: makeAlertRegex("Note"),
|
|
288
|
+
icon: '<svg class="octicon octicon-info mr-2" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path d="M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z"></path></svg>',
|
|
289
|
+
},
|
|
290
|
+
{
|
|
291
|
+
variant: "Important",
|
|
292
|
+
regex: makeAlertRegex("Important"),
|
|
293
|
+
icon: '<svg class="octicon octicon-report mr-2" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path d="M0 1.75C0 .784.784 0 1.75 0h12.5C15.216 0 16 .784 16 1.75v9.5A1.75 1.75 0 0 1 14.25 13H8.06l-2.573 2.573A1.458 1.458 0 0 1 3 14.543V13H1.75A1.75 1.75 0 0 1 0 11.25Zm1.75-.25a.25.25 0 0 0-.25.25v9.5c0 .138.112.25.25.25h2a.75.75 0 0 1 .75.75v2.19l2.72-2.72a.749.749 0 0 1 .53-.22h6.5a.25.25 0 0 0 .25-.25v-9.5a.25.25 0 0 0-.25-.25Zm7 2.25v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 9a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path></svg>',
|
|
294
|
+
},
|
|
295
|
+
{
|
|
296
|
+
variant: "Warning",
|
|
297
|
+
regex: makeAlertRegex("Warning"),
|
|
298
|
+
icon: '<svg class="octicon octicon-alert mr-2" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path></svg>',
|
|
299
|
+
},
|
|
300
|
+
];
|