Skip to content

Commit 9207df8

Browse files
authored
Merge pull request #102 from jiyee/bugfix-text-highlight-missing-in-invalid-children
fix(lark): text highlight missing in invalid children of table cell
2 parents fc201d5 + f9d81ef commit 9207df8

File tree

6 files changed

+38
-15
lines changed

6 files changed

+38
-15
lines changed

.changeset/shy-taxes-chew.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: text highlight missing in invalid children of table cell

apps/chrome-extension/src/common/utils.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export class UniqueFileName {
7575

7676
export const transformInvalidTablesToHtml = (
7777
invalidTables: InvalidTable[],
78+
options: { allowDangerousHtml: boolean } = { allowDangerousHtml: false },
7879
): void => {
7980
invalidTables.forEach(invalidTable => {
8081
const invalidTableIndex = invalidTable.parent?.children.findIndex(
@@ -84,17 +85,25 @@ export const transformInvalidTablesToHtml = (
8485
invalidTable.parent?.children.splice(invalidTableIndex, 1, {
8586
type: 'html',
8687
value: toHtml(
87-
toHast({
88-
...invalidTable.inner,
89-
// @ts-expect-error non-phrasing content can be supported.
90-
children: invalidTable.inner.children.map(row => ({
91-
...row,
92-
children: row.children.map(cell => ({
93-
...cell,
94-
children: cell.data?.invalidChildren ?? cell.children,
88+
toHast(
89+
{
90+
...invalidTable.inner,
91+
// @ts-expect-error non-phrasing content can be supported.
92+
children: invalidTable.inner.children.map(row => ({
93+
...row,
94+
children: row.children.map(cell => ({
95+
...cell,
96+
children: cell.data?.invalidChildren ?? cell.children,
97+
})),
9598
})),
96-
})),
97-
}),
99+
},
100+
{
101+
allowDangerousHtml: options.allowDangerousHtml,
102+
},
103+
),
104+
{
105+
allowDangerousHtml: options.allowDangerousHtml,
106+
},
98107
),
99108
})
100109
}

apps/chrome-extension/src/scripts/copy-lark-docx-as-markdown.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ const main = async () => {
9292
settings[SettingKey.TableWithNonPhrasingContent] ===
9393
TableWithNonPhrasingContent.ToHTML
9494
) {
95-
transformInvalidTablesToHtml(invalidTables)
95+
transformInvalidTablesToHtml(invalidTables, {
96+
allowDangerousHtml: true,
97+
})
9698
}
9799

98100
const markdown = Docx.stringify(root)

apps/chrome-extension/src/scripts/download-lark-docx-as-markdown.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,9 @@ const main = async (options: { signal?: AbortSignal } = {}) => {
526526
settings[SettingKey.TableWithNonPhrasingContent] ===
527527
TableWithNonPhrasingContent.ToHTML
528528
) {
529-
transformInvalidTablesToHtml(invalidTables)
529+
transformInvalidTablesToHtml(invalidTables, {
530+
allowDangerousHtml: true,
531+
})
530532
}
531533

532534
const markdown = Docx.stringify(root)
@@ -588,7 +590,9 @@ const main = async (options: { signal?: AbortSignal } = {}) => {
588590
settings[SettingKey.TableWithNonPhrasingContent] ===
589591
TableWithNonPhrasingContent.ToHTML
590592
) {
591-
transformInvalidTablesToHtml(invalidTables)
593+
transformInvalidTablesToHtml(invalidTables, {
594+
allowDangerousHtml: true,
595+
})
592596
}
593597

594598
const markdown = Docx.stringify(root)

apps/chrome-extension/src/scripts/view-lark-docx-as-markdown.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ const main = async () => {
9494
settings[SettingKey.TableWithNonPhrasingContent] ===
9595
TableWithNonPhrasingContent.ToHTML
9696
) {
97-
transformInvalidTablesToHtml(invalidTables)
97+
transformInvalidTablesToHtml(invalidTables, {
98+
allowDangerousHtml: true,
99+
})
98100
}
99101

100102
const markdown = Docx.stringify(root)

packages/lark/src/docx.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
} from './utils/mdast'
2525
import { resolveFileDownloadUrl } from './file'
2626
import isString from 'lodash-es/isString'
27+
import escape from 'lodash-es/escape'
2728

2829
declare module 'mdast' {
2930
interface ImageData {
@@ -720,7 +721,7 @@ export const transformOperationsToPhrasingContents = (
720721
if (options.highlight && (textHighlight || textHighlightBackground)) {
721722
return {
722723
type: 'html',
723-
value: `<span style="color: ${textHighlight ?? 'inherit'}; background-color: ${textHighlightBackground ?? 'inherit'}">${insert}</span>`,
724+
value: `<span style="color: ${textHighlight ?? 'inherit'}; background-color: ${textHighlightBackground ?? 'inherit'}">${escape(insert)}</span>`,
724725
}
725726
}
726727

0 commit comments

Comments
 (0)