@jacobbubu/md-to-lark 1.4.0 → 1.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -511,7 +511,7 @@ export function isRelationMismatchError(error) {
511
511
  return false;
512
512
  return /1770013|relation mismatch/i.test(error.message);
513
513
  }
514
- export async function replaceImageBlock(client, documentId, blockId, imageToken, authOptions, limiter) {
514
+ export async function replaceImageBlock(client, documentId, blockId, imageToken, imageOptions, authOptions, limiter) {
515
515
  await limiter.wait();
516
516
  const response = await withRetry('docx.documentBlock.batchUpdate(replace_image)', async () => client.docx.documentBlock.batchUpdate({
517
517
  path: {
@@ -523,6 +523,11 @@ export async function replaceImageBlock(client, documentId, blockId, imageToken,
523
523
  block_id: blockId,
524
524
  replace_image: {
525
525
  token: imageToken,
526
+ ...(typeof imageOptions.width === 'number' ? { width: imageOptions.width } : {}),
527
+ ...(typeof imageOptions.height === 'number' ? { height: imageOptions.height } : {}),
528
+ ...(typeof imageOptions.align === 'number' ? { align: imageOptions.align } : {}),
529
+ ...(imageOptions.caption ? { caption: imageOptions.caption } : {}),
530
+ ...(typeof imageOptions.scale === 'number' ? { scale: imageOptions.scale } : {}),
526
531
  },
527
532
  },
528
533
  ],
@@ -25,16 +25,23 @@ export async function applyCreatedImageBlock(client, documentId, createdBlockId,
25
25
  const localPath = image && typeof image.local_path === 'string' ? image.local_path : '';
26
26
  if (!localPath)
27
27
  return null;
28
+ const replaceOptions = {
29
+ ...(image && typeof image.width === 'number' ? { width: image.width } : {}),
30
+ ...(image && typeof image.height === 'number' ? { height: image.height } : {}),
31
+ ...(image && typeof image.align === 'number' ? { align: image.align } : {}),
32
+ ...(image && toObjectRecord(image.caption) ? { caption: toObjectRecord(image.caption) } : {}),
33
+ ...(image && typeof image.scale === 'number' ? { scale: image.scale } : {}),
34
+ };
28
35
  let imageToken = await uploadBinaryToNode(client, 'docx_image', createdBlockId, localPath, authOptions, mediaLimiter);
29
36
  try {
30
- await replaceImageBlock(client, documentId, createdBlockId, imageToken, authOptions, docxLimiter);
37
+ await replaceImageBlock(client, documentId, createdBlockId, imageToken, replaceOptions, authOptions, docxLimiter);
31
38
  }
32
39
  catch (error) {
33
40
  if (!isRelationMismatchError(error)) {
34
41
  throw error;
35
42
  }
36
43
  imageToken = await uploadBinaryToNode(client, 'docx_image', createdBlockId, localPath, authOptions, mediaLimiter);
37
- await replaceImageBlock(client, documentId, createdBlockId, imageToken, authOptions, docxLimiter);
44
+ await replaceImageBlock(client, documentId, createdBlockId, imageToken, replaceOptions, authOptions, docxLimiter);
38
45
  }
39
46
  return {
40
47
  kind: 'image',
@@ -0,0 +1,9 @@
1
+ export const DEFAULT_IMAGE_WIDTH = 1000;
2
+ export const DEFAULT_TABLE_CELL_IMAGE_WIDTH = 240;
3
+ export function createDefaultImagePayload(parentType) {
4
+ return {
5
+ width: parentType === 'table_cell' ? DEFAULT_TABLE_CELL_IMAGE_WIDTH : DEFAULT_IMAGE_WIDTH,
6
+ token: '',
7
+ align: 'left',
8
+ };
9
+ }
@@ -1,4 +1,5 @@
1
1
  import { toString } from 'hast-util-to-string';
2
+ import { createDefaultImagePayload } from '../last/image-defaults.js';
2
3
  import { LAST_TEXTUAL_BLOCK_TYPE_SET } from '../last/textual-block-types.js';
3
4
  const BLOCK_CONTAINER_TAGS = new Set([
4
5
  'article',
@@ -146,17 +147,13 @@ function createDividerBlock(ctx, parentId) {
146
147
  }
147
148
  function createImageBlock(ctx, parentId, sourceUrl) {
148
149
  const blockId = nextBlockId(ctx);
150
+ const parentBlock = ctx.blocks[parentId];
149
151
  const blockBase = {
150
152
  id: blockId,
151
153
  type: 'image',
152
154
  parentId,
153
155
  children: [],
154
- payload: {
155
- width: 0,
156
- height: 0,
157
- token: '',
158
- align: 'left',
159
- },
156
+ payload: createDefaultImagePayload(parentBlock?.type),
160
157
  };
161
158
  if (sourceUrl) {
162
159
  blockBase.selector = { attrs: { sourceUrl } };
@@ -1,5 +1,6 @@
1
1
  import { existsSync } from 'node:fs';
2
2
  import path from 'node:path';
3
+ import { createDefaultImagePayload } from '../last/image-defaults.js';
3
4
  import { createDefaultMarks, extractTextFromInlines, firstInlineLinkUrl, getPathExtension, inferMediaKind, isTextualBlock, resolveLocalPathFromSource, shouldUsePreviewView, stripQueryAndHash, } from './common.js';
4
5
  export function applyStandaloneAttachmentTransforms(last, baseDir) {
5
6
  const localAssetByBlockId = new Map();
@@ -26,18 +27,14 @@ export function applyStandaloneAttachmentTransforms(last, baseDir) {
26
27
  const displayName = extractTextFromInlines(block.payload.inlines).trim();
27
28
  const fileName = displayName.length > 0 ? displayName : path.basename(stripQueryAndHash(linkUrl));
28
29
  if (mediaKind === 'image') {
30
+ const parentBlock = block.parentId ? last.blocks[block.parentId] : undefined;
29
31
  const transformed = {
30
32
  id: block.id,
31
33
  ...(block.bttId ? { bttId: block.bttId } : {}),
32
34
  type: 'image',
33
35
  parentId: block.parentId,
34
36
  children: [],
35
- payload: {
36
- width: 0,
37
- height: 0,
38
- token: '',
39
- align: 'left',
40
- },
37
+ payload: createDefaultImagePayload(parentBlock?.type),
41
38
  selector: {
42
39
  attrs: {
43
40
  sourceUrl: linkUrl,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jacobbubu/md-to-lark",
3
- "version": "1.4.0",
3
+ "version": "1.4.2",
4
4
  "description": "Publish Markdown to Feishu docs with a stable pipeline.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",