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.
package/src/Tokens.ts DELETED
@@ -1,199 +0,0 @@
1
- /* eslint-disable no-use-before-define */
2
- export type Token = (Tokens.Space
3
- | Tokens.Code
4
- | Tokens.Heading
5
- | Tokens.Table
6
- | Tokens.Hr
7
- | Tokens.Blockquote
8
- | Tokens.List
9
- | Tokens.ListItem
10
- | Tokens.Paragraph
11
- | Tokens.HTML
12
- | Tokens.Text
13
- | Tokens.Def
14
- | Tokens.Escape
15
- | Tokens.Tag
16
- | Tokens.Image
17
- | Tokens.Link
18
- | Tokens.Strong
19
- | Tokens.Em
20
- | Tokens.Codespan
21
- | Tokens.Br
22
- | Tokens.Del) & { loose?: boolean, tokens?: Token[] };
23
-
24
- export namespace Tokens {
25
- export interface Space {
26
- type: 'space';
27
- raw: string;
28
- }
29
-
30
- export interface Code {
31
- type: 'code';
32
- raw: string;
33
- codeBlockStyle?: 'indented' | undefined;
34
- lang?: string | undefined;
35
- text: string;
36
- escaped?: boolean;
37
- }
38
-
39
- export interface Heading {
40
- type: 'heading';
41
- raw: string;
42
- depth: number;
43
- text: string;
44
- tokens: Token[];
45
- }
46
-
47
- export interface Table {
48
- type: 'table';
49
- raw?: string;
50
- align: Array<'center' | 'left' | 'right' | null>;
51
- header: TableCell[];
52
- rows: TableCell[][];
53
- }
54
-
55
- export interface TableCell {
56
- text: string;
57
- tokens?: Token[];
58
- }
59
-
60
- export interface Hr {
61
- type: 'hr';
62
- raw: string;
63
- }
64
-
65
- export interface Blockquote {
66
- type: 'blockquote';
67
- raw: string;
68
- text: string;
69
- tokens: Token[];
70
- }
71
-
72
- export interface List {
73
- type: 'list';
74
- raw: string;
75
- ordered: boolean;
76
- start: number | '';
77
- loose: boolean;
78
- items: ListItem[];
79
- }
80
-
81
- export interface ListItem {
82
- type: 'list_item';
83
- raw: string;
84
- task: boolean;
85
- checked?: boolean | undefined;
86
- loose: boolean;
87
- text: string;
88
- tokens?: Token[];
89
- }
90
-
91
- export interface Paragraph {
92
- type: 'paragraph';
93
- raw: string;
94
- pre?: boolean | undefined;
95
- text: string;
96
- tokens: Token[];
97
- }
98
-
99
- export interface HTML {
100
- type: 'html';
101
- raw: string;
102
- pre: boolean;
103
- text: string;
104
- block: boolean;
105
- }
106
-
107
- export interface Text {
108
- type: 'text';
109
- raw: string;
110
- text: string;
111
- tokens?: Token[];
112
- }
113
-
114
- export interface Def {
115
- type: 'def';
116
- raw: string;
117
- tag: string;
118
- href: string;
119
- title: string;
120
- }
121
-
122
- export interface Escape {
123
- type: 'escape';
124
- raw: string;
125
- text: string;
126
- }
127
-
128
- export interface Tag {
129
- type: 'text' | 'html';
130
- raw: string;
131
- inLink: boolean;
132
- inRawBlock: boolean;
133
- text: string;
134
- block: boolean;
135
- }
136
-
137
- export interface Link {
138
- type: 'link';
139
- raw: string;
140
- href: string;
141
- title?: string | null;
142
- text: string;
143
- tokens: Token[];
144
- }
145
-
146
- export interface Image {
147
- type: 'image';
148
- raw: string;
149
- href: string;
150
- title: string | null;
151
- text: string;
152
- }
153
-
154
- export interface Strong {
155
- type: 'strong';
156
- raw: string;
157
- text: string;
158
- tokens: Token[];
159
- }
160
-
161
- export interface Em {
162
- type: 'em';
163
- raw: string;
164
- text: string;
165
- tokens: Token[];
166
- }
167
-
168
- export interface Codespan {
169
- type: 'codespan';
170
- raw: string;
171
- text: string;
172
- }
173
-
174
- export interface Br {
175
- type: 'br';
176
- raw: string;
177
- }
178
-
179
- export interface Del {
180
- type: 'del';
181
- raw: string;
182
- text: string;
183
- tokens: Token[];
184
- }
185
-
186
- export interface Generic {
187
- [index: string]: any;
188
-
189
- type: string;
190
- raw: string;
191
- tokens?: Token[] | undefined;
192
- }
193
- }
194
-
195
- export type Links = Record<string, Pick<Tokens.Link | Tokens.Image, 'href' | 'title'>>;
196
-
197
- export type TokensList = Token[] & {
198
- links: Links;
199
- };
package/src/defaults.ts DELETED
@@ -1,35 +0,0 @@
1
- import type { MarkedOptions } from './MarkedOptions.ts';
2
-
3
- /**
4
- * Gets the original marked default options.
5
- */
6
- export function _getDefaults(): MarkedOptions {
7
- return {
8
- async: false,
9
- baseUrl: null,
10
- breaks: false,
11
- extensions: null,
12
- gfm: true,
13
- headerIds: false,
14
- headerPrefix: '',
15
- highlight: null,
16
- hooks: null,
17
- langPrefix: 'language-',
18
- mangle: false,
19
- pedantic: false,
20
- renderer: null,
21
- sanitize: false,
22
- sanitizer: null,
23
- silent: false,
24
- smartypants: false,
25
- tokenizer: null,
26
- walkTokens: null,
27
- xhtml: false
28
- };
29
- }
30
-
31
- export let _defaults = _getDefaults();
32
-
33
- export function changeDefaults(newDefaults: MarkedOptions) {
34
- _defaults = newDefaults;
35
- }
package/src/helpers.ts DELETED
@@ -1,264 +0,0 @@
1
- import type { MarkedOptions } from './MarkedOptions.ts';
2
- import type { ResultCallback } from './Instance.ts';
3
- import type { Rule } from './rules.ts';
4
-
5
- /**
6
- * Helpers
7
- */
8
- const escapeTest = /[&<>"']/;
9
- const escapeReplace = new RegExp(escapeTest.source, 'g');
10
- const escapeTestNoEncode = /[<>"']|&(?!(#\d{1,7}|#[Xx][a-fA-F0-9]{1,6}|\w+);)/;
11
- const escapeReplaceNoEncode = new RegExp(escapeTestNoEncode.source, 'g');
12
- const escapeReplacements = {
13
- '&': '&amp;',
14
- '<': '&lt;',
15
- '>': '&gt;',
16
- '"': '&quot;',
17
- "'": '&#39;'
18
- };
19
- const getEscapeReplacement = (ch: string) => escapeReplacements[ch];
20
-
21
- export function escape(html: string, encode?: boolean) {
22
- if (encode) {
23
- if (escapeTest.test(html)) {
24
- return html.replace(escapeReplace, getEscapeReplacement);
25
- }
26
- } else {
27
- if (escapeTestNoEncode.test(html)) {
28
- return html.replace(escapeReplaceNoEncode, getEscapeReplacement);
29
- }
30
- }
31
-
32
- return html;
33
- }
34
-
35
- const unescapeTest = /&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/ig;
36
-
37
- export function unescape(html: string) {
38
- // explicitly match decimal, hex, and named HTML entities
39
- return html.replace(unescapeTest, (_, n) => {
40
- n = n.toLowerCase();
41
- if (n === 'colon') return ':';
42
- if (n.charAt(0) === '#') {
43
- return n.charAt(1) === 'x'
44
- ? String.fromCharCode(parseInt(n.substring(2), 16))
45
- : String.fromCharCode(+n.substring(1));
46
- }
47
- return '';
48
- });
49
- }
50
-
51
- const caret = /(^|[^\[])\^/g;
52
-
53
- export function edit(regex: Rule, opt?: string) {
54
- regex = typeof regex === 'string' ? regex : regex.source;
55
- opt = opt || '';
56
- const obj = {
57
- replace: (name: string | RegExp, val: string | RegExp) => {
58
- val = typeof val === 'object' && 'source' in val ? val.source : val;
59
- val = val.replace(caret, '$1');
60
- regex = (regex as string).replace(name, val);
61
- return obj;
62
- },
63
- getRegex: () => {
64
- return new RegExp(regex, opt);
65
- }
66
- };
67
- return obj;
68
- }
69
-
70
- const nonWordAndColonTest = /[^\w:]/g;
71
- const originIndependentUrl = /^$|^[a-z][a-z0-9+.-]*:|^[?#]/i;
72
-
73
- export function cleanUrl(sanitize: boolean | undefined, base: string | undefined | null, href: string) {
74
- if (sanitize) {
75
- let prot;
76
- try {
77
- prot = decodeURIComponent(unescape(href))
78
- .replace(nonWordAndColonTest, '')
79
- .toLowerCase();
80
- } catch (e) {
81
- return null;
82
- }
83
- if (prot.indexOf('javascript:') === 0 || prot.indexOf('vbscript:') === 0 || prot.indexOf('data:') === 0) {
84
- return null;
85
- }
86
- }
87
- if (base && !originIndependentUrl.test(href)) {
88
- href = resolveUrl(base, href);
89
- }
90
- try {
91
- href = encodeURI(href).replace(/%25/g, '%');
92
- } catch (e) {
93
- return null;
94
- }
95
- return href;
96
- }
97
-
98
- const baseUrls: Record<string, string> = {};
99
- const justDomain = /^[^:]+:\/*[^/]*$/;
100
- const protocol = /^([^:]+:)[\s\S]*$/;
101
- const domain = /^([^:]+:\/*[^/]*)[\s\S]*$/;
102
-
103
- export function resolveUrl(base: string, href: string) {
104
- if (!baseUrls[' ' + base]) {
105
- // we can ignore everything in base after the last slash of its path component,
106
- // but we might need to add _that_
107
- // https://tools.ietf.org/html/rfc3986#section-3
108
- if (justDomain.test(base)) {
109
- baseUrls[' ' + base] = base + '/';
110
- } else {
111
- baseUrls[' ' + base] = rtrim(base, '/', true);
112
- }
113
- }
114
- base = baseUrls[' ' + base];
115
- const relativeBase = base.indexOf(':') === -1;
116
-
117
- if (href.substring(0, 2) === '//') {
118
- if (relativeBase) {
119
- return href;
120
- }
121
- return base.replace(protocol, '$1') + href;
122
- } else if (href.charAt(0) === '/') {
123
- if (relativeBase) {
124
- return href;
125
- }
126
- return base.replace(domain, '$1') + href;
127
- } else {
128
- return base + href;
129
- }
130
- }
131
-
132
- export const noopTest = { exec: () => null };
133
-
134
- export function splitCells(tableRow: string, count: number) {
135
- // ensure that every cell-delimiting pipe has a space
136
- // before it to distinguish it from an escaped pipe
137
- const row = tableRow.replace(/\|/g, (match, offset, str) => {
138
- let escaped = false,
139
- curr = offset;
140
- while (--curr >= 0 && str[curr] === '\\') escaped = !escaped;
141
- if (escaped) {
142
- // odd number of slashes means | is escaped
143
- // so we leave it alone
144
- return '|';
145
- } else {
146
- // add space before unescaped |
147
- return ' |';
148
- }
149
- }),
150
- cells = row.split(/ \|/);
151
- let i = 0;
152
-
153
- // First/last cell in a row cannot be empty if it has no leading/trailing pipe
154
- if (!cells[0].trim()) {
155
- cells.shift();
156
- }
157
- if (cells.length > 0 && !cells[cells.length - 1].trim()) {
158
- cells.pop();
159
- }
160
-
161
- if (cells.length > count) {
162
- cells.splice(count);
163
- } else {
164
- while (cells.length < count) cells.push('');
165
- }
166
-
167
- for (; i < cells.length; i++) {
168
- // leading or trailing whitespace is ignored per the gfm spec
169
- cells[i] = cells[i].trim().replace(/\\\|/g, '|');
170
- }
171
- return cells;
172
- }
173
-
174
- /**
175
- * Remove trailing 'c's. Equivalent to str.replace(/c*$/, '').
176
- * /c*$/ is vulnerable to REDOS.
177
- *
178
- * @param str
179
- * @param c
180
- * @param invert Remove suffix of non-c chars instead. Default falsey.
181
- */
182
- export function rtrim(str: string, c: string, invert?: boolean) {
183
- const l = str.length;
184
- if (l === 0) {
185
- return '';
186
- }
187
-
188
- // Length of suffix matching the invert condition.
189
- let suffLen = 0;
190
-
191
- // Step left until we fail to match the invert condition.
192
- while (suffLen < l) {
193
- const currChar = str.charAt(l - suffLen - 1);
194
- if (currChar === c && !invert) {
195
- suffLen++;
196
- } else if (currChar !== c && invert) {
197
- suffLen++;
198
- } else {
199
- break;
200
- }
201
- }
202
-
203
- return str.slice(0, l - suffLen);
204
- }
205
-
206
- export function findClosingBracket(str: string, b: string) {
207
- if (str.indexOf(b[1]) === -1) {
208
- return -1;
209
- }
210
- const l = str.length;
211
- let level = 0,
212
- i = 0;
213
- for (; i < l; i++) {
214
- if (str[i] === '\\') {
215
- i++;
216
- } else if (str[i] === b[0]) {
217
- level++;
218
- } else if (str[i] === b[1]) {
219
- level--;
220
- if (level < 0) {
221
- return i;
222
- }
223
- }
224
- }
225
- return -1;
226
- }
227
-
228
- export function checkDeprecations(opt: MarkedOptions, callback?: ResultCallback) {
229
- if (!opt || opt.silent) {
230
- return;
231
- }
232
-
233
- if (callback) {
234
- console.warn('marked(): callback is deprecated since version 5.0.0, should not be used and will be removed in the future. Read more here: https://marked.js.org/using_pro#async');
235
- }
236
-
237
- if (opt.sanitize || opt.sanitizer) {
238
- console.warn('marked(): sanitize and sanitizer parameters are deprecated since version 0.7.0, should not be used and will be removed in the future. Read more here: https://marked.js.org/#/USING_ADVANCED.md#options');
239
- }
240
-
241
- if (opt.highlight || opt.langPrefix !== 'language-') {
242
- console.warn('marked(): highlight and langPrefix parameters are deprecated since version 5.0.0, should not be used and will be removed in the future. Instead use https://www.npmjs.com/package/marked-highlight.');
243
- }
244
-
245
- if (opt.mangle) {
246
- console.warn('marked(): mangle parameter is enabled by default, but is deprecated since version 5.0.0, and will be removed in the future. To clear this warning, install https://www.npmjs.com/package/marked-mangle, or disable by setting `{mangle: false}`.');
247
- }
248
-
249
- if (opt.baseUrl) {
250
- console.warn('marked(): baseUrl parameter is deprecated since version 5.0.0, should not be used and will be removed in the future. Instead use https://www.npmjs.com/package/marked-base-url.');
251
- }
252
-
253
- if (opt.smartypants) {
254
- console.warn('marked(): smartypants parameter is deprecated since version 5.0.0, should not be used and will be removed in the future. Instead use https://www.npmjs.com/package/marked-smartypants.');
255
- }
256
-
257
- if (opt.xhtml) {
258
- console.warn('marked(): xhtml parameter is deprecated since version 5.0.0, should not be used and will be removed in the future. Instead use https://www.npmjs.com/package/marked-xhtml.');
259
- }
260
-
261
- if (opt.headerIds || opt.headerPrefix) {
262
- console.warn('marked(): headerIds and headerPrefix parameters enabled by default, but are deprecated since version 5.0.0, and will be removed in the future. To clear this warning, install https://www.npmjs.com/package/marked-gfm-heading-id, or disable by setting `{headerIds: false}`.');
263
- }
264
- }
package/src/marked.ts DELETED
@@ -1,143 +0,0 @@
1
- import { _Lexer } from './Lexer.ts';
2
- import { _Parser } from './Parser.ts';
3
- import { _Tokenizer } from './Tokenizer.ts';
4
- import { _Renderer } from './Renderer.ts';
5
- import { _TextRenderer } from './TextRenderer.ts';
6
- import { _Slugger } from './Slugger.ts';
7
- import { _Hooks } from './Hooks.ts';
8
- import { Marked } from './Instance.ts';
9
- import {
10
- _getDefaults,
11
- changeDefaults,
12
- _defaults
13
- } from './defaults.ts';
14
- import type { MarkedExtension, MarkedOptions } from './MarkedOptions.ts';
15
- import type { Token, TokensList } from './Tokens.ts';
16
- import type { ResultCallback } from './Instance.ts';
17
-
18
- const markedInstance = new Marked();
19
-
20
- /**
21
- * Compiles markdown to HTML asynchronously.
22
- *
23
- * @param src String of markdown source to be compiled
24
- * @param options Hash of options, having async: true
25
- * @return Promise of string of compiled HTML
26
- */
27
- export function marked(src: string, options: MarkedOptions & { async: true }): Promise<string>;
28
-
29
- /**
30
- * Compiles markdown to HTML synchronously.
31
- *
32
- * @param src String of markdown source to be compiled
33
- * @param options Optional hash of options
34
- * @return String of compiled HTML
35
- */
36
- export function marked(src: string, options?: MarkedOptions): string;
37
-
38
- /**
39
- * Compiles markdown to HTML asynchronously with a callback.
40
- *
41
- * @param src String of markdown source to be compiled
42
- * @param callback Function called when the markdownString has been fully parsed when using async highlighting
43
- */
44
- export function marked(src: string, callback: ResultCallback): void;
45
-
46
- /**
47
- * Compiles markdown to HTML asynchronously with a callback.
48
- *
49
- * @param src String of markdown source to be compiled
50
- * @param options Hash of options
51
- * @param callback Function called when the markdownString has been fully parsed when using async highlighting
52
- */
53
- export function marked(
54
- src: string,
55
- options: MarkedOptions,
56
- callback: ResultCallback,
57
- ): void;
58
- export function marked(src: string, opt?: MarkedOptions | ResultCallback, callback?: ResultCallback): string | Promise<string | undefined> | undefined {
59
- return markedInstance.parse(src, opt, callback);
60
- }
61
-
62
- /**
63
- * Sets the default options.
64
- *
65
- * @param options Hash of options
66
- */
67
- marked.options =
68
- marked.setOptions = function(options: MarkedOptions) {
69
- markedInstance.setOptions(options);
70
- marked.defaults = markedInstance.defaults;
71
- changeDefaults(marked.defaults);
72
- return marked;
73
- };
74
-
75
- /**
76
- * Gets the original marked default options.
77
- */
78
- marked.getDefaults = _getDefaults;
79
-
80
- marked.defaults = _defaults;
81
-
82
- /**
83
- * Use Extension
84
- */
85
-
86
- marked.use = function(...args: MarkedExtension[]) {
87
- markedInstance.use(...args);
88
- marked.defaults = markedInstance.defaults;
89
- changeDefaults(marked.defaults);
90
- return marked;
91
- };
92
-
93
- /**
94
- * Run callback for every token
95
- */
96
-
97
- marked.walkTokens = function <T = void>(tokens: Token[] | TokensList, callback: (token: Token) => T | T[]) {
98
- return markedInstance.walkTokens(tokens, callback);
99
- };
100
-
101
- /**
102
- * Compiles markdown to HTML without enclosing `p` tag.
103
- *
104
- * @param src String of markdown source to be compiled
105
- * @param options Hash of options
106
- * @return String of compiled HTML
107
- */
108
- marked.parseInline = markedInstance.parseInline;
109
-
110
- /**
111
- * Expose
112
- */
113
- marked.Parser = _Parser;
114
- marked.parser = _Parser.parse;
115
- marked.Renderer = _Renderer;
116
- marked.TextRenderer = _TextRenderer;
117
- marked.Lexer = _Lexer;
118
- marked.lexer = _Lexer.lex;
119
- marked.Tokenizer = _Tokenizer;
120
- marked.Slugger = _Slugger;
121
- marked.Hooks = _Hooks;
122
- marked.parse = marked;
123
-
124
- export const options = marked.options;
125
- export const setOptions = marked.setOptions;
126
- export const use = marked.use;
127
- export const walkTokens = marked.walkTokens;
128
- export const parseInline = marked.parseInline;
129
- export const parse = marked;
130
- export const parser = _Parser.parse;
131
- export const lexer = _Lexer.lex;
132
- export { _defaults as defaults, _getDefaults as getDefaults } from './defaults.ts';
133
- export { _Lexer as Lexer } from './Lexer.ts';
134
- export { _Parser as Parser } from './Parser.ts';
135
- export { _Tokenizer as Tokenizer } from './Tokenizer.ts';
136
- export { _Renderer as Renderer } from './Renderer.ts';
137
- export { _TextRenderer as TextRenderer } from './TextRenderer.ts';
138
- export { _Slugger as Slugger } from './Slugger.ts';
139
- export { _Hooks as Hooks } from './Hooks.ts';
140
- export { Marked } from './Instance.ts';
141
- export type * from './MarkedOptions.ts';
142
- export type * from './rules.ts';
143
- export type * from './Tokens.ts';