Skip to content

Commit

Permalink
fix(docx-to-vfile): fix imports
Browse files Browse the repository at this point in the history
  • Loading branch information
tefkah committed Mar 27, 2023
1 parent 4178515 commit 8fbd4d1
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 51 deletions.
2 changes: 1 addition & 1 deletion libs/reoff/docx-to-vfile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"repository": "https://github.com/TrialAndErrorOrg/parsers",
"author": "Thomas F. K. Jorna <hello@tefkah.com>",
"type": "module",
"description": "Reads a `.docx` file and stores its components in vfile format to be processed by other tools, like [`reoff-parse`](https://github.com/TrialAndErrorOrg/parsers/tree/main/libs/reoff/reoff-parse).",
"description": "Reads a `.docx` file and stores its components in vfile format to be processed by other tools, like `reoff-parse`.",
"keywords": [
"unified",
"docx",
Expand Down
41 changes: 0 additions & 41 deletions libs/reoff/docx-to-vfile/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1 @@
// import type { ReadStream } from 'fs'
// import { VFile } from 'vfile'
// import { docxToVFile as docxToVFileBrowser, Options } from './lib/docx-to-vfile-unzipit.js'

// /**
// * Takes a docx file as a Blob, Buffer, ArrayBuffer, ReadStream or string
// * 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.
// *
// * Should work in both Node and the browser, but to be sure it works in the browser, import it like
// * ```ts
// * import { docxToVFile } from 'docx-to-vfile/browser'
// * ```
// *
// * @param file The docx file as a File, Blob, Buffer, ArrayBuffer, ReadStream or string
// * @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 | File | Blob | Buffer | ReadStream | string,
// options?: Options,
// ): Promise<VFile> {
// if (typeof window !== 'undefined') {
// return await docxToVFileBrowser(file as Blob, options)
// }

// const { createReadStream, ReadStream } = await import('fs')
// const { blob } = await import('stream/consumers')
// const { Blob: NodeBlob } = await import('buffer')

// const inp = typeof file === 'string' ? createReadStream(file) : file

// const input =
// inp instanceof ReadStream
// ? await blob(inp)
// : inp instanceof ArrayBuffer
// ? new NodeBlob([Buffer.from(inp)])
// : inp

// return await docxToVFileBrowser(input as Blob | File, options)
// }

export * from './lib/docx-to-vfile-unzipit.js'
8 changes: 1 addition & 7 deletions libs/reoff/docx-to-vfile/src/lib/docx-to-vfile-unzipit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Data, VFile } from 'vfile'
import { unzip } from 'unzipit'
import type { ReadStream } from 'fs'

const removeHeader = (text: string | undefined) => (text ? text.replace(/<\?xml.*?\?>/, '') : '')

Expand Down Expand Up @@ -102,10 +101,8 @@ export async function docxToVFile(
let input = file

// node code
if (process.env) {
if (typeof window === 'undefined') {
const { readFile } = await import('fs/promises')
const { Buffer } = await import('buffer')

const inp = typeof file === 'string' ? await readFile(file) : file

input = Buffer.isBuffer(inp) ? new Blob([inp]) : file
Expand All @@ -117,9 +114,6 @@ export async function docxToVFile(
...userOptions,
}

console.log(input)
console.log(input instanceof Blob)

const { entries } = await unzip(input)
const rels = await entries['word/_rels/document.xml.rels'].text()
const relations = Object.fromEntries(
Expand Down
2 changes: 1 addition & 1 deletion libs/reoff/docx-to-vfile/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"declaration": true,
"types": ["node"]
},
"exclude": ["**/*.spec.ts", "**/*.test.ts", "jest.config.ts"],
"exclude": ["**/*.spec.ts", "**/*.test.ts", "jest.config.ts", "vite.config.ts"],
"include": ["**/*.ts"]
}
2 changes: 1 addition & 1 deletion libs/reoff/docx-to-vfile/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default defineConfig({
cache: {
dir: '../../../node_modules/.vitest',
},
environment: 'jsdom',
environment: 'node',
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
},
})

0 comments on commit 8fbd4d1

Please sign in to comment.