Skip to content

Commit babd1d7

Browse files
committed
fix(importer): edge cases in word/document entrypoint
1 parent 0ae772d commit babd1d7

File tree

2 files changed

+80
-53
lines changed

2 files changed

+80
-53
lines changed

packages/better-write-app/src/use/project.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,16 @@ export const useProject = () => {
205205
let pages = 0
206206

207207
entities.forEach((entity) => {
208-
if (entity.type === 'heading-one') {
208+
if (entity.type === 'heading-one' || chapter === null) {
209209
if (chapter) {
210210
chapters.push(chapter)
211211

212212
chapter = null
213213
}
214214

215+
// for edge case
216+
entity.type = 'heading-one'
217+
215218
pages++
216219

217220
chapter = {
@@ -316,7 +319,13 @@ export const useProject = () => {
316319
if (isDoc) {
317320
const importers = await DocxToJson(reader.result as string)
318321

319-
onLoadImporter(importers, file.name)
322+
onLoadImporter(
323+
importers,
324+
file.name
325+
.replace('.bw', '')
326+
.replace('.docx', '')
327+
.replace('.doc', '')
328+
)
320329

321330
return
322331
}

packages/better-write-importer/src/docx.ts

Lines changed: 69 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,68 +3,80 @@ import destr from 'destr'
33
import { xml2json } from 'xml-js'
44
import { EntityType, ImporterData, ImporterEntity } from 'better-write-types'
55

6-
const extractFilter = async (entries: zip.Entry[], position: number = 3) => {
6+
const extractFilter = async (
7+
entries: zip.Entry[]
8+
): Promise<any[] | undefined> => {
9+
const arr: any = []
10+
711
if (entries.length) {
8-
const target = entries[position]
9-
10-
// @ts-ignore
11-
const text = await target.getData(new zip.TextWriter())
12-
13-
const result = await xml2json(text, {
14-
compact: true,
15-
elementNameFn: (val) => {
16-
return val.replace('w:', '').replace('_', '').replace('xml:', '')
17-
},
18-
ignoreComment: true,
19-
ignoreDeclaration: true,
20-
spaces: 2,
21-
})
12+
for (const target of entries) {
13+
if (target.filename === 'word/document.xml') {
14+
// @ts-ignore
15+
const text = await target.getData(new zip.TextWriter())
16+
17+
const result = await xml2json(text, {
18+
compact: true,
19+
elementNameFn: (val) => {
20+
return val.replace('w:', '').replace('_', '').replace('xml:', '')
21+
},
22+
ignoreComment: true,
23+
ignoreDeclaration: true,
24+
spaces: 2,
25+
})
26+
27+
arr.push(destr(result))
28+
}
29+
}
2230

23-
return destr(result)
31+
return arr
2432
}
33+
34+
return undefined
2535
}
2636

27-
const DocxToBW = async (data: Record<string, any>): Promise<ImporterData> => {
37+
const DocxToBW = async (arr: Record<string, any>[]): Promise<ImporterData> => {
2838
const result: ImporterEntity[] = []
2939

30-
data.body.p.forEach((target: any) => {
31-
const style = target?.pPr?.pStyle?._attributes['w:val']
32-
const raw = target?.r?.t?._text
33-
34-
if (style && raw) {
35-
let type: EntityType | null = null
36-
37-
switch (style) {
38-
case 'Heading1':
39-
type = 'heading-one'
40-
break
41-
case 'Heading2':
42-
type = 'heading-two'
43-
break
44-
case 'Heading3':
45-
case 'Heading4':
46-
case 'Heading5':
47-
case 'Heading6':
48-
type = 'heading-three'
49-
break
40+
arr.forEach((data) => {
41+
data?.document?.body?.p?.forEach((target: any) => {
42+
const style = target?.pPr?.pStyle?._attributes['w:val']
43+
const raw = target?.r?.t?._text
44+
45+
if (style && raw) {
46+
let type: EntityType | null = null
47+
48+
switch (style) {
49+
case 'Heading1':
50+
case 'Heading2':
51+
type = 'heading-one'
52+
break
53+
case 'Heading3':
54+
case 'Heading4':
55+
type = 'heading-two'
56+
break
57+
case 'Heading5':
58+
case 'Heading6':
59+
type = 'heading-three'
60+
break
61+
}
62+
63+
if (type) {
64+
result.push({
65+
type,
66+
raw,
67+
})
68+
}
69+
70+
return
5071
}
5172

52-
if (type) {
73+
if (raw) {
5374
result.push({
54-
type,
75+
type: 'paragraph',
5576
raw,
5677
})
5778
}
58-
59-
return
60-
}
61-
62-
if (raw) {
63-
result.push({
64-
type: 'paragraph',
65-
raw,
66-
})
67-
}
79+
})
6880
})
6981

7082
return {
@@ -77,11 +89,17 @@ export const DocxToJson = async (data: string): Promise<ImporterData> => {
7789

7890
const entries = await reader.getEntries()
7991

80-
const { document } = await extractFilter(entries)
92+
const arr = await extractFilter(entries)
8193

8294
await reader.close()
8395

84-
const final = DocxToBW(document)
96+
if (!arr) {
97+
return {
98+
entities: [],
99+
}
100+
}
101+
102+
const final = DocxToBW(arr)
85103

86104
return final
87105
}

0 commit comments

Comments
 (0)