Note
This repository is automatically generated from the main parser monorepo. Please submit any issues or pull requests there.
docx-to-vfile
Reads a .docx
file and stores its components in vfile format to be processed by other tools, like reoff-parse
.
Currently extremely dumb and just stores it all in memory, no streams for you.
File reading does happen in streams.
Based on docxtract
Contents
What is this?
When should I use this?
Install
This package is ESM only. In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install as
pnpm add docx-to-vfile
Use
API
docxToVFile()
Takes a docx file as an ArrayBuffer and returns a VFile with the contents of the document.xml file as the root, and the contents of the other xml files as data.
Signature
docxToVFile(file: ArrayBuffer, userOptions: Options = {}): Promise<VFile>;
Parameters
Name | Type | Description |
---|
file | ArrayBuffer | The docx file as an ArrayBuffer |
userOptions | Options | - |
Returns
Promise
<VFile
>
A VFile with the contents of the document.xml file as the root, and the contents of the other xml files as data.
Defined in: src/lib/docx-to-vfile-unzipit.ts:90
DocxData
The data attribute of a VFile
Is set to the DataMap interface in the vfile module
Hierarchy
Indexable
[key
: XMLOrRelsString
]: string
| undefined
Properties
media
object
The media files in the .docx file
Index signature
Type declaration
Overrides: Data.media
Defined in: src/lib/docx-to-vfile-unzipit.ts:45
relations
object
The relations between the .xml files in the .docx file
Index signature
Type declaration
Overrides: Data.relations
Defined in: src/lib/docx-to-vfile-unzipit.ts:49
DocxVFile
Extends VFile with a custom data attribute
This information should be on the VFile interface, this is just used in contexts where you only want to know the type of the data attribute,
e.g. when writing a library that does something with the output of docxToVFile
.
Hierarchy
Properties
cwd
string
Base of path
(default: process.cwd()
or '/'
in browsers).
Inherited from: VFile.cwd
Defined in: node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:53
data
DocxData
Overrides: VFile.data
Defined in: src/lib/docx-to-vfile-unzipit.ts:80
history
string
[]
List of filepaths the file moved between.
The first is the original path and the last is the current path.
Inherited from: VFile.history
Defined in: node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:47
map
undefined
| null
| Map
Source map.
This type is equivalent to the RawSourceMap
type from the source-map
module.
Inherited from: VFile.map
Defined in: node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:85
messages
VFileMessage
[]
List of messages associated with the file.
Inherited from: VFile.messages
Defined in: node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:39
result
unknown
Custom, non-string, compiled, representation.
This is used by unified to store non-string results.
One example is when turning markdown into React nodes.
Inherited from: VFile.result
Defined in: node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:76
stored
boolean
Whether a file was saved to disk.
This is used by vfile reporters.
Inherited from: VFile.stored
Defined in: node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:67
value
Value
Raw value.
Inherited from: VFile.value
Defined in: node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:59
Accessors
basename
Get the basename (including extname) (example: 'index.min.js'
).
Signature
basename(): undefined | string;
Returns
undefined
| string
Inherited from: VFile.basename
Defined in: node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:123
Set basename (including extname) ('index.min.js'
).
Cannot contain path separators ('/'
on unix, macOS, and browsers, '\'
on windows).
Cannot be nullified (use file.path = file.dirname
instead).
Signature
basename(arg: undefined | string): void;
Parameters
Returns
void
Inherited from: VFile.basename
Defined in: node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:119
Inherited from: VFile.basename
Defined in: node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:119 node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:123
dirname
Get the parent path (example: '~'
).
Signature
dirname(): undefined | string;
Returns
undefined
| string
Inherited from: VFile.dirname
Defined in: node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:111
Set the parent path (example: '~'
).
Cannot be set if there’s no path
yet.
Signature
dirname(arg: undefined | string): void;
Parameters
Returns
void
Inherited from: VFile.dirname
Defined in: node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:107
Inherited from: VFile.dirname
Defined in: node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:107 node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:111
extname
Get the extname (including dot) (example: '.js'
).
Signature
extname(): undefined | string;
Returns
undefined
| string
Inherited from: VFile.extname
Defined in: node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:135
Set the extname (including dot) (example: '.js'
).
Cannot contain path separators ('/'
on unix, macOS, and browsers, '\'
on windows).
Cannot be set if there’s no path
yet.
Signature
extname(arg: undefined | string): void;
Parameters
Returns
void
Inherited from: VFile.extname
Defined in: node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:131
Inherited from: VFile.extname
Defined in: node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:131 node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:135
path
Get the full path (example: '~/index.min.js'
).
Signature
path(): string;
Returns
string
Inherited from: VFile.path
Defined in: node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:101
Set the full path (example: '~/index.min.js'
).
Cannot be nullified.
You can set a file URL (a URL
object with a file:
protocol) which will
be turned into a path with url.fileURLToPath
.
Signature
path(arg: string): void;
Parameters
Returns
void
Inherited from: VFile.path
Defined in: node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:95
Inherited from: VFile.path
Defined in: node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:95 node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:101
stem
Get the stem (basename w/o extname) (example: 'index.min'
).
Signature
stem(): undefined | string;
Returns
undefined
| string
Inherited from: VFile.stem
Defined in: node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:147
Set the stem (basename w/o extname) (example: 'index.min'
).
Cannot contain path separators ('/'
on unix, macOS, and browsers, '\'
on windows).
Cannot be nullified (use file.path = file.dirname
instead).
Signature
stem(arg: undefined | string): void;
Parameters
Returns
void
Inherited from: VFile.stem
Defined in: node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:143
Inherited from: VFile.stem
Defined in: node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:143 node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:147
Methods
fail()
Create a fatal error associated with the file.
Its fatal
is set to true
and file
is set to the current file path.
Its added to file.messages
.
👉 Note: a fatal error means that a file is no longer processable.
Throws
Message.
Signature
fail(reason: string | VFileMessage | Error, place?: null | Node<Data> | NodeLike | Position | Point, origin?: null | string): never;
Parameters
Name | Type | Description |
---|
reason | string | VFileMessage |
place? | null | Node <Data > |
origin? | null | string |
Returns
never
Message.
Inherited from: VFile.fail
Defined in: node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:220
info()
Create an info message associated with the file.
Its fatal
is set to null
and file
is set to the current file path.
Its added to file.messages
.
Signature
info(reason: string | VFileMessage | Error, place?: null | Node<Data> | NodeLike | Position | Point, origin?: null | string): VFileMessage;
Parameters
Name | Type | Description |
---|
reason | string | VFileMessage |
place? | null | Node <Data > |
origin? | null | string |
Returns
VFileMessage
Message.
Inherited from: VFile.info
Defined in: node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:195
message()
Create a warning message associated with the file.
Its fatal
is set to false
and file
is set to the current file path.
Its added to file.messages
.
Signature
message(reason: string | VFileMessage | Error, place?: null | Node<Data> | NodeLike | Position | Point, origin?: null | string): VFileMessage;
Parameters
Name | Type | Description |
---|
reason | string | VFileMessage |
place? | null | Node <Data > |
origin? | null | string |
Returns
VFileMessage
Message.
Inherited from: VFile.message
Defined in: node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:174
toString()
Serialize the file.
Signature
toString(encoding?: null | BufferEncoding): string;
Parameters
Name | Type | Description |
---|
encoding? | null | BufferEncoding |
Returns
string
Serialized file.
Inherited from: VFile.toString
Defined in: node_modules/.pnpm/vfile@5.3.7/node_modules/vfile/lib/index.d.ts:157
Options
Properties
include?
string
[] | RegExp
[] | (key
: string
) => boolean
| "all"
| "allWithDocumentXML"
Include only the specified files on the data
attribute of the VFile.
This may be useful if you want to only do something with a subset of the files in the docx file, and don't intend to use 'reoff-stringify' to turn the VFile back into a docx file.
- If an array of strings or regexps is passed, only files that match one of the values will be included.
- If a function is passed, it will be called for each file and should return true to include the file.
- If the value is 'all', almost all files will be included, except for 'word/document.xml', as that already is the root of the VFile.
- If the value is 'allWithDocumentXML', all files will be included, including
word/document.xml
, even though that is already the root of the VFile. Useful if you really want to mimic the original docx file.
You should keep it at the default value if you intend to use 'reoff-stringify' to turn the VFile back into a docx file.
Default
'all'
Defined in: src/lib/docx-to-vfile-unzipit.ts:30
withoutMedia?
boolean
Whether or not to include media in the VFile.
By default, images are included on the data.media
attribute of the VFile as an object of ArrayBuffers, which are accessible both client and serverside.
Default
false
Defined in: src/lib/docx-to-vfile-unzipit.ts:16
XMLOrRelsString
${string}.xml
| ${string}.rels
Defined in: src/lib/docx-to-vfile-unzipit.ts:71
Syntax tree
Types
Compatibility
Security
Related
Contribute
License
GPL-3.0-or-later © Thomas F. K. Jorna