Update npm pkg (#39)

* fix: export read docx

* fix: numberings
main
bokuweb 2020-02-13 02:57:48 +09:00 committed by GitHub
parent 93b21cee78
commit 1ab91a6924
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 22 additions and 18 deletions

View File

@ -3,7 +3,7 @@ use std::fs::*;
use std::io::Read;
pub fn main() {
let mut file = File::open("./fixtures/paragraph/paragraph.docx").unwrap();
let mut file = File::open("./run.docx").unwrap();
let mut buf = vec![];
file.read_to_end(&mut buf).unwrap();
dbg!(read_docx(&buf).unwrap().json());

View File

@ -16,6 +16,8 @@ pub enum ReaderError {
DocumentRelsNotFoundError,
#[error("Failed to find styles.")]
DocumentStylesNotFoundError,
#[error("Failed to find numberings.")]
DocumentNumberingsNotFoundError,
#[error("Unknown error")]
Unknown,
}

View File

@ -54,27 +54,25 @@ pub fn read_docx(buf: &[u8]) -> Result<Docx, ReaderError> {
.ok_or(ReaderError::DocumentNotFoundError)?;
let document_xml = archive.by_name(&main_rel.2)?;
let document = Document::from_xml(document_xml)?;
let mut docx = Docx::new().document(document);
// Read document relationships
let rels = read_document_rels(&mut archive, &main_rel.2)?;
// Read styles
let style_path = rels
.find_target_path(STYLE_RELATIONSHIP_TYPE)
.ok_or(ReaderError::DocumentStylesNotFoundError)?;
let style_path = rels.find_target_path(STYLE_RELATIONSHIP_TYPE);
if let Some(style_path) = style_path {
let styles_xml = archive.by_name(style_path.to_str().expect("should have styles"))?;
let styles = Styles::from_xml(styles_xml)?;
docx = docx.styles(styles);
}
// Read numberings
let num_path = rels
.find_target_path(NUMBERING_RELATIONSHIP_TYPE)
.ok_or(ReaderError::DocumentStylesNotFoundError)?;
let num_path = rels.find_target_path(NUMBERING_RELATIONSHIP_TYPE);
if let Some(num_path) = num_path {
let num_xml = archive.by_name(num_path.to_str().expect("should have numberings"))?;
let nums = Numberings::from_xml(num_xml)?;
docx = docx.numberings(nums);
}
let docx = Docx::new()
.document(document)
.styles(styles)
.numberings(nums);
Ok(docx)
}

View File

@ -286,6 +286,10 @@ export class Docx {
}
}
export const readDocx = (buf: Uint8Array) => {
return wasm.readDocx(buf);
};
export * from "./paragraph";
export * from "./insert";
export * from "./delete";

View File

@ -1,6 +1,6 @@
{
"name": "docx-wasm",
"version": "0.0.22",
"version": "0.0.23",
"main": "dist/index.js",
"author": "bokuweb <bokuweb12@gmail.com>",
"license": "MIT",

View File

@ -1,8 +1,8 @@
use docx_rs;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(js_name=readError)]
#[allow(non_snake_case)]
#[wasm_bindgen]
pub fn readDocx(buf: &[u8]) -> Result<String, JsValue> {
let mut d = docx_rs::read_docx(buf);
match d {