@@ -32,6 +32,53 @@ export interface MarkdownRenderOptions extends FormattedTextRenderOptions {
32
32
readonly asyncRenderCallback ?: ( ) => void ;
33
33
}
34
34
35
+ const defaultMarkedRenderers = Object . freeze ( {
36
+ image : ( href : string | null , title : string | null , text : string ) : string => {
37
+ let dimensions : string [ ] = [ ] ;
38
+ let attributes : string [ ] = [ ] ;
39
+ if ( href ) {
40
+ ( { href, dimensions } = parseHrefAndDimensions ( href ) ) ;
41
+ attributes . push ( `src="${ escapeDoubleQuotes ( href ) } "` ) ;
42
+ }
43
+ if ( text ) {
44
+ attributes . push ( `alt="${ escapeDoubleQuotes ( text ) } "` ) ;
45
+ }
46
+ if ( title ) {
47
+ attributes . push ( `title="${ escapeDoubleQuotes ( title ) } "` ) ;
48
+ }
49
+ if ( dimensions . length ) {
50
+ attributes = attributes . concat ( dimensions ) ;
51
+ }
52
+ return '<img ' + attributes . join ( ' ' ) + '>' ;
53
+ } ,
54
+
55
+ paragraph : ( text : string ) : string => {
56
+ return `<p>${ text } </p>` ;
57
+ } ,
58
+
59
+ link : ( href : string | null , title : string | null , text : string ) : string => {
60
+ if ( typeof href !== 'string' ) {
61
+ return '' ;
62
+ }
63
+
64
+ // Remove markdown escapes. Workaround for https://github.com/chjj/marked/issues/829
65
+ if ( href === text ) { // raw link case
66
+ text = removeMarkdownEscapes ( text ) ;
67
+ }
68
+
69
+ title = typeof title === 'string' ? escapeDoubleQuotes ( removeMarkdownEscapes ( title ) ) : '' ;
70
+ href = removeMarkdownEscapes ( href ) ;
71
+
72
+ // HTML Encode href
73
+ href = href . replace ( / & / g, '&' )
74
+ . replace ( / < / g, '<' )
75
+ . replace ( / > / g, '>' )
76
+ . replace ( / " / g, '"' )
77
+ . replace ( / ' / g, ''' ) ;
78
+ return `<a href="${ href } " title="${ title || href } ">${ text } </a>` ;
79
+ } ,
80
+ } ) ;
81
+
35
82
/**
36
83
* Low-level way create a html element from a markdown string.
37
84
*
@@ -93,49 +140,9 @@ export function renderMarkdown(markdown: IMarkdownString, options: MarkdownRende
93
140
} ;
94
141
95
142
const renderer = new marked . Renderer ( ) ;
96
-
97
- renderer . image = ( href : string , title : string , text : string ) => {
98
- let dimensions : string [ ] = [ ] ;
99
- let attributes : string [ ] = [ ] ;
100
- if ( href ) {
101
- ( { href, dimensions } = parseHrefAndDimensions ( href ) ) ;
102
- attributes . push ( `src="${ escapeDoubleQuotes ( href ) } "` ) ;
103
- }
104
- if ( text ) {
105
- attributes . push ( `alt="${ escapeDoubleQuotes ( text ) } "` ) ;
106
- }
107
- if ( title ) {
108
- attributes . push ( `title="${ escapeDoubleQuotes ( title ) } "` ) ;
109
- }
110
- if ( dimensions . length ) {
111
- attributes = attributes . concat ( dimensions ) ;
112
- }
113
- return '<img ' + attributes . join ( ' ' ) + '>' ;
114
- } ;
115
- renderer . link = ( href , title , text ) : string => {
116
- if ( typeof href !== 'string' ) {
117
- return '' ;
118
- }
119
-
120
- // Remove markdown escapes. Workaround for https://github.com/chjj/marked/issues/829
121
- if ( href === text ) { // raw link case
122
- text = removeMarkdownEscapes ( text ) ;
123
- }
124
-
125
- title = typeof title === 'string' ? escapeDoubleQuotes ( removeMarkdownEscapes ( title ) ) : '' ;
126
- href = removeMarkdownEscapes ( href ) ;
127
-
128
- // HTML Encode href
129
- href = href . replace ( / & / g, '&' )
130
- . replace ( / < / g, '<' )
131
- . replace ( / > / g, '>' )
132
- . replace ( / " / g, '"' )
133
- . replace ( / ' / g, ''' ) ;
134
- return `<a href="${ href } " title="${ title || href } ">${ text } </a>` ;
135
- } ;
136
- renderer . paragraph = ( text ) : string => {
137
- return `<p>${ text } </p>` ;
138
- } ;
143
+ renderer . image = defaultMarkedRenderers . image ;
144
+ renderer . link = defaultMarkedRenderers . link ;
145
+ renderer . paragraph = defaultMarkedRenderers . paragraph ;
139
146
140
147
// Will collect [id, renderedElement] tuples
141
148
const codeBlocks : Promise < [ string , HTMLElement ] > [ ] = [ ] ;
0 commit comments