@@ -2,7 +2,7 @@ import { css, html, LitElement } from 'lit';
2
2
import { customElement , property } from 'lit/decorators.js' ;
3
3
import { unsafeHTML } from 'lit/directives/unsafe-html.js' ;
4
4
import { until } from 'lit/directives/until.js' ;
5
- import type { RendererObject } from 'marked' ;
5
+ import type { RendererObject , RendererThis , Tokens } from 'marked' ;
6
6
import { marked } from 'marked' ;
7
7
import type { ThemeIcon } from 'vscode' ;
8
8
@@ -116,34 +116,7 @@ export class GLMarkdown extends LitElement {
116
116
117
117
function getMarkdownRenderer ( ) : RendererObject {
118
118
return {
119
- // heading: function (text: string, level: number, raw: string, slugger: any): string {
120
- // level = Math.min(6, level);
121
- // const id = slugger.slug(text);
122
- // const hlinks = null;
123
-
124
- // let content;
125
-
126
- // if (hlinks === null) {
127
- // // No heading links
128
- // content = text;
129
- // } else {
130
- // content = `<a href="#${id}" class="anchor">`;
131
-
132
- // if (hlinks === '') {
133
- // // Heading content is the link
134
- // content += `${text}</a>`;
135
- // } else {
136
- // // Headings are prepended with a linked symbol
137
- // content += `${hlinks}</a>${text}`;
138
- // }
139
- // }
140
-
141
- // return `
142
- // <h${level} id="${id}">
143
- // ${content}
144
- // </h${level}>`;
145
- // },
146
- image : ( href : string | null , title : string | null , text : string ) : string => {
119
+ image : function ( this : RendererThis , { href, title, text } : Tokens . Image ) : string {
147
120
let dimensions : string [ ] = [ ] ;
148
121
let attributes : string [ ] = [ ] ;
149
122
if ( href ) {
@@ -161,27 +134,24 @@ function getMarkdownRenderer(): RendererObject {
161
134
}
162
135
return `<img ${ attributes . join ( ' ' ) } >` ;
163
136
} ,
164
-
165
- paragraph : ( text : string ) : string => {
137
+ paragraph : function ( this : RendererThis , { tokens } : Tokens . Paragraph ) : string {
138
+ const text = this . parser . parseInline ( tokens ) ;
166
139
return `<p>${ text } </p>` ;
167
140
} ,
168
-
169
- link : ( href : string , title : string | null | undefined , text : string ) : string | false => {
170
- if ( typeof href !== 'string' ) {
171
- return '' ;
172
- }
141
+ link : function ( this : RendererThis , { href, title, tokens } : Tokens . Link ) : string | false {
142
+ if ( typeof href !== 'string' ) return '' ;
173
143
174
144
// Remove markdown escapes. Workaround for https://github.com/chjj/marked/issues/829
145
+ let text = this . parser . parseInline ( tokens ) ;
175
146
if ( href === text ) {
176
147
// raw link case
177
148
text = removeMarkdownEscapes ( text ) ;
178
149
}
179
150
180
151
title = typeof title === 'string' ? escapeDoubleQuotes ( removeMarkdownEscapes ( title ) ) : '' ;
181
- href = removeMarkdownEscapes ( href ) ;
182
152
183
153
// HTML Encode href
184
- href = href
154
+ href = removeMarkdownEscapes ( href )
185
155
. replace ( / & / g, '&' )
186
156
. replace ( / < / g, '<' )
187
157
. replace ( / > / g, '>' )
@@ -190,17 +160,15 @@ function getMarkdownRenderer(): RendererObject {
190
160
191
161
return `<a href="${ href } " title="${ title || href } " draggable="false">${ text } </a>` ;
192
162
} ,
193
-
194
- code : function ( code : string , infostring : string | undefined , _escaped : boolean ) : string {
163
+ code : function ( this : RendererThis , { text, lang } : Tokens . Code ) : string {
195
164
// Remote code may include characters that need to be escaped to be visible in HTML
196
- code = code . replace ( / < / g, '<' ) ;
197
- return `<pre class="language-${ infostring } "><code>${ code } </code></pre>` ;
165
+ text = text . replace ( / < / g, '<' ) ;
166
+ return `<pre class="language-${ lang } "><code>${ text } </code></pre>` ;
198
167
} ,
199
-
200
- codespan : function ( code : string ) : string {
168
+ codespan : function ( this : RendererThis , { text } : Tokens . Codespan ) : string {
201
169
// Remote code may include characters that need to be escaped to be visible in HTML
202
- code = code . replace ( / < / g, '<' ) ;
203
- return `<code>${ code } </code>` ;
170
+ text = text . replace ( / < / g, '<' ) ;
171
+ return `<code>${ text } </code>` ;
204
172
} ,
205
173
} ;
206
174
}
0 commit comments