@@ -5,52 +5,54 @@ import type { NodeCreator } from "./creator.ts";
5
5
import type { ImageNode , PlainNode } from "./type.ts" ;
6
6
7
7
const srcFirstStrongImageRegExp =
8
- / \[ h t t p s ? : \/ \/ [ ^ \s \] ] + \. (?: p n g | j p e ? g | g i f | s v g ) (?: \? [ ^ \] \s ] + ) ? (?: \s + h t t p s ? : \/ \/ [ ^ \s \] ] + ) ? \] / i;
8
+ / \[ h t t p s ? : \/ \/ [ ^ \s \] ] + \. (?: p n g | j p e ? g | g i f | s v g ) (?: \? [ ^ \] \s ] + ) ? (?: \s + h t t p s ? : \/ \/ [ ^ \s \] ] + ) ? \] / i;
9
9
const linkFirstStrongImageRegExp =
10
- / \[ h t t p s ? : \/ \/ [ ^ \s \] ] + \s + h t t p s ? : \/ \/ [ ^ \s \] ] + \. (?: p n g | j p e ? g | g i f | s v g ) (?: \? [ ^ \] \s ] + ) ? \] / i;
10
+ / \[ h t t p s ? : \/ \/ [ ^ \s \] ] + \s + h t t p s ? : \/ \/ [ ^ \s \] ] + \. (?: p n g | j p e ? g | g i f | s v g ) (?: \? [ ^ \] \s ] + ) ? \] / i;
11
11
const srcFirstStrongGyazoImageRegExp =
12
- / \[ h t t p s ? : \/ \/ (?: [ 0 - 9 a - z - ] + \. ) ? g y a z o \. c o m \/ [ 0 - 9 a - f ] { 32 } (?: \/ r a w ) ? (?: \s + h t t p s ? : \/ \/ [ ^ \s \] ] + ) ? \] / ;
12
+ / \[ h t t p s ? : \/ \/ (?: [ 0 - 9 a - z - ] + \. ) ? g y a z o \. c o m \/ [ 0 - 9 a - f ] { 32 } (?: \/ r a w ) ? (?: \s + h t t p s ? : \/ \/ [ ^ \s \] ] + ) ? \] / ;
13
13
const linkFirstStrongGyazoImageRegExp =
14
- / \[ h t t p s ? : \/ \/ [ ^ \s \] ] + \s + h t t p s ? : \/ \/ (?: [ 0 - 9 a - z - ] + \. ) ? g y a z o \. c o m \/ [ 0 - 9 a - f ] { 32 } (?: \/ r a w ) ? \] / ;
14
+ / \[ h t t p s ? : \/ \/ [ ^ \s \] ] + \s + h t t p s ? : \/ \/ (?: [ 0 - 9 a - z - ] + \. ) ? g y a z o \. c o m \/ [ 0 - 9 a - f ] { 32 } (?: \/ r a w ) ? \] / ;
15
15
16
16
const isImageUrl = ( text : string ) : boolean =>
17
- / ^ h t t p s ? : \/ \/ [ ^ \s \] ] + \. ( p n g | j p e ? g | g i f | s v g ) ( \? [ ^ \] \s ] + ) ? $ / i. test ( text ) ||
18
- isGyazoImageUrl ( text ) ;
17
+ / ^ h t t p s ? : \/ \/ [ ^ \s \] ] + \. ( p n g | j p e ? g | g i f | s v g ) ( \? [ ^ \] \s ] + ) ? $ / i. test ( text ) ||
18
+ isGyazoImageUrl ( text ) ;
19
19
20
20
const isGyazoImageUrl = ( text : string ) : boolean =>
21
- / ^ h t t p s ? : \/ \/ ( [ 0 - 9 a - z - ] \. ) ? g y a z o \. c o m \/ [ 0 - 9 a - f ] { 32 } ( \/ r a w ) ? $ / . test ( text ) ;
21
+ / ^ h t t p s ? : \/ \/ ( [ 0 - 9 a - z - ] \. ) ? g y a z o \. c o m \/ [ 0 - 9 a - f ] { 32 } ( \/ r a w ) ? $ / . test ( text ) ;
22
22
23
23
const createImageNode : NodeCreator < ImageNode | PlainNode > = ( raw , opts ) => {
24
- if ( opts . context === "table" ) {
25
- return createPlainNode ( raw , opts ) ;
26
- }
27
-
28
- const index = raw . search ( / \s / ) ;
29
- const first =
30
- index !== - 1 ? raw . substring ( 1 , index ) : raw . substring ( 1 , raw . length - 1 ) ;
31
- const second =
32
- index !== - 1 ? raw . substring ( index , raw . length - 1 ) . replace ( / ^ \s + / , '' ) : "" ;
33
- const [ src , link ] = isImageUrl ( second ) ? [ second , first ] : [ first , second ] ;
34
-
35
- return [
36
- {
37
- type : "image" ,
38
- raw,
39
- src : / ^ h t t p s ? : \/ \/ ( [ 0 - 9 a - z - ] \. ) ? g y a z o \. c o m \/ [ 0 - 9 a - f ] { 32 } $ / . test ( src )
40
- ? `${ src } /thumb/1000`
41
- : src ,
42
- link,
43
- } ,
44
- ] ;
24
+ if ( opts . context === "table" ) {
25
+ return createPlainNode ( raw , opts ) ;
26
+ }
27
+
28
+ const index = raw . search ( / \s / ) ;
29
+ const first =
30
+ index !== - 1 ? raw . substring ( 1 , index ) : raw . substring ( 1 , raw . length - 1 ) ;
31
+ const second =
32
+ index !== - 1
33
+ ? raw . substring ( index , raw . length - 1 ) . replace ( / ^ \s + / , "" )
34
+ : "" ;
35
+ const [ src , link ] = isImageUrl ( second ) ? [ second , first ] : [ first , second ] ;
36
+
37
+ return [
38
+ {
39
+ type : "image" ,
40
+ raw,
41
+ src : / ^ h t t p s ? : \/ \/ ( [ 0 - 9 a - z - ] \. ) ? g y a z o \. c o m \/ [ 0 - 9 a - f ] { 32 } $ / . test ( src )
42
+ ? `${ src } /thumb/1000`
43
+ : src ,
44
+ link,
45
+ } ,
46
+ ] ;
45
47
} ;
46
48
47
49
export const ImageNodeParser = createNodeParser ( createImageNode , {
48
- parseOnNested : true ,
49
- parseOnQuoted : true ,
50
- patterns : [
51
- srcFirstStrongImageRegExp ,
52
- linkFirstStrongImageRegExp ,
53
- srcFirstStrongGyazoImageRegExp ,
54
- linkFirstStrongGyazoImageRegExp ,
55
- ] ,
50
+ parseOnNested : true ,
51
+ parseOnQuoted : true ,
52
+ patterns : [
53
+ srcFirstStrongImageRegExp ,
54
+ linkFirstStrongImageRegExp ,
55
+ srcFirstStrongGyazoImageRegExp ,
56
+ linkFirstStrongGyazoImageRegExp ,
57
+ ] ,
56
58
} ) ;
0 commit comments