Skip to content

Commit 015c6ae

Browse files
authored
Merge pull request #35 from whale4113/fix/lujunji/not-defined
fix(lark): attributes not defined (#34)
2 parents a8de18a + 358cf17 commit 015c6ae

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

.changeset/chatty-yaks-sell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@dolphin/lark': patch
3+
---
4+
5+
fix: Cannot read properties of undefined (reading 'fixEnter')

packages/lark/src/docx.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ interface Attributes {
9090
}
9191

9292
interface Operation {
93-
attributes: Attributes
93+
attributes?: Attributes
9494
insert: string
9595
}
9696

@@ -509,9 +509,22 @@ export const transformOperationsToPhrasingContents = (
509509
ops: Operation[],
510510
): mdast.PhrasingContent[] => {
511511
const operations = ops
512-
.filter(operation => !operation.attributes.fixEnter)
512+
.filter(operation => {
513+
if (
514+
isDefined(operation.attributes) &&
515+
isDefined(operation.attributes.fixEnter)
516+
) {
517+
return false
518+
}
519+
520+
if (!isDefined(operation.attributes) && operation.insert === '\n') {
521+
return false
522+
}
523+
524+
return true
525+
})
513526
.map(op => {
514-
if (op.attributes['inline-component']) {
527+
if (isDefined(op.attributes) && op.attributes['inline-component']) {
515528
try {
516529
const inlineComponent = JSON.parse(op.attributes['inline-component'])
517530
if (inlineComponent.type === 'mention_doc') {
@@ -521,7 +534,7 @@ export const transformOperationsToPhrasingContents = (
521534
link: inlineComponent.data.raw_url,
522535
},
523536
insert: op.insert + inlineComponent.data.title,
524-
}
537+
} as Operation
525538
}
526539

527540
return op
@@ -533,7 +546,7 @@ export const transformOperationsToPhrasingContents = (
533546
return op
534547
})
535548

536-
let indexToMarks = operations.map(({ attributes }) => {
549+
let indexToMarks = operations.map(({ attributes = {} }) => {
537550
type SupportAttrName = 'italic' | 'bold' | 'strikethrough' | 'link'
538551

539552
const isSupportAttr = (attr: string): attr is SupportAttrName =>
@@ -593,7 +606,7 @@ export const transformOperationsToPhrasingContents = (
593606
op: Operation,
594607
): mdast.Text | mdast.InlineCode | InlineMath => {
595608
const { attributes, insert } = op
596-
const { inlineCode, equation } = attributes
609+
const { inlineCode, equation } = attributes ?? {}
597610

598611
if (inlineCode) {
599612
return {
@@ -624,7 +637,7 @@ export const transformOperationsToPhrasingContents = (
624637
mark === 'link'
625638
? {
626639
type: mark,
627-
url: decodeURIComponent(op.attributes.link ?? ''),
640+
url: decodeURIComponent(op.attributes?.link ?? ''),
628641
children: [node],
629642
}
630643
: {

packages/lark/tests/docx.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,21 @@ describe('transformOperationsToPhrasingContents()', () => {
674674
])
675675
})
676676
})
677+
678+
describe('attributes not defined', () => {
679+
test('text', () => {
680+
expect(
681+
transformOperationsToPhrasingContents([
682+
{
683+
insert: 'emphasis',
684+
},
685+
{
686+
insert: '\n',
687+
},
688+
]),
689+
).toStrictEqual([{ type: 'text', value: 'emphasis' }])
690+
})
691+
})
677692
})
678693

679694
describe('transformer.transform()', () => {

0 commit comments

Comments
 (0)