diff options
Diffstat (limited to 'packages/gitbook-core/src/models/File.js')
-rw-r--r-- | packages/gitbook-core/src/models/File.js | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/packages/gitbook-core/src/models/File.js b/packages/gitbook-core/src/models/File.js new file mode 100644 index 0000000..09dfc1b --- /dev/null +++ b/packages/gitbook-core/src/models/File.js @@ -0,0 +1,43 @@ +const path = require('path'); +const { Record } = require('immutable'); + +const DEFAULTS = { + type: '', + mtime: '', + path: '' +}; + +class File extends Record(DEFAULTS) { + constructor(file) { + if (typeof file === 'string') { + file = { path: file }; + } + + super(file); + } + + /** + * Return url for a file in a GitBook context. + * @param {Context} context + * @return {String} url + */ + toURL(context) { + const { file } = context.getState(); + + return path.relative( + path.dirname(file.path), + this.path + ); + } + + static is(file) { + return (file instanceof File); + } + + static create(file) { + return file instanceof File ? + file : new File(file); + } +} + +module.exports = File; |