-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: better unzip lib that's browser compatible
- Loading branch information
Showing
6 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
33 changes: 33 additions & 0 deletions
33
libs/reoff/docx-to-vfile/src/lib/__snapshots__/docx-to-vfile-unzipit.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
libs/reoff/docx-to-vfile/src/lib/docx-to-vfile-unzipit.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters