From 1f5fc4f489db29f53c0381eec9fc3256ac40921a Mon Sep 17 00:00:00 2001 From: whale4113 Date: Wed, 11 Dec 2024 14:12:49 +0000 Subject: [PATCH] feat(lark): support heading sequence (#23) --- .changeset/plenty-keys-remember.md | 5 +++ packages/lark/src/docx.ts | 53 +++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 .changeset/plenty-keys-remember.md diff --git a/.changeset/plenty-keys-remember.md b/.changeset/plenty-keys-remember.md new file mode 100644 index 0000000..bbfe2c3 --- /dev/null +++ b/.changeset/plenty-keys-remember.md @@ -0,0 +1,5 @@ +--- +'@dolphin/lark': minor +--- + +feat: support heading sequence diff --git a/packages/lark/src/docx.ts b/packages/lark/src/docx.ts index 899e13c..a79b2f8 100644 --- a/packages/lark/src/docx.ts +++ b/packages/lark/src/docx.ts @@ -16,6 +16,7 @@ import { isListItemContent, } from './utils/mdast' import { resolveFileDownloadUrl } from './file' +import isString from 'lodash-es/isString' declare module 'mdast' { interface ImageData { @@ -132,6 +133,23 @@ interface HeadingBlock extends Block { | BlockType.HEADING8 | BlockType.HEADING9 depth: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 + snapshot: { + type: + | BlockType.HEADING1 + | BlockType.HEADING2 + | BlockType.HEADING3 + | BlockType.HEADING4 + | BlockType.HEADING5 + | BlockType.HEADING6 + | BlockType.HEADING7 + | BlockType.HEADING8 + | BlockType.HEADING9 + /** + * sequence value + */ + seq?: string | 'auto' + seq_level?: string | 'auto' + } } interface CodeBlock extends Block { @@ -709,6 +727,10 @@ export class Transformer { * Resource link to file. */ private files: mdast.Link[] = [] + /** + * heading sequence state + */ + private sequences: (string | undefined)[] = [] constructor( public options: TransformerOptions = { whiteboard: false, file: false }, @@ -799,13 +821,41 @@ export class Transformer { case BlockType.HEADING4: case BlockType.HEADING5: case BlockType.HEADING6: { + const depth = Number(block.type.at(-1)) as mdast.Heading['depth'] + const heading: mdast.Heading = { type: 'heading', - depth: Number(block.type.at(-1)) as mdast.Heading['depth'], + depth, children: transformOperationsToPhrasingContents( block.zoneState?.content.ops ?? [], ), } + + if (typeof block.snapshot.seq === 'string') { + // reset sequences state + this.sequences = this.sequences.slice(0, depth) + + // automatic incremental sequence number + if (block.snapshot.seq === 'auto') { + const previousSequenceSibling = this.sequences[depth - 1] ?? '0' + this.sequences[depth - 1] = String( + parseInt(previousSequenceSibling, 10) + 1, + ) + } else { + this.sequences[depth - 1] = block.snapshot.seq + } + + const sequences = + block.snapshot.seq_level === 'auto' + ? this.sequences.slice(0, depth).filter(isString) + : [block.snapshot.seq] + + heading.children.unshift({ + type: 'text', + value: sequences.join('.') + (sequences.length === 1 ? '. ' : ' '), + }) + } + return heading } case BlockType.CODE: { @@ -1008,6 +1058,7 @@ export class Transformer { this.parent = null this.images = [] this.files = [] + this.sequences = [] return result }