Skip to content

Commit

Permalink
fix(docx-to-vfile): set known data attributes on VFile DataMap interf…
Browse files Browse the repository at this point in the history
…ace instead of extending the filetype and breaking everything
  • Loading branch information
tefkah committed Mar 5, 2023
1 parent 2f153d2 commit 0afe6e5
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions libs/reoff/docx-to-vfile/src/lib/docx-to-vfile-unzipit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export interface Options {
include?: string[] | RegExp[] | ((key: string) => boolean) | 'all' | 'allWithDocumentXML'
}

/**
* The data attribute of a VFile
* Is set to the DataMap interface in the vfile module
**/
export interface DocxData extends Data {
/**
* The textcontent of .xml files in the .docx file
Expand All @@ -42,6 +46,25 @@ export interface DocxData extends Data {
relations: { [key: string]: string }
}

declare module 'vfile' {
interface DataMap {
/**
* The textcontent of .xml files in the .docx file
*/
[key: `${string}.xml` | `${string}.rels`]: string | undefined
/**
* The media files in the .docx file
* Possibly undefined only to be compatible with the VFile interface
*/
media?: { [key: string]: ArrayBuffer }
/**
* The relations between the .xml files in the .docx file
* Possibly undefined only to be compatible with the VFile interface
*/
relations?: { [key: string]: string }
}
}

/**
* Extends VFile with a custom data attribute
*/
Expand All @@ -56,10 +79,7 @@ export interface DocxVFile extends VFile {
* @param options Options
* @returns A VFile with the contents of the document.xml file as the root, and the contents of the other xml files as data.
*/
export async function docxToVFile(
file: ArrayBuffer,
userOptions: Options = {},
): Promise<DocxVFile> {
export async function docxToVFile(file: ArrayBuffer, userOptions: Options = {}): Promise<VFile> {
const options: Options = {
withoutMedia: false,
include: 'all',
Expand Down Expand Up @@ -114,7 +134,7 @@ export async function docxToVFile(
// vfile.data = vfileData

if (options.withoutMedia) {
return new VFile({ value: removeCarriage(doc), data: vfileData }) as DocxVFile
return new VFile({ value: removeCarriage(doc), data: vfileData })
}

const mediaUrls = Object.values(relations).filter((rel: string) => rel.includes('media/'))
Expand All @@ -123,5 +143,5 @@ export async function docxToVFile(
media[url] = await entries[`word/${url}`].arrayBuffer()
}
vfileData.media = media
return new VFile({ value: removeCarriage(doc), data: vfileData }) as DocxVFile
return new VFile({ value: removeCarriage(doc), data: vfileData })
}

0 comments on commit 0afe6e5

Please sign in to comment.