Skip to content

Commit

Permalink
feat: better unzip lib that's browser compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
tefkah committed Feb 14, 2022
1 parent 53f8f03 commit 0646a95
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 1 deletion.
2 changes: 1 addition & 1 deletion libs/reoff/docx-to-vfile/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './lib/get-xml-data'
export * from './lib/docx-to-vfile-yauzl'
export * from './lib/docx-to-vfile-unzipit'
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`reoffDocxToVfile should work 1`] = `
VFile {
"cwd": "/Users/thomas/Projects/jote-monorepo",
"data": Object {},
"history": Array [],
"messages": Array [],
"value": "[object
[
o
b
j
e
c
t
A
r
r
a
y
B
u
f
f
e
r
]
</w:document>",
}
`;
15 changes: 15 additions & 0 deletions libs/reoff/docx-to-vfile/src/lib/docx-to-vfile-unzipit.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { docxToVFile } from './docx-to-vfile-unzipit'
import fs from 'fs'
import path from 'path'

describe('reoffDocxToVfile', () => {
const doc = fs.readFileSync(
path.join(__dirname, '../../../reoff-parse/src/test/word-citation.docx')
)
jest.setTimeout(10000)
it('should work', async () => {
const vfile = await docxToVFile(new Uint8Array(doc))
console.log(vfile)
expect(vfile).toMatchSnapshot()
})
})
39 changes: 39 additions & 0 deletions libs/reoff/docx-to-vfile/src/lib/docx-to-vfile-unzipit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { VFile } from 'vfile'
import { unzip } from 'unzipit'

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

export async function docxToVFile(file: ArrayBuffer) {
const { entries } = await unzip(file)
console.log(entries['word/document.xml'])
const doc = (await entries['word/document.xml'].arrayBuffer()).toString()
const foot =
(await entries?.['word/footnotes.xml']?.arrayBuffer())?.toString() || ''
const bib =
(await entries?.['customXml/item1.xml']?.arrayBuffer())?.toString() || ''
// const data = await getXMLDatas(file, {
// filenames: [
// /customXml\/item\d+\.xml/,
// 'word/document.xml',
// 'word/footnotes.xml',
// ],
// })

// const {
// 'word/document.xml': document,
// 'word/footnotes.xml': footnotes,
// ...bibliography
// } = data
const total = `${removeHeader(doc).slice(0, -'</w:document>'.length)}
${removeHeader(foot)}
${Object.values(bib)
.map((bib) => removeHeader(bib))
.join('\n')}
</w:document>`
const vfile = new VFile(total)
// if (footnotes) {
// Object.assign(vfile.data, { footnotes })
// }
return vfile
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"unist-util-remove": "^3.0.0",
"unist-util-select": "^4.0.1",
"unist-util-visit": "4.0.0",
"unzipit": "^1.4.0",
"utility-types": "^3.10.0",
"vfile": "^5.3.0",
"xast-util-from-xml": "^2.0.1",
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12542,6 +12542,13 @@ untildify@^4.0.0:
resolved "https://registry.yarnpkg.com/untildify/-/untildify-4.0.0.tgz#2bc947b953652487e4600949fb091e3ae8cd919b"
integrity sha512-KK8xQ1mkzZeg9inewmFVDNkg3l5LUhoq9kN6iWYB/CC9YMG8HA+c1Q8HwDe6dEX7kErrEVNVBO3fWsVq5iDgtw==

unzipit@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/unzipit/-/unzipit-1.4.0.tgz#69fc6eef47730897e39b15e54f972d60a1a92441"
integrity sha512-hjoB8j1igXJgmxqaAvqkIW+faKTpG9cPK6QvkBhNCyd8OPWqODXTBVqTEmZbz62K5J/dX4Xa8lTa0NRikQwSjQ==
dependencies:
uzip-module "^1.0.2"

upath2@^3.1.12:
version "3.1.12"
resolved "https://registry.yarnpkg.com/upath2/-/upath2-3.1.12.tgz#441b3dfbadde21731017bd1b7beb169498efd0a9"
Expand Down Expand Up @@ -12664,6 +12671,11 @@ uvu@^0.5.0:
kleur "^4.0.3"
sade "^1.7.3"

uzip-module@^1.0.2:
version "1.0.3"
resolved "https://registry.yarnpkg.com/uzip-module/-/uzip-module-1.0.3.tgz#6bbabe2a3efea5d5a4a47479f523a571de3427ce"
integrity sha512-AMqwWZaknLM77G+VPYNZLEruMGWGzyigPK3/Whg99B3S6vGHuqsyl5ZrOv1UUF3paGK1U6PM0cnayioaryg/fA==

v8-compile-cache@2.3.0, v8-compile-cache@^2.0.3:
version "2.3.0"
resolved "https://registry.yarnpkg.com/v8-compile-cache/-/v8-compile-cache-2.3.0.tgz#2de19618c66dc247dcfb6f99338035d8245a2cee"
Expand Down

0 comments on commit 0646a95

Please sign in to comment.