@jacobbubu/md-to-lark 1.5.0 → 1.5.1

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.
@@ -145,7 +145,7 @@ function createDividerBlock(ctx, parentId) {
145
145
  });
146
146
  return blockId;
147
147
  }
148
- function createImageBlock(ctx, parentId, sourceUrl) {
148
+ function createImageBlock(ctx, parentId, sourceUrl, alt = null) {
149
149
  const blockId = nextBlockId(ctx);
150
150
  const parentBlock = ctx.blocks[parentId];
151
151
  const blockBase = {
@@ -155,8 +155,15 @@ function createImageBlock(ctx, parentId, sourceUrl) {
155
155
  children: [],
156
156
  payload: createDefaultImagePayload(parentBlock?.type),
157
157
  };
158
+ const selectorAttrs = {};
158
159
  if (sourceUrl) {
159
- blockBase.selector = { attrs: { sourceUrl } };
160
+ selectorAttrs.sourceUrl = sourceUrl;
161
+ }
162
+ if (alt) {
163
+ selectorAttrs.alt = alt;
164
+ }
165
+ if (Object.keys(selectorAttrs).length > 0) {
166
+ blockBase.selector = { attrs: selectorAttrs };
160
167
  }
161
168
  addBlock(ctx, blockBase);
162
169
  return blockId;
@@ -358,14 +365,31 @@ function isWhitespaceTextNode(node) {
358
365
  function getMeaningfulChildren(nodes) {
359
366
  return nodes.filter((child) => !isWhitespaceTextNode(child));
360
367
  }
361
- function findStandaloneImageSrcInParagraph(paragraph) {
368
+ function extractImageSourceFromElement(element) {
369
+ if (element.tagName === 'img') {
370
+ return {
371
+ sourceUrl: getStringProp(element, 'src'),
372
+ alt: getStringProp(element, 'alt'),
373
+ };
374
+ }
375
+ if (element.tagName !== 'a')
376
+ return null;
377
+ const meaningfulChildren = getMeaningfulChildren(getChildren(element));
378
+ if (meaningfulChildren.length !== 1)
379
+ return null;
380
+ const only = meaningfulChildren[0];
381
+ if (!only || !isElement(only))
382
+ return null;
383
+ return extractImageSourceFromElement(only);
384
+ }
385
+ function findStandaloneImageInParagraph(paragraph) {
362
386
  const meaningfulChildren = getMeaningfulChildren(getChildren(paragraph));
363
387
  if (meaningfulChildren.length !== 1)
364
388
  return null;
365
389
  const only = meaningfulChildren[0];
366
- if (!only || !isElement(only) || only.tagName !== 'img')
390
+ if (!only || !isElement(only))
367
391
  return null;
368
- return getStringProp(only, 'src');
392
+ return extractImageSourceFromElement(only);
369
393
  }
370
394
  function parseHttpUrl(url) {
371
395
  try {
@@ -472,10 +496,11 @@ function findStandaloneRichItemInTableCell(cell) {
472
496
  }
473
497
  if (!only || !isElement(only))
474
498
  return null;
475
- if (only.tagName === 'img') {
499
+ const imageSource = extractImageSourceFromElement(only);
500
+ if (imageSource) {
476
501
  return {
477
502
  kind: 'image',
478
- sourceUrl: getStringProp(only, 'src'),
503
+ ...imageSource,
479
504
  };
480
505
  }
481
506
  if (only.tagName !== 'a')
@@ -667,7 +692,7 @@ function convertTable(ctx, table, parentId) {
667
692
  const cellBlock = ctx.blocks[cellId];
668
693
  const richItem = cell ? findStandaloneRichItemInTableCell(cell) : null;
669
694
  if (cellBlock?.type === 'table_cell' && richItem?.kind === 'image') {
670
- const imageId = createImageBlock(ctx, cellId, richItem.sourceUrl);
695
+ const imageId = createImageBlock(ctx, cellId, richItem.sourceUrl, richItem.alt);
671
696
  cellBlock.children = [imageId];
672
697
  cells.push(cellId);
673
698
  continue;
@@ -733,6 +758,44 @@ function convertUnknownElement(ctx, element, parentId) {
733
758
  ]),
734
759
  ];
735
760
  }
761
+ function hasNonWhitespaceInline(inlines) {
762
+ return inlines.some((inline) => toSearchText(inline).text.trim().length > 0);
763
+ }
764
+ function convertParagraph(ctx, paragraph, parentId) {
765
+ const standaloneImage = findStandaloneImageInParagraph(paragraph);
766
+ if (standaloneImage?.sourceUrl) {
767
+ return [createImageBlock(ctx, parentId, standaloneImage.sourceUrl, standaloneImage.alt)];
768
+ }
769
+ const standaloneIframe = findStandaloneIframePayloadInParagraph(paragraph);
770
+ if (standaloneIframe) {
771
+ return [createIframeBlock(ctx, parentId, standaloneIframe.url, standaloneIframe.iframeType)];
772
+ }
773
+ const ids = [];
774
+ let pendingInlineNodes = [];
775
+ const flushText = () => {
776
+ if (pendingInlineNodes.length === 0)
777
+ return;
778
+ const inlines = parseInlineNodes(ctx, pendingInlineNodes);
779
+ pendingInlineNodes = [];
780
+ if (!hasNonWhitespaceInline(inlines))
781
+ return;
782
+ ids.push(createTextualBlock(ctx, 'text', parentId, inlines));
783
+ };
784
+ for (const child of getChildren(paragraph)) {
785
+ const image = isElement(child) ? extractImageSourceFromElement(child) : null;
786
+ if (image?.sourceUrl) {
787
+ flushText();
788
+ ids.push(createImageBlock(ctx, parentId, image.sourceUrl, image.alt));
789
+ continue;
790
+ }
791
+ pendingInlineNodes.push(child);
792
+ }
793
+ flushText();
794
+ if (ids.length > 0) {
795
+ return ids;
796
+ }
797
+ return [createTextualBlock(ctx, 'text', parentId, parseInlineNodes(ctx, getChildren(paragraph)))];
798
+ }
736
799
  function convertBlock(ctx, node, parentId) {
737
800
  if (isWhitespaceTextNode(node)) {
738
801
  return [];
@@ -753,17 +816,8 @@ function convertBlock(ctx, node, parentId) {
753
816
  return [];
754
817
  }
755
818
  switch (node.tagName) {
756
- case 'p': {
757
- const standaloneImageSrc = findStandaloneImageSrcInParagraph(node);
758
- if (standaloneImageSrc) {
759
- return [createImageBlock(ctx, parentId, standaloneImageSrc)];
760
- }
761
- const standaloneIframe = findStandaloneIframePayloadInParagraph(node);
762
- if (standaloneIframe) {
763
- return [createIframeBlock(ctx, parentId, standaloneIframe.url, standaloneIframe.iframeType)];
764
- }
765
- return [createTextualBlock(ctx, 'text', parentId, parseInlineNodes(ctx, getChildren(node)))];
766
- }
819
+ case 'p':
820
+ return convertParagraph(ctx, node, parentId);
767
821
  case 'h1':
768
822
  case 'h2':
769
823
  case 'h3':
@@ -789,7 +843,7 @@ function convertBlock(ctx, node, parentId) {
789
843
  case 'table':
790
844
  return convertTable(ctx, node, parentId);
791
845
  case 'img':
792
- return [createImageBlock(ctx, parentId, getStringProp(node, 'src'))];
846
+ return [createImageBlock(ctx, parentId, getStringProp(node, 'src'), getStringProp(node, 'alt'))];
793
847
  case 'br':
794
848
  return [
795
849
  createTextualBlock(ctx, 'text', parentId, [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jacobbubu/md-to-lark",
3
- "version": "1.5.0",
3
+ "version": "1.5.1",
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",