marked 7.0.1 → 7.0.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.
@@ -1,212 +0,0 @@
1
- import type { Token, Tokens, TokensList } from './Tokens.ts';
2
- import { _Parser } from './Parser.ts';
3
- import { _Lexer } from './Lexer.ts';
4
- import { _Renderer } from './Renderer.ts';
5
- import { _Tokenizer } from './Tokenizer.ts';
6
-
7
- export interface SluggerOptions {
8
- /** Generates the next unique slug without updating the internal accumulator. */
9
- dryrun?: boolean;
10
- }
11
-
12
- export interface TokenizerThis {
13
- lexer: _Lexer;
14
- }
15
-
16
- export interface TokenizerExtension {
17
- name: string;
18
- level: 'block' | 'inline';
19
- start?: ((this: TokenizerThis, src: string) => number | void) | undefined;
20
- tokenizer: (this: TokenizerThis, src: string, tokens: Token[] | TokensList) => Tokens.Generic | void;
21
- childTokens?: string[] | undefined;
22
- }
23
-
24
- export interface RendererThis {
25
- parser: _Parser;
26
- }
27
-
28
- export interface RendererExtension {
29
- name: string;
30
- renderer: (this: RendererThis, token: Tokens.Generic) => string | false | undefined;
31
- }
32
-
33
- export type TokenizerAndRendererExtension = TokenizerExtension | RendererExtension | (TokenizerExtension & RendererExtension);
34
-
35
- type RendererApi = Omit<_Renderer, 'constructor' | 'options'>;
36
- type RendererObject = {
37
- [K in keyof RendererApi]?: (...args: Parameters<RendererApi[K]>) => ReturnType<RendererApi[K]> | false
38
- };
39
-
40
- type TokenizerApi = Omit<_Tokenizer, 'constructor' | 'options' | 'rules' | 'lexer'>;
41
- type TokenizerObject = {
42
- [K in keyof TokenizerApi]?: (...args: Parameters<TokenizerApi[K]>) => ReturnType<TokenizerApi[K]> | false
43
- };
44
-
45
- export interface MarkedExtension {
46
- /**
47
- * True will tell marked to await any walkTokens functions before parsing the tokens and returning an HTML string.
48
- */
49
- async?: boolean;
50
-
51
- /**
52
- * A prefix URL for any relative link.
53
- * @deprecated Deprecated in v5.0.0 use marked-base-url to prefix url for any relative link.
54
- */
55
- baseUrl?: string | undefined | null;
56
-
57
- /**
58
- * Enable GFM line breaks. This option requires the gfm option to be true.
59
- */
60
- breaks?: boolean | undefined;
61
-
62
- /**
63
- * Add tokenizers and renderers to marked
64
- */
65
- extensions?:
66
- | TokenizerAndRendererExtension[]
67
- | undefined | null;
68
-
69
- /**
70
- * Enable GitHub flavored markdown.
71
- */
72
- gfm?: boolean | undefined;
73
-
74
- /**
75
- * Include an id attribute when emitting headings.
76
- * @deprecated Deprecated in v5.0.0 use marked-gfm-heading-id to include an id attribute when emitting headings (h1, h2, h3, etc).
77
- */
78
- headerIds?: boolean | undefined;
79
-
80
- /**
81
- * Set the prefix for header tag ids.
82
- * @deprecated Deprecated in v5.0.0 use marked-gfm-heading-id to add a string to prefix the id attribute when emitting headings (h1, h2, h3, etc).
83
- */
84
- headerPrefix?: string | undefined;
85
-
86
- /**
87
- * A function to highlight code blocks. The function can either be
88
- * synchronous (returning a string) or asynchronous (callback invoked
89
- * with an error if any occurred during highlighting and a string
90
- * if highlighting was successful)
91
- * @deprecated Deprecated in v5.0.0 use marked-highlight to add highlighting to code blocks.
92
- */
93
- highlight?: ((code: string, lang: string | undefined, callback?: (error: Error, code?: string) => void) => string | void) | null;
94
-
95
- /**
96
- * Hooks are methods that hook into some part of marked.
97
- * preprocess is called to process markdown before sending it to marked.
98
- * postprocess is called to process html after marked has finished parsing.
99
- */
100
- hooks?: {
101
- preprocess: (markdown: string) => string,
102
- postprocess: (html: string | undefined) => string | undefined,
103
- // eslint-disable-next-line no-use-before-define
104
- options?: MarkedOptions
105
- } | null;
106
-
107
- /**
108
- * Set the prefix for code block classes.
109
- * @deprecated Deprecated in v5.0.0 use marked-highlight to prefix the className in a <code> block. Useful for syntax highlighting.
110
- */
111
- langPrefix?: string | undefined;
112
-
113
- /**
114
- * Mangle autolinks (<email@domain.com>).
115
- * @deprecated Deprecated in v5.0.0 use marked-mangle to mangle email addresses.
116
- */
117
- mangle?: boolean | undefined;
118
-
119
- /**
120
- * Conform to obscure parts of markdown.pl as much as possible. Don't fix any of the original markdown bugs or poor behavior.
121
- */
122
- pedantic?: boolean | undefined;
123
-
124
- /**
125
- * Type: object Default: new Renderer()
126
- *
127
- * An object containing functions to render tokens to HTML.
128
- */
129
- renderer?: RendererObject | undefined | null;
130
-
131
- /**
132
- * Sanitize the output. Ignore any HTML that has been input. If true, sanitize the HTML passed into markdownString with the sanitizer function.
133
- * @deprecated Warning: This feature is deprecated and it should NOT be used as it cannot be considered secure. Instead use a sanitize library, like DOMPurify (recommended), sanitize-html or insane on the output HTML!
134
- */
135
- sanitize?: boolean | undefined;
136
-
137
- /**
138
- * Optionally sanitize found HTML with a sanitizer function.
139
- * @deprecated A function to sanitize the HTML passed into markdownString.
140
- */
141
- sanitizer?: ((html: string) => string) | null;
142
-
143
- /**
144
- * Shows an HTML error message when rendering fails.
145
- */
146
- silent?: boolean | undefined;
147
-
148
- /**
149
- * Use smarter list behavior than the original markdown. May eventually be default with the old behavior moved into pedantic.
150
- */
151
- smartLists?: boolean | undefined;
152
-
153
- /**
154
- * Use "smart" typograhic punctuation for things like quotes and dashes.
155
- * @deprecated Deprecated in v5.0.0 use marked-smartypants to use "smart" typographic punctuation for things like quotes and dashes.
156
- */
157
- smartypants?: boolean | undefined;
158
-
159
- /**
160
- * The tokenizer defines how to turn markdown text into tokens.
161
- */
162
- tokenizer?: TokenizerObject | undefined | null;
163
-
164
- /**
165
- * The walkTokens function gets called with every token.
166
- * Child tokens are called before moving on to sibling tokens.
167
- * Each token is passed by reference so updates are persisted when passed to the parser.
168
- * The return value of the function is ignored.
169
- */
170
- walkTokens?: ((token: Token) => void | Promise<void>) | undefined | null;
171
- /**
172
- * Generate closing slash for self-closing tags (<br/> instead of <br>)
173
- * @deprecated Deprecated in v5.0.0 use marked-xhtml to emit self-closing HTML tags for void elements (<br/>, <img/>, etc.) with a "/" as required by XHTML.
174
- */
175
- xhtml?: boolean | undefined;
176
- }
177
-
178
- export interface MarkedOptions extends Omit<MarkedExtension, 'extensions' | 'renderer' | 'tokenizer' | 'walkTokens'> {
179
- /**
180
- * Type: object Default: new Renderer()
181
- *
182
- * An object containing functions to render tokens to HTML.
183
- */
184
- renderer?: Omit<_Renderer, 'constructor'> | undefined | null;
185
-
186
- /**
187
- * The tokenizer defines how to turn markdown text into tokens.
188
- */
189
- tokenizer?: Omit<_Tokenizer, 'constructor'> | undefined | null;
190
-
191
- /**
192
- * The walkTokens function gets called with every token.
193
- * Child tokens are called before moving on to sibling tokens.
194
- * Each token is passed by reference so updates are persisted when passed to the parser.
195
- * The return value of the function is ignored.
196
- */
197
- walkTokens?: ((token: Token) => void | Promise<void> | Array<void | Promise<void>>) | undefined | null;
198
-
199
- /**
200
- * Add tokenizers and renderers to marked
201
- */
202
- extensions?:
203
- | (TokenizerAndRendererExtension[] & {
204
- renderers: Record<string, (this: RendererThis, token: Tokens.Generic) => string | false | undefined>,
205
- childTokens: Record<string, string[]>,
206
- block: any[],
207
- inline: any[],
208
- startBlock: Array<(this: TokenizerThis, src: string) => number | void>,
209
- startInline: Array<(this: TokenizerThis, src: string) => number | void>
210
- })
211
- | undefined | null;
212
- }
package/src/Parser.ts DELETED
@@ -1,291 +0,0 @@
1
- import { _Renderer } from './Renderer.ts';
2
- import { _TextRenderer } from './TextRenderer.ts';
3
- import { _Slugger } from './Slugger.ts';
4
- import { _defaults } from './defaults.ts';
5
- import {
6
- unescape
7
- } from './helpers.ts';
8
- import type { Token, Tokens } from './Tokens.ts';
9
- import type { MarkedOptions } from './MarkedOptions.ts';
10
-
11
- /**
12
- * Parsing & Compiling
13
- */
14
- export class _Parser {
15
- options: MarkedOptions;
16
- renderer: _Renderer;
17
- textRenderer: _TextRenderer;
18
- slugger: _Slugger;
19
- constructor(options?: MarkedOptions) {
20
- this.options = options || _defaults;
21
- this.options.renderer = this.options.renderer || new _Renderer();
22
- this.renderer = this.options.renderer;
23
- this.renderer.options = this.options;
24
- this.textRenderer = new _TextRenderer();
25
- this.slugger = new _Slugger();
26
- }
27
-
28
- /**
29
- * Static Parse Method
30
- */
31
- static parse(tokens: Token[], options?: MarkedOptions) {
32
- const parser = new _Parser(options);
33
- return parser.parse(tokens);
34
- }
35
-
36
- /**
37
- * Static Parse Inline Method
38
- */
39
- static parseInline(tokens: Token[], options?: MarkedOptions) {
40
- const parser = new _Parser(options);
41
- return parser.parseInline(tokens);
42
- }
43
-
44
- /**
45
- * Parse Loop
46
- */
47
- parse(tokens: Token[], top = true): string {
48
- let out = '',
49
- i,
50
- j,
51
- k,
52
- l2,
53
- l3,
54
- row,
55
- cell,
56
- header,
57
- body,
58
- token,
59
- ordered,
60
- start,
61
- loose,
62
- itemBody,
63
- item,
64
- checked,
65
- task,
66
- checkbox,
67
- ret;
68
-
69
- const l = tokens.length;
70
- for (i = 0; i < l; i++) {
71
- token = tokens[i];
72
-
73
- // Run any renderer extensions
74
- if (this.options.extensions && this.options.extensions.renderers && this.options.extensions.renderers[token.type]) {
75
- ret = this.options.extensions.renderers[token.type].call({ parser: this }, token);
76
- if (ret !== false || !['space', 'hr', 'heading', 'code', 'table', 'blockquote', 'list', 'html', 'paragraph', 'text'].includes(token.type)) {
77
- out += ret || '';
78
- continue;
79
- }
80
- }
81
-
82
- switch (token.type) {
83
- case 'space': {
84
- continue;
85
- }
86
- case 'hr': {
87
- out += this.renderer.hr();
88
- continue;
89
- }
90
- case 'heading': {
91
- out += this.renderer.heading(
92
- this.parseInline(token.tokens) as string,
93
- token.depth,
94
- unescape(this.parseInline(token.tokens, this.textRenderer) as string),
95
- this.slugger);
96
- continue;
97
- }
98
- case 'code': {
99
- out += this.renderer.code(token.text,
100
- token.lang,
101
- !!token.escaped);
102
- continue;
103
- }
104
- case 'table': {
105
- header = '';
106
-
107
- // header
108
- cell = '';
109
- l2 = token.header.length;
110
- for (j = 0; j < l2; j++) {
111
- cell += this.renderer.tablecell(
112
- this.parseInline(token.header[j].tokens)!,
113
- { header: true, align: token.align[j] }
114
- );
115
- }
116
- header += this.renderer.tablerow(cell);
117
-
118
- body = '';
119
- l2 = token.rows.length;
120
- for (j = 0; j < l2; j++) {
121
- row = token.rows[j];
122
-
123
- cell = '';
124
- l3 = row.length;
125
- for (k = 0; k < l3; k++) {
126
- cell += this.renderer.tablecell(
127
- this.parseInline(row[k].tokens)!,
128
- { header: false, align: token.align[k] }
129
- );
130
- }
131
-
132
- body += this.renderer.tablerow(cell);
133
- }
134
- out += this.renderer.table(header, body);
135
- continue;
136
- }
137
- case 'blockquote': {
138
- body = this.parse(token.tokens)!;
139
- out += this.renderer.blockquote(body);
140
- continue;
141
- }
142
- case 'list': {
143
- ordered = token.ordered;
144
- start = token.start;
145
- loose = token.loose;
146
- l2 = token.items.length;
147
-
148
- body = '';
149
- for (j = 0; j < l2; j++) {
150
- item = token.items[j];
151
- checked = item.checked;
152
- task = item.task;
153
-
154
- itemBody = '';
155
- if (item.task) {
156
- checkbox = this.renderer.checkbox(!!checked);
157
- if (loose) {
158
- if (item.tokens.length > 0 && item.tokens[0].type === 'paragraph') {
159
- item.tokens[0].text = checkbox + ' ' + item.tokens[0].text;
160
- if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === 'text') {
161
- item.tokens[0].tokens[0].text = checkbox + ' ' + item.tokens[0].tokens[0].text;
162
- }
163
- } else {
164
- item.tokens.unshift({
165
- type: 'text',
166
- text: checkbox
167
- } as Tokens.Text);
168
- }
169
- } else {
170
- itemBody += checkbox;
171
- }
172
- }
173
-
174
- itemBody += this.parse(item.tokens, loose);
175
- body += this.renderer.listitem(itemBody, task, !!checked);
176
- }
177
-
178
- out += this.renderer.list(body, ordered, start);
179
- continue;
180
- }
181
- case 'html': {
182
- out += this.renderer.html(token.text, token.block);
183
- continue;
184
- }
185
- case 'paragraph': {
186
- out += this.renderer.paragraph(this.parseInline(token.tokens)!);
187
- continue;
188
- }
189
- case 'text': {
190
- body = token.tokens ? this.parseInline(token.tokens) : token.text;
191
- while (i + 1 < l && tokens[i + 1].type === 'text') {
192
- token = tokens[++i];
193
- body += '\n' + (token.tokens ? this.parseInline(token.tokens) : token.text);
194
- }
195
- out += top ? this.renderer.paragraph(body!) : body;
196
- continue;
197
- }
198
-
199
- default: {
200
- const errMsg = 'Token with "' + token.type + '" type was not found.';
201
- if (this.options.silent) {
202
- console.error(errMsg);
203
- return '';
204
- } else {
205
- throw new Error(errMsg);
206
- }
207
- }
208
- }
209
- }
210
-
211
- return out;
212
- }
213
-
214
- /**
215
- * Parse Inline Tokens
216
- */
217
- parseInline(tokens: Token[], renderer?: _Renderer | _TextRenderer): string {
218
- renderer = renderer || this.renderer;
219
- let out = '',
220
- i,
221
- token,
222
- ret;
223
-
224
- const l = tokens.length;
225
- for (i = 0; i < l; i++) {
226
- token = tokens[i];
227
-
228
- // Run any renderer extensions
229
- if (this.options.extensions && this.options.extensions.renderers && this.options.extensions.renderers[token.type]) {
230
- ret = this.options.extensions.renderers[token.type].call({ parser: this }, token);
231
- if (ret !== false || !['escape', 'html', 'link', 'image', 'strong', 'em', 'codespan', 'br', 'del', 'text'].includes(token.type)) {
232
- out += ret || '';
233
- continue;
234
- }
235
- }
236
-
237
- switch (token.type) {
238
- case 'escape': {
239
- out += renderer.text(token.text);
240
- break;
241
- }
242
- case 'html': {
243
- out += renderer.html(token.text);
244
- break;
245
- }
246
- case 'link': {
247
- out += renderer.link(token.href, token.title, this.parseInline(token.tokens, renderer)!);
248
- break;
249
- }
250
- case 'image': {
251
- out += renderer.image(token.href, token.title, token.text);
252
- break;
253
- }
254
- case 'strong': {
255
- out += renderer.strong(this.parseInline(token.tokens, renderer)!);
256
- break;
257
- }
258
- case 'em': {
259
- out += renderer.em(this.parseInline(token.tokens, renderer)!);
260
- break;
261
- }
262
- case 'codespan': {
263
- out += renderer.codespan(token.text);
264
- break;
265
- }
266
- case 'br': {
267
- out += renderer.br();
268
- break;
269
- }
270
- case 'del': {
271
- out += renderer.del(this.parseInline(token.tokens, renderer)!);
272
- break;
273
- }
274
- case 'text': {
275
- out += renderer.text(token.text);
276
- break;
277
- }
278
- default: {
279
- const errMsg = 'Token with "' + token.type + '" type was not found.';
280
- if (this.options.silent) {
281
- console.error(errMsg);
282
- return '';
283
- } else {
284
- throw new Error(errMsg);
285
- }
286
- }
287
- }
288
- }
289
- return out;
290
- }
291
- }
package/src/Renderer.ts DELETED
@@ -1,167 +0,0 @@
1
- import { _defaults } from './defaults.ts';
2
- import {
3
- cleanUrl,
4
- escape
5
- } from './helpers.ts';
6
- import type { MarkedOptions } from './MarkedOptions.ts';
7
- import { Slugger } from './marked.ts';
8
-
9
- /**
10
- * Renderer
11
- */
12
- export class _Renderer {
13
- options: MarkedOptions;
14
- constructor(options?: MarkedOptions) {
15
- this.options = options || _defaults;
16
- }
17
-
18
- code(code: string, infostring: string | undefined, escaped: boolean): string {
19
- const lang = (infostring || '').match(/\S*/)![0];
20
- if (this.options.highlight) {
21
- const out = this.options.highlight(code, lang);
22
- if (out != null && out !== code) {
23
- escaped = true;
24
- code = out;
25
- }
26
- }
27
-
28
- code = code.replace(/\n$/, '') + '\n';
29
-
30
- if (!lang) {
31
- return '<pre><code>'
32
- + (escaped ? code : escape(code, true))
33
- + '</code></pre>\n';
34
- }
35
-
36
- return '<pre><code class="'
37
- + this.options.langPrefix
38
- + escape(lang)
39
- + '">'
40
- + (escaped ? code : escape(code, true))
41
- + '</code></pre>\n';
42
- }
43
-
44
- blockquote(quote: string): string {
45
- return `<blockquote>\n${quote}</blockquote>\n`;
46
- }
47
-
48
- html(html: string, block?: boolean) : string {
49
- return html;
50
- }
51
-
52
- heading(text: string, level: number, raw: string, slugger: Slugger): string {
53
- if (this.options.headerIds) {
54
- const id = this.options.headerPrefix + slugger.slug(raw);
55
- return `<h${level} id="${id}">${text}</h${level}>\n`;
56
- }
57
-
58
- // ignore IDs
59
- return `<h${level}>${text}</h${level}>\n`;
60
- }
61
-
62
- hr(): string {
63
- return this.options.xhtml ? '<hr/>\n' : '<hr>\n';
64
- }
65
-
66
- list(body: string, ordered: boolean, start: number | ''): string {
67
- const type = ordered ? 'ol' : 'ul',
68
- startatt = (ordered && start !== 1) ? (' start="' + start + '"') : '';
69
- return '<' + type + startatt + '>\n' + body + '</' + type + '>\n';
70
- }
71
-
72
- listitem(text: string, task: boolean, checked: boolean): string {
73
- return `<li>${text}</li>\n`;
74
- }
75
-
76
- checkbox(checked: boolean): string {
77
- return '<input '
78
- + (checked ? 'checked="" ' : '')
79
- + 'disabled="" type="checkbox"'
80
- + (this.options.xhtml ? ' /' : '')
81
- + '> ';
82
- }
83
-
84
- paragraph(text: string): string {
85
- return `<p>${text}</p>\n`;
86
- }
87
-
88
- table(header: string, body: string): string {
89
- if (body) body = `<tbody>${body}</tbody>`;
90
-
91
- return '<table>\n'
92
- + '<thead>\n'
93
- + header
94
- + '</thead>\n'
95
- + body
96
- + '</table>\n';
97
- }
98
-
99
- tablerow(content: string): string {
100
- return `<tr>\n${content}</tr>\n`;
101
- }
102
-
103
- tablecell(content: string, flags: {
104
- header: boolean;
105
- align: 'center' | 'left' | 'right' | null;
106
- }): string {
107
- const type = flags.header ? 'th' : 'td';
108
- const tag = flags.align
109
- ? `<${type} align="${flags.align}">`
110
- : `<${type}>`;
111
- return tag + content + `</${type}>\n`;
112
- }
113
-
114
- /**
115
- * span level renderer
116
- */
117
- strong(text: string): string {
118
- return `<strong>${text}</strong>`;
119
- }
120
-
121
- em(text: string): string {
122
- return `<em>${text}</em>`;
123
- }
124
-
125
- codespan(text: string): string {
126
- return `<code>${text}</code>`;
127
- }
128
-
129
- br(): string {
130
- return this.options.xhtml ? '<br/>' : '<br>';
131
- }
132
-
133
- del(text: string): string {
134
- return `<del>${text}</del>`;
135
- }
136
-
137
- link(href: string, title: string | null | undefined, text: string): string {
138
- href = cleanUrl(this.options.sanitize, this.options.baseUrl, href) as any;
139
- if (href === null) {
140
- return text;
141
- }
142
- let out = '<a href="' + href + '"';
143
- if (title) {
144
- out += ' title="' + title + '"';
145
- }
146
- out += '>' + text + '</a>';
147
- return out;
148
- }
149
-
150
- image(href: string, title: string | null, text: string): string {
151
- href = cleanUrl(this.options.sanitize, this.options.baseUrl, href) as any;
152
- if (href === null) {
153
- return text;
154
- }
155
-
156
- let out = `<img src="${href}" alt="${text}"`;
157
- if (title) {
158
- out += ` title="${title}"`;
159
- }
160
- out += this.options.xhtml ? '/>' : '>';
161
- return out;
162
- }
163
-
164
- text(text: string) : string {
165
- return text;
166
- }
167
- }