2019-07-30 09:35:42 +02:00
/* eslint-disable no-unused-vars */
2021-08-12 16:54:10 +01:00
import markdownUtils from './markdownUtils' ;
2018-05-23 12:14:38 +01:00
describe ( 'markdownUtils' , function ( ) {
beforeEach ( async ( done ) = > {
done ( ) ;
} ) ;
2020-12-01 18:05:24 +00:00
it ( 'should prepend a base URL' , ( async ( ) = > {
2018-05-23 12:14:38 +01:00
const baseUrl = 'https://test.com/site' ;
const testCases = [
[ '[something](testing.html)' , '[something](https://test.com/site/testing.html)' ] ,
[ '' , '' ] ,
[ '[](/index.html "Home page")' , '[](https://test.com/index.html "Home page")' ] ,
[ '[onelink.com](/jmp/?id=123&u=http://something.com/test)' , '[onelink.com](https://test.com/jmp/?id=123&u=http://something.com/test)' ] ,
2019-10-29 05:02:42 -04:00
[ '[](/jmp/?s=80&l=related&u=http://example.com "some description")' , '[](https://test.com/jmp/?s=80&l=related&u=http://example.com "some description")' ] ,
2018-05-23 12:14:38 +01:00
] ;
for ( let i = 0 ; i < testCases . length ; i ++ ) {
const md = testCases [ i ] [ 0 ] ;
const expected = testCases [ i ] [ 1 ] ;
expect ( markdownUtils . prependBaseUrl ( md , baseUrl ) ) . toBe ( expected ) ;
}
2019-09-23 23:23:10 +01:00
} ) ) ;
2018-05-23 12:14:38 +01:00
2020-12-01 18:05:24 +00:00
it ( 'should extract image URLs' , ( async ( ) = > {
2018-05-23 14:25:59 +01:00
const testCases = [
[ '' , [ 'http://test.com/img.png' ] ] ,
[ ' ' , [ 'http://test.com/img.png' , 'http://test.com/img2.png' ] ] ,
[ '' , [ 'http://test.com/img.png' ] ] ,
2018-09-24 20:15:23 +01:00
[ '.png)' , [ 'https://test.com/ohoh_(123).png' ] ] ,
2020-02-12 18:15:16 +00:00
[ '![nothing]() ' , [ 'http://test.com/img.png' ] ] ,
2021-05-19 23:22:03 +02:00
[ '' , [ 'img.png' ] ] ,
[ '' , [ '/img.png' ] ] ,
[ '' , [ '../img.png' ] ] ,
[ '' , [ '../upload/img.png' ] ] ,
[ '' , [ './upload/img.png' ] ] ,
[ '[something](testing.html)' , [ '' ] ] ,
[ '[something](img.png)' , [ '' ] ] ,
[ '' , [ 'file://img.png' ] ] ,
2018-05-23 14:25:59 +01:00
] ;
for ( let i = 0 ; i < testCases . length ; i ++ ) {
2021-08-12 16:54:10 +01:00
const md = testCases [ i ] [ 0 ] as string ;
2020-02-12 18:15:16 +00:00
const actual = markdownUtils . extractImageUrls ( md ) ;
2018-05-23 14:25:59 +01:00
const expected = testCases [ i ] [ 1 ] ;
2021-05-19 23:22:03 +02:00
expect ( actual . join ( ' ' ) ) . toBe ( ( expected as string [ ] ) . join ( ' ' ) ) ;
}
} ) ) ;
it ( 'should extract files URLs' , ( async ( ) = > {
const testCases = [
[ '[something](http://test.com/img.png)' , [ 'http://test.com/img.png' ] ] ,
[ '[something](http://test.com/test.txt)' , [ 'http://test.com/test.txt' ] ] ,
[ '[something](http://test.com/img.png) ' , [ 'http://test.com/img.png' , 'http://test.com/img2.png' ] ] ,
[ '[something](http://test.com/img.png "Some description")' , [ 'http://test.com/img.png' ] ] ,
[ '[something](https://test.com/ohoh_(123).png)' , [ 'https://test.com/ohoh_(123).png' ] ] ,
[ '[nothing]() ' , [ 'http://test.com/img.png' ] ] ,
[ '[something](test.txt)' , [ 'test.txt' ] ] ,
[ '[something](/test.txt)' , [ '/test.txt' ] ] ,
[ '[something](../test.txt)' , [ '../test.txt' ] ] ,
[ '[something](../upload/test.txt)' , [ '../upload/test.txt' ] ] ,
[ '[something](./upload/test.txt)' , [ './upload/test.txt' ] ] ,
[ '[something](testing.html)' , [ 'testing.html' ] ] ,
[ '[something](img.png)' , [ 'img.png' ] ] ,
[ '[something](file://img.png)' , [ 'file://img.png' ] ] ,
] ;
for ( let i = 0 ; i < testCases . length ; i ++ ) {
2021-08-12 16:54:10 +01:00
const md = testCases [ i ] [ 0 ] as string ;
2021-05-19 23:22:03 +02:00
const actual = markdownUtils . extractFileUrls ( md ) ;
const expected = testCases [ i ] [ 1 ] ;
2018-05-23 14:25:59 +01:00
2021-05-19 23:22:03 +02:00
expect ( actual . join ( ' ' ) ) . toBe ( ( expected as string [ ] ) . join ( ' ' ) ) ;
2018-05-23 14:25:59 +01:00
}
2019-09-23 23:23:10 +01:00
} ) ) ;
2018-05-23 14:25:59 +01:00
2020-12-01 18:05:24 +00:00
it ( 'escape a markdown link' , ( async ( ) = > {
2020-05-25 09:52:10 +01:00
const testCases = [
[ 'file:///Users/who put spaces in their username??/.config/joplin' , 'file:///Users/who%20put%20spaces%20in%20their%20username??/.config/joplin' ] ,
[ 'file:///Users/(and brackets???)/.config/joplin' , 'file:///Users/%28and%20brackets???%29/.config/joplin' ] ,
[ 'file:///Users/thisisfine/.config/joplin' , 'file:///Users/thisisfine/.config/joplin' ] ,
] ;
for ( let i = 0 ; i < testCases . length ; i ++ ) {
const md = testCases [ i ] [ 0 ] ;
const expected = testCases [ i ] [ 1 ] ;
expect ( markdownUtils . escapeLinkUrl ( md ) ) . toBe ( expected ) ;
}
} ) ) ;
2020-12-01 18:05:24 +00:00
it ( 'escape a markdown link (title)' , ( async ( ) = > {
2020-02-07 17:15:41 -05:00
const testCases = [
[ 'Helmut K. C. Tessarek' , 'Helmut K. C. Tessarek' ] ,
[ 'Helmut (K. C.) Tessarek' , 'Helmut (K. C.) Tessarek' ] ,
[ 'Helmut [K. C.] Tessarek' , 'Helmut \\[K. C.\\] Tessarek' ] ,
] ;
for ( let i = 0 ; i < testCases . length ; i ++ ) {
const md = testCases [ i ] [ 0 ] ;
const expected = testCases [ i ] [ 1 ] ;
expect ( markdownUtils . escapeTitleText ( md ) ) . toBe ( expected ) ;
}
} ) ) ;
2020-12-01 18:05:24 +00:00
it ( 'replace markdown link with description' , ( async ( ) = > {
2020-05-09 20:25:00 +05:30
const testCases = [
[ 'Test case [one](link)' , 'Test case one' ] ,
[ 'Test case ' , 'Test case two' ] ,
[ '**# -Test case three' , 'Test case three' ] ,
[ 'This is a looooooong tiiitlllle with moore thaaaaaaan eighty characters and a [link](linkurl) at the end' , 'This is a looooooong tiiitlllle with moore thaaaaaaan eighty characters and a li' ] ,
[ '' , '' ] ,
[ 'These are [link1](one), [link2](two) and ' , 'These are link1, link2 and link3' ] ,
[ 'No description link to [](https://joplinapp.org)' , 'No description link to https://joplinapp.org' ] ,
] ;
for ( let i = 0 ; i < testCases . length ; i ++ ) {
const md = testCases [ i ] [ 0 ] ;
const expected = testCases [ i ] [ 1 ] ;
expect ( markdownUtils . titleFromBody ( md ) ) . toBe ( expected ) ;
}
} ) ) ;
2019-07-30 09:35:42 +02:00
} ) ;