Skip to content

Commit e64f226

Browse files
authoredAug 7, 2024··
fix: Remove useNewRenderer (#3342)
BREAKING CHANGE: Remove old renderer
1 parent b5a5004 commit e64f226

File tree

3 files changed

+3
-266
lines changed

3 files changed

+3
-266
lines changed
 

‎docs/USING_PRO.md

-4
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ Marked provides methods for directly overriding the `renderer` and `tokenizer` f
5656

5757
<h2 id="renderer">The Renderer : <code>renderer</code></h2>
5858

59-
<!-- TODO: Remove this after next major version -->
60-
61-
🚨 Marked v13 changed the renderer to accept tokens. To opt in to these new renderer functions add `useNewRenderer: true` to the extension. See [the v13 release notes](https://github.com/markedjs/marked/releases/tag/v13.0.0) for an example 🚨
62-
6359
The renderer defines the HTML output of a given token. If you supply a `renderer` in the options object passed to `marked.use()`, any functions in the object will override the default handling of that token type.
6460

6561
Calling `marked.use()` to override the same function multiple times will give priority to the version that was assigned *last*. Overriding functions can return `false` to fall back to the previous override in the sequence, or resume default behavior if all overrides return `false`. Returning any other value (including nothing) will prevent fallback behavior.

‎src/Instance.ts

+2-255
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import { _Hooks } from './Hooks.ts';
55
import { _Renderer } from './Renderer.ts';
66
import { _Tokenizer } from './Tokenizer.ts';
77
import { _TextRenderer } from './TextRenderer.ts';
8-
import {
9-
escape,
10-
unescape,
11-
} from './helpers.ts';
8+
import { escape } from './helpers.ts';
129
import type { MarkedExtension, MarkedOptions } from './MarkedOptions.ts';
1310
import type { Token, Tokens, TokensList } from './Tokens.ts';
1411

@@ -152,11 +149,7 @@ export class Marked {
152149
continue;
153150
}
154151
const rendererProp = prop as Exclude<keyof _Renderer, 'options' | 'parser'>;
155-
let rendererFunc = pack.renderer[rendererProp] as GenericRendererFunction;
156-
if (!pack.useNewRenderer) {
157-
// TODO: Remove this in next major version
158-
rendererFunc = this.#convertRendererFunction(rendererFunc, rendererProp, renderer) as GenericRendererFunction;
159-
}
152+
const rendererFunc = pack.renderer[rendererProp] as GenericRendererFunction;
160153
const prevRenderer = renderer[rendererProp] as GenericRendererFunction;
161154
// Replace renderer with func to run extension, but fall back if false
162155
renderer[rendererProp] = (...args: unknown[]) => {
@@ -255,252 +248,6 @@ export class Marked {
255248
return this;
256249
}
257250

258-
// TODO: Remove this in next major release
259-
#convertRendererFunction(func: GenericRendererFunction, prop: string, renderer: _Renderer) {
260-
switch (prop) {
261-
case 'heading':
262-
return function(this: _Renderer, token: Tokens.Heading) {
263-
if (!token.type || token.type !== prop) {
264-
// @ts-ignore
265-
// eslint-disable-next-line prefer-rest-params
266-
return func.apply(this, arguments);
267-
}
268-
269-
return func.call(
270-
this,
271-
renderer.parser.parseInline(token.tokens),
272-
token.depth,
273-
unescape(renderer.parser.parseInline(token.tokens, renderer.parser.textRenderer)),
274-
);
275-
};
276-
case 'code':
277-
return function(this: _Renderer, token: Tokens.Code) {
278-
if (!token.type || token.type !== prop) {
279-
// @ts-ignore
280-
// eslint-disable-next-line prefer-rest-params
281-
return func.apply(this, arguments);
282-
}
283-
284-
return func.call(
285-
this,
286-
token.text,
287-
token.lang,
288-
!!token.escaped,
289-
);
290-
};
291-
case 'table':
292-
return function(this: _Renderer, token: Tokens.Table) {
293-
if (!token.type || token.type !== prop) {
294-
// @ts-ignore
295-
// eslint-disable-next-line prefer-rest-params
296-
return func.apply(this, arguments);
297-
}
298-
299-
let header = '';
300-
// header
301-
let cell = '';
302-
for (let j = 0; j < token.header.length; j++) {
303-
cell += this.tablecell(
304-
{
305-
text: token.header[j].text,
306-
tokens: token.header[j].tokens,
307-
header: true,
308-
align: token.align[j],
309-
},
310-
);
311-
}
312-
header += this.tablerow({ text: cell });
313-
314-
let body = '';
315-
for (let j = 0; j < token.rows.length; j++) {
316-
const row = token.rows[j];
317-
318-
cell = '';
319-
for (let k = 0; k < row.length; k++) {
320-
cell += this.tablecell(
321-
{
322-
text: row[k].text,
323-
tokens: row[k].tokens,
324-
header: false,
325-
align: token.align[k],
326-
},
327-
);
328-
}
329-
330-
body += this.tablerow({ text: cell });
331-
}
332-
333-
return func.call(this, header, body);
334-
};
335-
case 'blockquote':
336-
return function(this: _Renderer, token: Tokens.Blockquote) {
337-
if (!token.type || token.type !== prop) {
338-
// @ts-ignore
339-
// eslint-disable-next-line prefer-rest-params
340-
return func.apply(this, arguments);
341-
}
342-
343-
const body = this.parser.parse(token.tokens);
344-
return func.call(this, body);
345-
};
346-
case 'list':
347-
return function(this: _Renderer, token: Tokens.List) {
348-
if (!token.type || token.type !== prop) {
349-
// @ts-ignore
350-
// eslint-disable-next-line prefer-rest-params
351-
return func.apply(this, arguments);
352-
}
353-
354-
const ordered = token.ordered;
355-
const start = token.start;
356-
const loose = token.loose;
357-
358-
let body = '';
359-
for (let j = 0; j < token.items.length; j++) {
360-
const item = token.items[j];
361-
const checked = item.checked;
362-
const task = item.task;
363-
364-
let itemBody = '';
365-
if (item.task) {
366-
const checkbox = this.checkbox({ checked: !!checked });
367-
if (loose) {
368-
if (item.tokens.length > 0 && item.tokens[0].type === 'paragraph') {
369-
item.tokens[0].text = checkbox + ' ' + item.tokens[0].text;
370-
if (item.tokens[0].tokens && item.tokens[0].tokens.length > 0 && item.tokens[0].tokens[0].type === 'text') {
371-
item.tokens[0].tokens[0].text = checkbox + ' ' + item.tokens[0].tokens[0].text;
372-
}
373-
} else {
374-
item.tokens.unshift({
375-
type: 'text',
376-
text: checkbox + ' ',
377-
} as Tokens.Text);
378-
}
379-
} else {
380-
itemBody += checkbox + ' ';
381-
}
382-
}
383-
384-
itemBody += this.parser.parse(item.tokens, loose);
385-
body += this.listitem({
386-
type: 'list_item',
387-
raw: itemBody,
388-
text: itemBody,
389-
task,
390-
checked: !!checked,
391-
loose,
392-
tokens: item.tokens,
393-
});
394-
}
395-
396-
return func.call(this, body, ordered, start);
397-
};
398-
case 'html':
399-
return function(this: _Renderer, token: Tokens.HTML) {
400-
if (!token.type || token.type !== prop) {
401-
// @ts-ignore
402-
// eslint-disable-next-line prefer-rest-params
403-
return func.apply(this, arguments);
404-
}
405-
406-
return func.call(this, token.text, token.block);
407-
};
408-
case 'paragraph':
409-
return function(this: _Renderer, token: Tokens.Paragraph) {
410-
if (!token.type || token.type !== prop) {
411-
// @ts-ignore
412-
// eslint-disable-next-line prefer-rest-params
413-
return func.apply(this, arguments);
414-
}
415-
416-
return func.call(this, this.parser.parseInline(token.tokens));
417-
};
418-
case 'escape':
419-
return function(this: _Renderer, token: Tokens.Escape) {
420-
if (!token.type || token.type !== prop) {
421-
// @ts-ignore
422-
// eslint-disable-next-line prefer-rest-params
423-
return func.apply(this, arguments);
424-
}
425-
426-
return func.call(this, token.text);
427-
};
428-
case 'link':
429-
return function(this: _Renderer, token: Tokens.Link) {
430-
if (!token.type || token.type !== prop) {
431-
// @ts-ignore
432-
// eslint-disable-next-line prefer-rest-params
433-
return func.apply(this, arguments);
434-
}
435-
436-
return func.call(this, token.href, token.title, this.parser.parseInline(token.tokens));
437-
};
438-
case 'image':
439-
return function(this: _Renderer, token: Tokens.Image) {
440-
if (!token.type || token.type !== prop) {
441-
// @ts-ignore
442-
// eslint-disable-next-line prefer-rest-params
443-
return func.apply(this, arguments);
444-
}
445-
446-
return func.call(this, token.href, token.title, token.text);
447-
};
448-
case 'strong':
449-
return function(this: _Renderer, token: Tokens.Strong) {
450-
if (!token.type || token.type !== prop) {
451-
// @ts-ignore
452-
// eslint-disable-next-line prefer-rest-params
453-
return func.apply(this, arguments);
454-
}
455-
456-
return func.call(this, this.parser.parseInline(token.tokens));
457-
};
458-
case 'em':
459-
return function(this: _Renderer, token: Tokens.Em) {
460-
if (!token.type || token.type !== prop) {
461-
// @ts-ignore
462-
// eslint-disable-next-line prefer-rest-params
463-
return func.apply(this, arguments);
464-
}
465-
466-
return func.call(this, this.parser.parseInline(token.tokens));
467-
};
468-
case 'codespan':
469-
return function(this: _Renderer, token: Tokens.Codespan) {
470-
if (!token.type || token.type !== prop) {
471-
// @ts-ignore
472-
// eslint-disable-next-line prefer-rest-params
473-
return func.apply(this, arguments);
474-
}
475-
476-
return func.call(this, token.text);
477-
};
478-
case 'del':
479-
return function(this: _Renderer, token: Tokens.Del) {
480-
if (!token.type || token.type !== prop) {
481-
// @ts-ignore
482-
// eslint-disable-next-line prefer-rest-params
483-
return func.apply(this, arguments);
484-
}
485-
486-
return func.call(this, this.parser.parseInline(token.tokens));
487-
};
488-
case 'text':
489-
return function(this: _Renderer, token: Tokens.Text) {
490-
if (!token.type || token.type !== prop) {
491-
// @ts-ignore
492-
// eslint-disable-next-line prefer-rest-params
493-
return func.apply(this, arguments);
494-
}
495-
496-
return func.call(this, token.text);
497-
};
498-
default:
499-
// do nothing
500-
}
501-
return func;
502-
}
503-
504251
setOptions(opt: MarkedOptions) {
505252
this.defaults = { ...this.defaults, ...opt };
506253
return this;

‎src/MarkedOptions.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,9 @@ export interface MarkedExtension {
109109
* The return value of the function is ignored.
110110
*/
111111
walkTokens?: ((token: Token) => void | Promise<void>) | undefined | null;
112-
113-
/**
114-
* Use the new renderer that accepts an object instead of individual parameters.
115-
* This option will be removed and default to true in the next major version.
116-
*/
117-
useNewRenderer?: boolean | undefined;
118112
}
119113

120-
export interface MarkedOptions extends Omit<MarkedExtension, 'useNewRenderer' | 'hooks' | 'renderer' | 'tokenizer' | 'extensions' | 'walkTokens'> {
114+
export interface MarkedOptions extends Omit<MarkedExtension, 'hooks' | 'renderer' | 'tokenizer' | 'extensions' | 'walkTokens'> {
121115
/**
122116
* Hooks are methods that hook into some part of marked.
123117
*/

0 commit comments

Comments
 (0)
Please sign in to comment.