File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -9,7 +9,8 @@ import { $ } from 'dom';
99
1010marked . use ( {
1111 renderer : {
12- link : markdownParsers . link
12+ link : markdownParsers . link ,
13+ codespan : markdownParsers . codespan as any
1314 }
1415} ) ;
1516
Original file line number Diff line number Diff line change @@ -3,3 +3,33 @@ import { Tokens } from 'marked';
33export function link ( token : Tokens . Link ) : string {
44 return `<a href="${ token . href } " ${ token . title ? `title="${ token . title } "` : '' } target="_blank">${ token . text } </a>` ;
55}
6+
7+ function decodeHtmlEntities ( input : string ) : string {
8+ if ( ! input ) {
9+ return '' ;
10+ }
11+ return input
12+ . replace ( / & a m p ; / g, '&' )
13+ . replace ( / & l t ; / g, '<' )
14+ . replace ( / & g t ; / g, '>' )
15+ . replace ( / & q u o t ; / g, '"' )
16+ . replace ( / & # 3 9 ; / g, "'" ) ;
17+ }
18+
19+ function escapeForCode ( input : string ) : string {
20+ if ( ! input ) {
21+ return '' ;
22+ }
23+ // For code content, escape only what is necessary for safe HTML text rendering.
24+ // We intentionally do NOT escape '>' so sequences like `g>g` render literally.
25+ return input
26+ . replace ( / & / g, '&' )
27+ . replace ( / < / g, '<' ) ;
28+ }
29+
30+ export function codespan ( token : Tokens . Codespan ) : string {
31+ const text = token . text ;
32+ const decoded = decodeHtmlEntities ( text ) ;
33+ const escaped = escapeForCode ( decoded ) ;
34+ return `<code>${ escaped } </code>` ;
35+ }
You can’t perform that action at this time.
0 commit comments