1
- import { isSupportedFileFormat } from " ./isSupportedFileFormat" ;
1
+ import { isSupportedFileFormat } from ' ./isSupportedFileFormat' ;
2
2
3
3
const defaultWikiLinkResolver = ( target : string ) => {
4
4
// for [[#heading]] links
5
5
if ( ! target ) {
6
6
return [ ] ;
7
7
}
8
- let permalink = target . replace ( / \/ i n d e x $ / , "" ) ;
8
+ let permalink = target . replace ( / \/ i n d e x $ / , '' ) ;
9
9
// TODO what to do with [[index]] link?
10
10
if ( permalink . length === 0 ) {
11
- permalink = "/" ;
11
+ permalink = '/' ;
12
12
}
13
13
return [ permalink ] ;
14
14
} ;
15
15
16
16
export interface FromMarkdownOptions {
17
17
pathFormat ?:
18
- | " raw" // default; use for regular relative or absolute paths
19
- | " obsidian-absolute" // use for Obsidian-style absolute paths (with no leading slash)
20
- | " obsidian-short" ; // use for Obsidian-style shortened paths (shortest path possible)
18
+ | ' raw' // default; use for regular relative or absolute paths
19
+ | ' obsidian-absolute' // use for Obsidian-style absolute paths (with no leading slash)
20
+ | ' obsidian-short' ; // use for Obsidian-style shortened paths (shortest path possible)
21
21
permalinks ?: string [ ] ; // list of permalinks to match possible permalinks of a wiki link against
22
22
wikiLinkResolver ?: ( name : string ) => string [ ] ; // function to resolve wiki links to an array of possible permalinks
23
23
newClassName ?: string ; // class name to add to links that don't have a matching permalink
24
24
wikiLinkClassName ?: string ; // class name to add to all wiki links
25
25
hrefTemplate ?: ( permalink : string ) => string ; // function to generate the href attribute of a link
26
26
}
27
27
28
+ export function getImageSize ( size : string ) {
29
+ // eslint-disable-next-line prefer-const
30
+ let [ width , height ] = size . split ( 'x' ) ;
31
+
32
+ if ( ! height ) height = width ;
33
+
34
+ return { width, height } ;
35
+ }
36
+
28
37
// mdas-util-from-markdown extension
29
38
// https://github.com/syntax-tree/mdast-util-from-markdown#extension
30
39
function fromMarkdown ( opts : FromMarkdownOptions = { } ) {
31
- const pathFormat = opts . pathFormat || " raw" ;
40
+ const pathFormat = opts . pathFormat || ' raw' ;
32
41
const permalinks = opts . permalinks || [ ] ;
33
42
const wikiLinkResolver = opts . wikiLinkResolver || defaultWikiLinkResolver ;
34
- const newClassName = opts . newClassName || " new" ;
35
- const wikiLinkClassName = opts . wikiLinkClassName || " internal" ;
43
+ const newClassName = opts . newClassName || ' new' ;
44
+ const wikiLinkClassName = opts . wikiLinkClassName || ' internal' ;
36
45
const defaultHrefTemplate = ( permalink : string ) => permalink ;
37
46
38
47
const hrefTemplate = opts . hrefTemplate || defaultHrefTemplate ;
@@ -44,9 +53,9 @@ function fromMarkdown(opts: FromMarkdownOptions = {}) {
44
53
function enterWikiLink ( token ) {
45
54
this . enter (
46
55
{
47
- type : " wikiLink" ,
56
+ type : ' wikiLink' ,
48
57
data : {
49
- isEmbed : token . isType === " embed" ,
58
+ isEmbed : token . isType === ' embed' ,
50
59
target : null , // the target of the link, e.g. "Foo Bar#Heading" in "[[Foo Bar#Heading]]"
51
60
alias : null , // the alias of the link, e.g. "Foo" in "[[Foo Bar|Foo]]"
52
61
permalink : null , // TODO shouldn't this be named just "link"?
@@ -80,18 +89,18 @@ function fromMarkdown(opts: FromMarkdownOptions = {}) {
80
89
} = wikiLink ;
81
90
// eslint-disable-next-line no-useless-escape
82
91
const wikiLinkWithHeadingPattern = / ^ ( .* ?) ( # .* ) ? $ / u;
83
- const [ , path , heading = "" ] = target . match ( wikiLinkWithHeadingPattern ) ;
92
+ const [ , path , heading = '' ] = target . match ( wikiLinkWithHeadingPattern ) ;
84
93
85
94
const possibleWikiLinkPermalinks = wikiLinkResolver ( path ) ;
86
95
87
96
const matchingPermalink = permalinks . find ( ( e ) => {
88
97
return possibleWikiLinkPermalinks . find ( ( p ) => {
89
- if ( pathFormat === " obsidian-short" ) {
98
+ if ( pathFormat === ' obsidian-short' ) {
90
99
if ( e === p || e . endsWith ( p ) ) {
91
100
return true ;
92
101
}
93
- } else if ( pathFormat === " obsidian-absolute" ) {
94
- if ( e === "/" + p ) {
102
+ } else if ( pathFormat === ' obsidian-absolute' ) {
103
+ if ( e === '/' + p ) {
95
104
return true ;
96
105
}
97
106
} else {
@@ -106,65 +115,75 @@ function fromMarkdown(opts: FromMarkdownOptions = {}) {
106
115
// TODO this is ugly
107
116
const link =
108
117
matchingPermalink ||
109
- ( pathFormat === " obsidian-absolute"
110
- ? "/" + possibleWikiLinkPermalinks [ 0 ]
118
+ ( pathFormat === ' obsidian-absolute'
119
+ ? '/' + possibleWikiLinkPermalinks [ 0 ]
111
120
: possibleWikiLinkPermalinks [ 0 ] ) ||
112
- "" ;
121
+ '' ;
113
122
114
123
wikiLink . data . exists = ! ! matchingPermalink ;
115
124
wikiLink . data . permalink = link ;
116
-
117
125
// remove leading # if the target is a heading on the same page
118
- const displayName = alias || target . replace ( / ^ # / , "" ) ;
119
- const headingId = heading . replace ( / \s + / g, "-" ) . toLowerCase ( ) ;
126
+ const displayName = alias || target . replace ( / ^ # / , '' ) ;
127
+ const headingId = heading . replace ( / \s + / g, '-' ) . toLowerCase ( ) ;
120
128
let classNames = wikiLinkClassName ;
121
129
if ( ! matchingPermalink ) {
122
- classNames += " " + newClassName ;
130
+ classNames += ' ' + newClassName ;
123
131
}
124
132
125
133
if ( isEmbed ) {
126
134
const [ isSupportedFormat , format ] = isSupportedFileFormat ( target ) ;
127
135
if ( ! isSupportedFormat ) {
128
136
// Temporarily render note transclusion as a regular wiki link
129
137
if ( ! format ) {
130
- wikiLink . data . hName = "a" ;
138
+ wikiLink . data . hName = 'a' ;
131
139
wikiLink . data . hProperties = {
132
- className : classNames + " " + " transclusion" ,
140
+ className : classNames + ' ' + ' transclusion' ,
133
141
href : hrefTemplate ( link ) + headingId ,
134
142
} ;
135
- wikiLink . data . hChildren = [ { type : "text" , value : displayName } ] ;
136
-
143
+ wikiLink . data . hChildren = [ { type : 'text' , value : displayName } ] ;
137
144
} else {
138
- wikiLink . data . hName = "p" ;
145
+ wikiLink . data . hName = 'p' ;
139
146
wikiLink . data . hChildren = [
140
147
{
141
- type : " text" ,
148
+ type : ' text' ,
142
149
value : `![[${ target } ]]` ,
143
150
} ,
144
151
] ;
145
152
}
146
- } else if ( format === " pdf" ) {
147
- wikiLink . data . hName = " iframe" ;
153
+ } else if ( format === ' pdf' ) {
154
+ wikiLink . data . hName = ' iframe' ;
148
155
wikiLink . data . hProperties = {
149
156
className : classNames ,
150
- width : " 100%" ,
157
+ width : ' 100%' ,
151
158
src : `${ hrefTemplate ( link ) } #toolbar=0` ,
152
159
} ;
153
160
} else {
154
- wikiLink . data . hName = "img" ;
155
- wikiLink . data . hProperties = {
156
- className : classNames ,
157
- src : hrefTemplate ( link ) ,
158
- alt : displayName ,
159
- } ;
161
+ const hasDimensions = alias && / ^ \d + ( x \d + ) ? $ / . test ( alias ) ;
162
+ // Take the target as alt text except if alt name was provided [[target|alt text]]
163
+ const altText = hasDimensions || ! alias ? target : alias ;
164
+
165
+ wikiLink . data . hName = 'img' ;
166
+ wikiLink . data . hProperties = {
167
+ className : classNames ,
168
+ src : hrefTemplate ( link ) ,
169
+ alt : altText
170
+ } ;
171
+
172
+ if ( hasDimensions ) {
173
+ const { width, height } = getImageSize ( alias as string ) ;
174
+ Object . assign ( wikiLink . data . hProperties , {
175
+ width,
176
+ height,
177
+ } ) ;
178
+ }
160
179
}
161
180
} else {
162
- wikiLink . data . hName = "a" ;
181
+ wikiLink . data . hName = 'a' ;
163
182
wikiLink . data . hProperties = {
164
183
className : classNames ,
165
184
href : hrefTemplate ( link ) + headingId ,
166
185
} ;
167
- wikiLink . data . hChildren = [ { type : " text" , value : displayName } ] ;
186
+ wikiLink . data . hChildren = [ { type : ' text' , value : displayName } ] ;
168
187
}
169
188
}
170
189
0 commit comments