@@ -63,36 +63,19 @@ describe("HTML in native Markdown", () => {
6363 expect ( result ) . toContain ( '`<img src="inline.png">`' ) ;
6464 } ) ;
6565
66- it ( "turns HTML anchors into Markdown links, including inline tags in headings and table cells" , ( ) => {
67- const markdown = [
68- '## Deploying with <a href="https://workers.dev"><img alt="Workers" src="https://example.com/logo.svg" width="16"></a> Workers' ,
69- "" ,
70- "| Status | Preview URL |" ,
71- "| -|-|" ,
72- "| Done <br>[View logs](https://example.com/logs) | <a href='https://commit.example.com'>Commit Preview URL</a><br><br><a href='https://branch.example.com'>Branch Preview URL</a> |" ,
73- ] . join ( "\n" ) ;
74- const document = parseMarkdownDocument ( markdown ) ;
75- const links : Array < { url : string ; text : string } > = [ ] ;
76- const visit = ( node : ( typeof document . children ) [ number ] | typeof document ) => {
77- if ( node . type === "link" )
78- links . push ( {
79- url : node . url ,
80- text : node . children
81- . map ( ( child ) => ( "value" in child ? child . value : child . type ) )
82- . join ( "" ) ,
83- } ) ;
84- if ( "children" in node ) node . children . forEach ( visit ) ;
85- } ;
86- visit ( document ) ;
87- expect ( links ) . toEqual ( [
88- { url : "https://workers.dev" , text : "image" } ,
89- { url : "https://example.com/logs" , text : "View logs" } ,
90- { url : "https://commit.example.com" , text : "Commit Preview URL" } ,
91- { url : "https://branch.example.com" , text : "Branch Preview URL" } ,
66+ it ( "links HTML anchors in a table cell without breaking the row at <br>" , ( ) => {
67+ const document = parseMarkdownDocument (
68+ "| Status | Preview URL |\n| - | - |\n| Done | <a href='https://commit.example.com'>Commit</a><br><a href='https://branch.example.com'>Branch</a> |" ,
69+ ) ;
70+ const table = document . children [ 0 ] ;
71+ expect ( table ?. type ) . toBe ( "table" ) ;
72+ if ( table ?. type !== "table" ) return ;
73+ expect ( table . children ) . toHaveLength ( 2 ) ;
74+ expect ( table . children [ 1 ] ?. children [ 1 ] ?. children ) . toMatchObject ( [
75+ { type : "link" , url : "https://commit.example.com" , children : [ { value : "Commit" } ] } ,
76+ { type : "text" , value : " " } ,
77+ { type : "link" , url : "https://branch.example.com" , children : [ { value : "Branch" } ] } ,
9278 ] ) ;
93- expect ( document . children [ 1 ] ) . toMatchObject ( { type : "table" } ) ;
94- if ( document . children [ 1 ] ?. type === "table" )
95- expect ( document . children [ 1 ] . children ) . toHaveLength ( 2 ) ;
9679 } ) ;
9780
9881 it ( "keeps a table row intact when an anchor href contains a pipe" , ( ) => {
@@ -108,63 +91,7 @@ describe("HTML in native Markdown", () => {
10891 ] ) ;
10992 } ) ;
11093
111- it ( "keeps anchor text unlinked for unsafe or unclosed hrefs" , ( ) => {
94+ it ( "keeps anchor text unlinked for unsafe hrefs" , ( ) => {
11295 expect ( markdownWithHtml ( 'Run <a href="javascript:alert(1)">this</a> now' ) ) . toBe ( "Run this now" ) ;
113- expect ( markdownWithHtml ( "<p><a href='https://example.com'>open</p>" ) ) . toBe ( "\n\nopen\n\n" ) ;
114- expect ( markdownWithHtml ( "Mail <a href='mailto:[email protected] '>us</a>" ) ) . toBe ( 115- "Mail [us](<mailto:[email protected] >)" , 116- ) ;
117- } ) ;
118-
119- it ( "keeps an anchor a link after a bang or with brackets in its text" , ( ) => {
120- for ( const [ markdown , text , label ] of [
121- [ "Done!<a href='https://example.com'>logs [1] ]</a>" , "Done!" , "logs [1] ]" ] ,
122- [ "<p>Done!<a href='https://example.com'>logs [1] ]</a></p>" , "Done!" , "logs [1] ]" ] ,
123- [ "Done\\\\!<a href='https://example.com'>logs \\\\]</a>" , "Done\\!" , "logs \\]" ] ,
124- [ "<p>Done\\\\!<a href='https://example.com'>logs \\\\]</a></p>" , "Done\\!" , "logs \\]" ] ,
125- ] as const ) {
126- const document = parseMarkdownDocument ( markdown ) ;
127- const paragraph = document . children [ 0 ] ;
128- expect ( paragraph ?. type ) . toBe ( "paragraph" ) ;
129- if ( paragraph ?. type !== "paragraph" ) continue ;
130- expect ( paragraph . children ) . toMatchObject ( [
131- { type : "text" , value : text } ,
132- { type : "link" , url : "https://example.com" , children : [ { value : label } ] } ,
133- ] ) ;
134- }
135- const document = parseMarkdownDocument (
136- "<p><a href='https://example.com'>`array[0]` in folder\\</a></p>" ,
137- ) ;
138- expect ( document . children [ 0 ] ) . toMatchObject ( {
139- type : "paragraph" ,
140- children : [
141- {
142- type : "link" ,
143- url : "https://example.com" ,
144- children : [
145- { type : "inlineCode" , value : "array[0]" } ,
146- { type : "text" , value : " in folder\\" } ,
147- ] ,
148- } ,
149- ] ,
150- } ) ;
151- } ) ;
152-
153- it ( "keeps an anchor around block content a single link" , ( ) => {
154- const document = parseMarkdownDocument (
155- "<div><a href='https://example.com'><p>Open</p><p>the</p>docs<p>here</p></a></div>" ,
156- ) ;
157- expect ( document . children ) . toMatchObject ( [
158- {
159- type : "paragraph" ,
160- children : [
161- {
162- type : "link" ,
163- url : "https://example.com" ,
164- children : [ { value : "Open the docs here " } ] ,
165- } ,
166- ] ,
167- } ,
168- ] ) ;
16996 } ) ;
17097} ) ;
0 commit comments