File tree Expand file tree Collapse file tree 3 files changed +28
-2
lines changed Expand file tree Collapse file tree 3 files changed +28
-2
lines changed Original file line number Diff line number Diff line change 8
8
* @typedef {BufferEncoding|{encoding?: null|BufferEncoding, mode: Mode?, flag?: string} } WriteOptions
9
9
*
10
10
* @typedef {string|Uint8Array } Path Path of the file.
11
- * @typedef {Path|Options|VFile } Compatible Things that can be
11
+ * @typedef {typeof import('url').URL } URL WHATWG URL
12
+ * @typedef {Path|URL|Options|VFile } Compatible Things that can be
12
13
* passed to the function.
13
14
*/
14
15
20
21
21
22
import fs from 'fs'
22
23
import path from 'path'
24
+ import { fileURLToPath } from 'url'
23
25
import buffer from 'is-buffer'
24
26
import { VFile } from 'vfile'
25
27
@@ -35,6 +37,8 @@ import {VFile} from 'vfile'
35
37
export function toVFile ( options ) {
36
38
if ( typeof options === 'string' || buffer ( options ) ) {
37
39
options = { path : String ( options ) }
40
+ } else if ( options && options . href && options . origin ) {
41
+ options = { path : fileURLToPath ( options ) }
38
42
}
39
43
40
44
return options instanceof VFile ? options : new VFile ( options )
Original file line number Diff line number Diff line change @@ -28,6 +28,7 @@ npm install to-vfile
28
28
import {toVFile } from ' to-vfile'
29
29
30
30
console .log (toVFile (' readme.md' ))
31
+ console .log (toVFile (new URL (' ./readme.md' , import .meta.url)))
31
32
console .log (toVFile .readSync (' .git/HEAD' ))
32
33
console .log (toVFile .readSync (' .git/HEAD' , ' utf8' ))
33
34
` ` `
@@ -41,6 +42,12 @@ VFile {
41
42
history: [' readme.md' ],
42
43
cwd: ' /Users/tilde/projects/oss/to-vfile'
43
44
}
45
+ VFile {
46
+ data: {},
47
+ messages: [],
48
+ history: [' readme.md' ],
49
+ cwd: ' /Users/tilde/projects/oss/to-vfile'
50
+ }
44
51
VFile {
45
52
data: {},
46
53
messages: [],
@@ -67,7 +74,8 @@ There is no default export.
67
74
Create a virtual file.
68
75
Works like the [vfile][] constructor, except when ` options` is ` string` or
69
76
` Buffer` , in which case it’s treated as ` {path: options}` instead of
70
- ` {value: options} ` .
77
+ ` {value: options}` , or when ` options` is a WHATWG ` URL ` object, in which case
78
+ it’s treated as ` {path: fileURLToPath (options)}` .
71
79
72
80
### ` toVFile .read (options[, encoding][, callback])`
73
81
Original file line number Diff line number Diff line change 1
1
import fs from 'fs'
2
2
import path from 'path'
3
+ import { fileURLToPath , URL } from 'url'
3
4
import test from 'tape'
4
5
import buffer from 'is-buffer'
5
6
import { toVFile } from './index.js'
@@ -52,6 +53,19 @@ test('toVFile()', function (t) {
52
53
st . equal ( first , second )
53
54
st . end ( )
54
55
} )
56
+
57
+ t . test ( 'should accept a WHATWG URL object' , function ( st ) {
58
+ const dir = fileURLToPath ( new URL ( './' , import . meta. url ) )
59
+ var file = toVFile ( new URL ( './baz.qux' , import . meta. url ) )
60
+
61
+ st . equal ( file . path , join ( dir , 'baz.qux' ) )
62
+ st . equal ( file . basename , 'baz.qux' )
63
+ st . equal ( file . stem , 'baz' )
64
+ st . equal ( file . extname , '.qux' )
65
+ st . equal ( file . dirname , dir . replace ( / [ / \\ ] $ / , '' ) )
66
+ st . equal ( file . value , undefined )
67
+ st . end ( )
68
+ } )
55
69
} )
56
70
57
71
test ( 'toVFile.readSync' , function ( t ) {
You can’t perform that action at this time.
0 commit comments