Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for WHATWG URL #17

Merged
merged 3 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import fs from 'fs'
import path from 'path'
import {fileURLToPath} from 'url'
import buffer from 'is-buffer'
import {VFile} from 'vfile'

Expand All @@ -35,6 +36,8 @@ import {VFile} from 'vfile'
export function toVFile(options) {
if (typeof options === 'string' || buffer(options)) {
options = {path: String(options)}
} else if (options && options.href && options.origin) {
options = {path: fileURLToPath(options)}
}

return options instanceof VFile ? options : new VFile(options)
Expand Down
9 changes: 5 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ npm install to-vfile
```js
import {toVFile} from 'to-vfile'

console.log(toVFile('readme.md'))
console.log(toVFile.readSync('.git/HEAD'))
console.log(toVFile.readSync('.git/HEAD', 'utf8'))
console.log(toVFile(new URL('./readme.md', import.meta.url)))
console.log(toVFile.readSync(new URL('./.git/HEAD', import.meta.url)))
console.log(toVFile.readSync(new URL('./.git/HEAD', import.meta.url), 'utf8'))
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
```

Yields:
Expand Down Expand Up @@ -67,7 +67,8 @@ There is no default export.
Create a virtual file.
Works like the [vfile][] constructor, except when `options` is `string` or
`Buffer`, in which case it’s treated as `{path: options}` instead of
`{value: options}`.
`{value: options}`, or when `options` is a WHATWG `URL` object, in which case
it’s treated as `{path: fileURLToPath(options)}`.

### `toVFile.read(options[, encoding][, callback])`

Expand Down
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ test('toVFile()', function (t) {
st.equal(first, second)
st.end()
})

t.test('should accept a WHATWG URL object', function (st) {
var file = toVFile(new URL('file:///foo/bar/baz.qux'))
wooorm marked this conversation as resolved.
Show resolved Hide resolved

st.equal(file.path, join('/foo', 'bar', 'baz.qux'))
st.equal(file.basename, 'baz.qux')
st.equal(file.stem, 'baz')
st.equal(file.extname, '.qux')
st.equal(file.dirname, join('/foo', 'bar'))
st.equal(file.value, undefined)
st.end()
})
})

test('toVFile.readSync', function (t) {
Expand Down