Skip to content

Commit

Permalink
feat: add relationmapping for vfile
Browse files Browse the repository at this point in the history
  • Loading branch information
tefkah committed Feb 15, 2022
1 parent d4b5703 commit e4662b5
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 0 deletions.
Binary file not shown.
3 changes: 3 additions & 0 deletions libs/processors/docx-to-tex/src/test/fixtures/image/index.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
\documentclass{article}
\usepackage[style=apa]{biblatex}
\addbibresource{bibliography.bib}\usepackage{graphicx}\usepackage{hyperref}
15 changes: 15 additions & 0 deletions libs/processors/docx-to-tex/src/test/fixtures/image/result.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
\documentclass{article}
\usepackage[style=apa]{biblatex}
\addbibresource{bibliography.bib}\usepackage{graphicx}\usepackage{hyperref}



\begin{document}



Figure 1: A pretty pic



\end{document}
92 changes: 92 additions & 0 deletions libs/processors/docx-to-tex/src/test/fixtures/image/test.jats.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
{
"type": "root",
"attributes": {},
"children": [
{
"type": "instruction",
"name": "xml",
"value": "version=\"1.0\" encoding=\"utf-8\""
},
{
"type": "doctype",
"name": "article",
"public": "-//NLM//DTD JATS (Z39.96) Journal Publishing DTD v1.2 20190208//EN",
"system": "https://jats.nlm.nih.gov/publishing/1.2/JATS-journalpublishing1.dtd"
},
{
"type": "text",
"value": " "
},
{
"type": "element",
"name": "article",
"attributes": {},
"children": [
{
"type": "element",
"name": "front",
"attributes": {},
"children": []
},
{
"type": "element",
"name": "body",
"attributes": {},
"children": [
{
"type": "element",
"name": "p",
"attributes": {},
"children": [
{
"type": "text",
"value": ""
}
]
},
{
"type": "element",
"name": "p",
"attributes": {
"style": "Caption"
},
"children": [
{
"type": "text",
"value": "Figure "
},
{
"type": "text",
"value": "1"
},
{
"type": "text",
"value": ": A pretty pic"
}
]
}
]
},
{
"type": "element",
"name": "back",
"attributes": {},
"children": [
{
"type": "element",
"name": "refList",
"attributes": {},
"children": []
},
{
"type": "element",
"name": "fnGroup",
"attributes": {},
"children": []
}
]
}
]
}
]
}
153 changes: 153 additions & 0 deletions libs/processors/docx-to-tex/src/test/fixtures/image/test.tex.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
{
"type": "root",
"children": [
{
"type": "text",
"value": " "
},
{
"type": "root",
"children": [
{
"type": "preamble",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "command",
"name": "documentclass",
"children": [
{
"type": "commandArg",
"optional": false,
"children": [
{
"type": "text",
"value": "article"
}
]
}
]
},
{
"type": "text",
"value": "\n"
},
{
"type": "command",
"name": "usepackage",
"children": [
{
"type": "commandArg",
"optional": true,
"children": [
{
"type": "text",
"value": "style=apa"
}
]
},
{
"type": "commandArg",
"optional": false,
"children": [
{
"type": "text",
"value": "biblatex"
}
]
}
]
},
{
"type": "text",
"value": "\n"
},
{
"type": "command",
"name": "addbibresource",
"children": [
{
"type": "commandArg",
"optional": false,
"children": [
{
"type": "text",
"value": "bibliography.bib"
}
]
}
]
},
{
"type": "command",
"name": "usepackage",
"children": [
{
"type": "commandArg",
"optional": false,
"children": [
{
"type": "text",
"value": "graphicx"
}
]
}
]
},
{
"type": "command",
"name": "usepackage",
"children": [
{
"type": "commandArg",
"optional": false,
"children": [
{
"type": "text",
"value": "hyperref"
}
]
}
]
},
{
"type": "text",
"value": "\n"
}
]
}
]
},
{
"type": "environment",
"name": "document",
"children": [
{
"type": "paragraph",
"children": []
},
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "Figure "
},
{
"type": "text",
"value": "1"
},
{
"type": "text",
"value": ": A pretty pic"
}
]
}
]
}
]
}
]
}
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ describe('reoffDocxToVfile', () => {
fs.writeFileSync(path.join(__dirname, 'test'), String(vfile))
expect(vfile).toMatchSnapshot()
})
it('should contain have vfile with relations data object', async () => {
const vfile = await docxToVFile(new Uint8Array(doc))
fs.writeFileSync(path.join(__dirname, 'test'), String(vfile))
expect(vfile.data.relations).toBeDefined()
})
})
9 changes: 9 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
Expand Up @@ -8,6 +8,14 @@ const removeCarriage = (text: string | undefined) =>
text ? text.replace(/\r/, '') : ''
export async function docxToVFile(file: ArrayBuffer) {
const { entries } = await unzip(file)
const rels = await entries['word/_rels/document.xml.rels'].text()
const relations = Object.fromEntries(
[...rels.matchAll(/Id="(.*?)".*?Target="(.*?)"/g)].map((match) => [
match[0],
match[1],
])
)

const doc = await entries['word/document.xml'].text()
const foot = (await entries?.['word/footnotes.xml']?.text()) || ''
const bib = (await entries?.['customXml/item1.xml']?.text()) || ''
Expand All @@ -29,6 +37,7 @@ export async function docxToVFile(file: ArrayBuffer) {
${removeHeader(bib)}
</w:document>`
const vfile = new VFile(total)
vfile.data.relations = relations
// if (footnotes) {
// Object.assign(vfile.data, { footnotes })
// }
Expand Down

0 comments on commit e4662b5

Please sign in to comment.