1
1
import { emojify } from "emoji" ;
2
- import * as Marked from "marked" ;
3
2
import GitHubSlugger from "github-slugger" ;
3
+ import he from "he" ;
4
+ import katex from "katex" ;
5
+ import * as Marked from "marked" ;
4
6
import markedAlert from "marked-alert" ;
5
7
import markedFootnote from "marked-footnote" ;
6
8
import { gfmHeadingId } from "marked-gfm-heading-id" ;
7
9
import Prism from "prismjs" ;
8
10
import sanitizeHtml from "sanitize-html" ;
9
- import he from "he" ;
10
- import katex from "katex" ;
11
11
12
+ import "https://esm.sh/[email protected] /components/prism-yaml" ;
12
13
import { CSS , KATEX_CLASSES , KATEX_CSS } from "./style.ts" ;
13
14
export { CSS , KATEX_CSS , Marked } ;
14
- import "https://esm.sh/[email protected] /components/prism-yaml" ;
15
15
16
16
Marked . marked . use ( markedAlert ( ) ) ;
17
17
Marked . marked . use ( gfmHeadingId ( ) ) ;
@@ -39,11 +39,7 @@ export class Renderer extends Marked.Renderer {
39
39
this . #slugger = new GitHubSlugger ( ) ;
40
40
}
41
41
42
- heading (
43
- text : string ,
44
- level : 1 | 2 | 3 | 4 | 5 | 6 ,
45
- raw : string ,
46
- ) : string {
42
+ heading ( text : string , level : 1 | 2 | 3 | 4 | 5 | 6 , raw : string ) : string {
47
43
const slug = this . #slugger. slug ( raw ) ;
48
44
return `<h${ level } id="${ slug } "><a class="anchor" aria-hidden="true" tabindex="-1" href="#${ slug } "><svg class="octicon octicon-link" viewBox="0 0 16 16" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a>${ text } </h${ level } >\n` ;
49
45
}
@@ -163,10 +159,11 @@ export function render(markdown: string, opts: RenderOptions = {}): string {
163
159
164
160
const marked_opts = getOpts ( opts ) ;
165
161
166
- const html =
167
- ( opts . inline
162
+ const html = (
163
+ opts . inline
168
164
? Marked . marked . parseInline ( markdown , marked_opts )
169
- : Marked . marked . parse ( markdown , marked_opts ) ) as string ;
165
+ : Marked . marked . parse ( markdown , marked_opts )
166
+ ) as string ;
170
167
171
168
if ( opts . disableHtmlSanitization ) {
172
169
return html ;
@@ -241,6 +238,7 @@ export function render(markdown: string, opts: RenderOptions = {}): string {
241
238
"notranslate" ,
242
239
"markdown-alert" ,
243
240
"markdown-alert-*" ,
241
+ "markdown-code-title" ,
244
242
] ,
245
243
span : [
246
244
"token" ,
@@ -322,18 +320,22 @@ export function render(markdown: string, opts: RenderOptions = {}): string {
322
320
annotation : [ "encoding" ] , // Only enabled when math is enabled
323
321
details : [ "open" ] ,
324
322
section : [ "data-footnotes" ] ,
325
- input : [ "checked" , "disabled" , {
326
- name : "type" ,
327
- values : [ "checkbox" ] ,
328
- } ] ,
323
+ input : [
324
+ "checked" ,
325
+ "disabled" ,
326
+ {
327
+ name : "type" ,
328
+ values : [ "checkbox" ] ,
329
+ } ,
330
+ ] ,
329
331
} ;
330
332
331
333
return sanitizeHtml ( html , {
332
334
transformTags : {
333
335
img : transformMedia ,
334
336
video : transformMedia ,
335
337
} ,
336
- allowedTags : [ ...defaultAllowedTags , ...opts . allowedTags ?? [ ] ] ,
338
+ allowedTags : [ ...defaultAllowedTags , ...( opts . allowedTags ?? [ ] ) ] ,
337
339
allowedAttributes : mergeAttributes (
338
340
defaultAllowedAttributes ,
339
341
opts . allowedAttributes ?? { } ,
@@ -366,14 +368,12 @@ function stripTokens(
366
368
367
369
for ( const token of tokens ) {
368
370
if ( token . type === "heading" ) {
369
- sections [ index ] . header = sections [ index ] . header . trim ( ) . replace (
370
- / \n { 3 , } / g,
371
- "\n" ,
372
- ) ;
373
- sections [ index ] . content = sections [ index ] . content . trim ( ) . replace (
374
- / \n { 3 , } / g,
375
- "\n" ,
376
- ) ;
371
+ sections [ index ] . header = sections [ index ] . header
372
+ . trim ( )
373
+ . replace ( / \n { 3 , } / g, "\n" ) ;
374
+ sections [ index ] . content = sections [ index ] . content
375
+ . trim ( )
376
+ . replace ( / \n { 3 , } / g, "\n" ) ;
377
377
378
378
sections . push ( { header : "" , depth : token . depth , content : "" } ) ;
379
379
index += 1 ;
@@ -498,20 +498,21 @@ export function stripSplitBySections(
498
498
markdown : string ,
499
499
opts : RenderOptions = { } ,
500
500
) : MarkdownSections [ ] {
501
- markdown = emojify ( markdown ) . replace ( BLOCK_MATH_REGEXP , "" ) . replace (
502
- INLINE_MATH_REGEXP ,
503
- "" ,
504
- ) ;
501
+ markdown = emojify ( markdown )
502
+ . replace ( BLOCK_MATH_REGEXP , "" )
503
+ . replace ( INLINE_MATH_REGEXP , "" ) ;
505
504
const tokens = Marked . marked . lexer ( markdown , {
506
505
...getOpts ( opts ) ,
507
506
tokenizer : new StripTokenizer ( ) ,
508
507
} ) ;
509
508
510
- const sections : MarkdownSections [ ] = [ {
511
- header : "" ,
512
- depth : 0 ,
513
- content : "" ,
514
- } ] ;
509
+ const sections : MarkdownSections [ ] = [
510
+ {
511
+ header : "" ,
512
+ depth : 0 ,
513
+ content : "" ,
514
+ } ,
515
+ ] ;
515
516
stripTokens ( tokens , sections , false ) ;
516
517
517
518
return sections ;
@@ -521,7 +522,11 @@ export function stripSplitBySections(
521
522
* Strip all markdown syntax to get a plaintext output
522
523
*/
523
524
export function strip ( markdown : string , opts : RenderOptions = { } ) : string {
524
- return stripSplitBySections ( markdown , opts ) . map ( ( section ) =>
525
- section . header + "\n\n" + section . content
526
- ) . join ( "\n\n" ) . trim ( ) . replace ( / \n { 3 , } / g, "\n" ) + "\n" ;
525
+ return (
526
+ stripSplitBySections ( markdown , opts )
527
+ . map ( ( section ) => section . header + "\n\n" + section . content )
528
+ . join ( "\n\n" )
529
+ . trim ( )
530
+ . replace ( / \n { 3 , } / g, "\n" ) + "\n"
531
+ ) ;
527
532
}
0 commit comments