@@ -10,9 +10,14 @@ import { Resource, ResourceParser } from '../../core/model/note';
10
10
import { getFoamVsCodeConfig } from '../../services/config' ;
11
11
import { fromVsCodeUri , toVsCodeUri } from '../../utils/vsc-utils' ;
12
12
import { MarkdownLink } from '../../core/services/markdown-link' ;
13
+ import { URI } from '../../core/model/uri' ;
13
14
import { Position } from '../../core/model/position' ;
14
15
import { TextEdit } from '../../core/services/text-edit' ;
15
16
import { isNone , isSome } from '../../core/utils' ;
17
+ import {
18
+ asAbsoluteWorkspaceUri ,
19
+ isVirtualWorkspace ,
20
+ } from '../../services/editor' ;
16
21
17
22
export const WIKILINK_EMBED_REGEX =
18
23
/ ( (?: (?: f u l l | c o n t e n t ) - (?: i n l i n e | c a r d ) | f u l l | c o n t e n t | i n l i n e | c a r d ) ? ! \[ \[ [ ^ [ \] ] + ?\] \] ) / ;
@@ -22,7 +27,7 @@ export const WIKILINK_EMBED_REGEX =
22
27
export const WIKILINK_EMBED_REGEX_GROUPS =
23
28
/ ( (?: \w + ) | (?: (?: \w + ) - (?: \w + ) ) ) ? ! \[ \[ ( [ ^ [ \] ] + ?) \] \] / ;
24
29
export const CONFIG_EMBED_NOTE_TYPE = 'preview.embedNoteType' ;
25
- const refsStack : string [ ] = [ ] ;
30
+ let refsStack : string [ ] = [ ] ;
26
31
27
32
export const markdownItWikilinkEmbed = (
28
33
md : markdownit ,
@@ -38,6 +43,14 @@ export const markdownItWikilinkEmbed = (
38
43
WIKILINK_EMBED_REGEX_GROUPS
39
44
) ;
40
45
46
+ if ( isVirtualWorkspace ( ) ) {
47
+ return `
48
+ <div class="foam-embed-not-supported-warning">
49
+ Embed not supported in virtual workspace: ![[${ wikilink } ]]
50
+ </div>
51
+ ` ;
52
+ }
53
+
41
54
const includedNote = workspace . find ( wikilink ) ;
42
55
43
56
if ( ! includedNote ) {
@@ -48,56 +61,31 @@ export const markdownItWikilinkEmbed = (
48
61
includedNote . uri . path . toLocaleLowerCase ( )
49
62
) ;
50
63
51
- if ( ! cyclicLinkDetected ) {
52
- refsStack . push ( includedNote . uri . path . toLocaleLowerCase ( ) ) ;
53
- }
54
-
55
64
if ( cyclicLinkDetected ) {
56
- return `<div class="foam-cyclic-link-warning">Cyclic link detected for wikilink: ${ wikilink } </div>` ;
57
- }
58
- let content = `Embed for [[${ wikilink } ]]` ;
59
- let html : string ;
60
-
61
- switch ( includedNote . type ) {
62
- case 'note' : {
63
- const { noteScope, noteStyle } =
64
- retrieveNoteConfig ( noteEmbedModifier ) ;
65
-
66
- const extractor : EmbedNoteExtractor =
67
- noteScope === 'full'
68
- ? fullExtractor
69
- : noteScope === 'content'
70
- ? contentExtractor
71
- : fullExtractor ;
72
-
73
- const formatter : EmbedNoteFormatter =
74
- noteStyle === 'card'
75
- ? cardFormatter
76
- : noteStyle === 'inline'
77
- ? inlineFormatter
78
- : cardFormatter ;
79
-
80
- content = extractor ( includedNote , parser , workspace ) ;
81
- html = formatter ( content , md ) ;
82
- break ;
83
- }
84
- case 'attachment' :
85
- content = `
86
- <div class="embed-container-attachment">
87
- ${ md . renderInline ( '[[' + wikilink + ']]' ) } <br/>
88
- Embed for attachments is not supported
89
- </div>` ;
90
- html = md . render ( content ) ;
91
- break ;
92
- case 'image' :
93
- content = `<div class="embed-container-image">${ md . render (
94
- ` } )`
95
- ) } </div>`;
96
- html = md . render ( content ) ;
97
- break ;
65
+ return `
66
+ <div class="foam-cyclic-link-warning">
67
+ Cyclic link detected for wikilink: ${ wikilink }
68
+ <div class="foam-cyclic-link-warning__stack">
69
+ Link sequence:
70
+ <ul>
71
+ ${ refsStack . map ( ref => `<li>${ ref } </li>` ) . join ( '' ) }
72
+ </ul>
73
+ </div>
74
+ </div>
75
+ ` ;
98
76
}
77
+
78
+ refsStack . push ( includedNote . uri . path . toLocaleLowerCase ( ) ) ;
79
+
80
+ const content = getNoteContent (
81
+ includedNote ,
82
+ noteEmbedModifier ,
83
+ parser ,
84
+ workspace ,
85
+ md
86
+ ) ;
99
87
refsStack . pop ( ) ;
100
- return html ;
88
+ return content ;
101
89
} catch ( e ) {
102
90
Logger . error (
103
91
`Error while including ${ wikilinkItem } into the current document of the Preview panel` ,
@@ -109,27 +97,79 @@ Embed for attachments is not supported
109
97
} ) ;
110
98
} ;
111
99
100
+ function getNoteContent (
101
+ includedNote : Resource ,
102
+ noteEmbedModifier : string | undefined ,
103
+ parser : ResourceParser ,
104
+ workspace : FoamWorkspace ,
105
+ md : markdownit
106
+ ) : string {
107
+ let content = `Embed for [[${ includedNote . uri . path } ]]` ;
108
+ let html : string ;
109
+
110
+ switch ( includedNote . type ) {
111
+ case 'note' : {
112
+ const { noteScope, noteStyle } = retrieveNoteConfig ( noteEmbedModifier ) ;
113
+
114
+ const extractor : EmbedNoteExtractor =
115
+ noteScope === 'full'
116
+ ? fullExtractor
117
+ : noteScope === 'content'
118
+ ? contentExtractor
119
+ : fullExtractor ;
120
+
121
+ const formatter : EmbedNoteFormatter =
122
+ noteStyle === 'card'
123
+ ? cardFormatter
124
+ : noteStyle === 'inline'
125
+ ? inlineFormatter
126
+ : cardFormatter ;
127
+
128
+ content = extractor ( includedNote , parser , workspace ) ;
129
+ html = formatter ( content , md ) ;
130
+ break ;
131
+ }
132
+ case 'attachment' :
133
+ content = `
134
+ <div class="embed-container-attachment">
135
+ ${ md . renderInline ( '[[' + includedNote . uri . path + ']]' ) } <br/>
136
+ Embed for attachments is not supported
137
+ </div>` ;
138
+ html = md . render ( content ) ;
139
+ break ;
140
+ case 'image' :
141
+ content = `<div class="embed-container-image">${ md . render (
142
+ ` } )`
143
+ ) } </div>`;
144
+ html = md . render ( content ) ;
145
+ break ;
146
+ default :
147
+ html = content ;
148
+ }
149
+
150
+ return html ;
151
+ }
152
+
112
153
function withLinksRelativeToWorkspaceRoot (
154
+ noteUri : URI ,
113
155
noteText : string ,
114
156
parser : ResourceParser ,
115
157
workspace : FoamWorkspace
116
- ) {
158
+ ) : string {
117
159
const note = parser . parse (
118
160
fromVsCodeUri ( vsWorkspace . workspaceFolders [ 0 ] . uri ) ,
119
161
noteText
120
162
) ;
121
163
const edits = note . links
122
164
. map ( link => {
123
165
const info = MarkdownLink . analyzeLink ( link ) ;
124
- const resource = workspace . find ( info . target ) ;
166
+ const resource = workspace . find ( info . target , noteUri ) ;
125
167
// embedded notes that aren't created are still collected
126
168
// return null so it can be filtered in the next step
127
169
if ( isNone ( resource ) ) {
128
170
return null ;
129
171
}
130
- const pathFromRoot = vsWorkspace . asRelativePath (
131
- toVsCodeUri ( resource . uri )
132
- ) ;
172
+ const pathFromRoot = asAbsoluteWorkspaceUri ( resource . uri ) . path ;
133
173
return MarkdownLink . createUpdateLinkEdit ( link , {
134
174
target : pathFromRoot ,
135
175
} ) ;
@@ -185,7 +225,12 @@ function fullExtractor(
185
225
. slice ( section . range . start . line , section . range . end . line )
186
226
. join ( '\n' ) ;
187
227
}
188
- noteText = withLinksRelativeToWorkspaceRoot ( noteText , parser , workspace ) ;
228
+ noteText = withLinksRelativeToWorkspaceRoot (
229
+ note . uri ,
230
+ noteText ,
231
+ parser ,
232
+ workspace
233
+ ) ;
189
234
return noteText ;
190
235
}
191
236
@@ -211,7 +256,12 @@ function contentExtractor(
211
256
}
212
257
rows . shift ( ) ;
213
258
noteText = rows . join ( '\n' ) ;
214
- noteText = withLinksRelativeToWorkspaceRoot ( noteText , parser , workspace ) ;
259
+ noteText = withLinksRelativeToWorkspaceRoot (
260
+ note . uri ,
261
+ noteText ,
262
+ parser ,
263
+ workspace
264
+ ) ;
215
265
return noteText ;
216
266
}
217
267
0 commit comments