@alemonjs/kook 2.1.1 → 2.1.3

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.
package/README.md CHANGED
@@ -24,4 +24,11 @@ kook:
24
24
  # 使用 user_id
25
25
  master_id:
26
26
  - 'yyy'
27
+ # 隐藏不支持的消息类型(可选,默认: false)
28
+ # 1:一级隐藏,不可读占位符([视频]、[音频]、[图片]、[附件]等)被置空,可读内容保留
29
+ # 2:二级隐藏,按钮仅显示指令数据(如 /挑战),链接仅显示 URL
30
+ # 3:三级隐藏,按钮和链接的 data 也不保留,完全隐藏
31
+ # 4:四级隐藏,不进行任何转换,降级数据直接丢弃
32
+ # 转换后内容为空时,将跳过发送并输出 info 日志
33
+ hideUnsupported: 1
27
34
  ```
package/lib/config.d.ts CHANGED
@@ -3,6 +3,7 @@ export type Options = {
3
3
  token: string;
4
4
  master_key?: string[];
5
5
  master_id?: string[];
6
+ hideUnsupported?: boolean | number;
6
7
  };
7
8
  export declare const getKOOKConfig: () => Options;
8
9
  export declare const getMaster: (UserId: string) => readonly [boolean, string];
package/lib/format.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import type { DataEnums, DataMarkDown } from 'alemonjs';
2
- export declare const markdownToKMarkdown: (items: DataMarkDown["value"]) => string;
3
- export declare const markdownRawToKMarkdown: (raw: string) => string;
4
- export declare const dataEnumToKMarkdown: (item: DataEnums) => string;
2
+ export declare const markdownToKMarkdown: (items: DataMarkDown["value"], hideUnsupported?: boolean | number) => string;
3
+ export declare const markdownRawToKMarkdown: (raw: string, hideUnsupported?: boolean | number) => string;
4
+ export declare const dataEnumToKMarkdown: (item: DataEnums, hideUnsupported?: boolean | number) => string;
package/lib/format.js CHANGED
@@ -1,4 +1,6 @@
1
- const markdownToKMarkdown = (items) => {
1
+ const markdownToKMarkdown = (items, hideUnsupported) => {
2
+ if (Number(hideUnsupported) >= 4)
3
+ return '';
2
4
  return items
3
5
  .map(item => {
4
6
  switch (item.type) {
@@ -17,10 +19,12 @@ const markdownToKMarkdown = (items) => {
17
19
  return `~~${item.value}~~`;
18
20
  case 'MD.link': {
19
21
  const v = item.value;
20
- return `[${v.text}](${v.url})`;
22
+ if (Number(hideUnsupported) >= 3)
23
+ return '';
24
+ return Number(hideUnsupported) >= 2 ? v.url : `[${v.text}](${v.url})`;
21
25
  }
22
26
  case 'MD.image':
23
- return '[图片]';
27
+ return hideUnsupported ? '' : '[图片]';
24
28
  case 'MD.list':
25
29
  return (item.value
26
30
  .map(li => {
@@ -33,7 +37,7 @@ const markdownToKMarkdown = (items) => {
33
37
  case 'MD.blockquote':
34
38
  return `> ${item.value}\n`;
35
39
  case 'MD.divider':
36
- return '---\n';
40
+ return hideUnsupported ? '' : '---\n';
37
41
  case 'MD.newline':
38
42
  return '\n';
39
43
  case 'MD.code': {
@@ -48,35 +52,43 @@ const markdownToKMarkdown = (items) => {
48
52
  case 'MD.content':
49
53
  return item.value;
50
54
  case 'MD.button':
51
- return `[${item.value}]`;
55
+ if (Number(hideUnsupported) >= 3)
56
+ return '';
57
+ if (Number(hideUnsupported) >= 2) {
58
+ return item.options?.data || String(item.value);
59
+ }
60
+ return hideUnsupported ? String(item.value) : `[${item.value}]`;
52
61
  default:
53
62
  return String(item?.value ?? '');
54
63
  }
55
64
  })
56
- .join('');
65
+ .join('')
66
+ .trim();
57
67
  };
58
- const markdownRawToKMarkdown = (raw) => {
68
+ const markdownRawToKMarkdown = (raw, hideUnsupported) => {
69
+ if (Number(hideUnsupported) >= 4)
70
+ return '';
59
71
  let text = raw;
60
- text = text.replace(/!\[([^\]]*)\]\([^)]*\)/g, '[图片]');
72
+ text = text.replace(/!\[([^\]]*)\]\([^)]*\)/g, hideUnsupported ? '' : '[图片]');
61
73
  return text;
62
74
  };
63
- const dataEnumToKMarkdown = (item) => {
75
+ const dataEnumToKMarkdown = (item, hideUnsupported) => {
76
+ if (Number(hideUnsupported) >= 4)
77
+ return '';
64
78
  switch (item.type) {
65
79
  case 'Markdown':
66
- return markdownToKMarkdown(item.value);
80
+ return markdownToKMarkdown(item.value, hideUnsupported);
67
81
  case 'MarkdownOriginal':
68
- return markdownRawToKMarkdown(String(item.value));
82
+ return markdownRawToKMarkdown(String(item.value), hideUnsupported);
69
83
  case 'BT.group':
70
84
  case 'ButtonGroup':
71
- return item.value
72
- .map((row) => row.value.map((btn) => `[${btn.value}]`).join(' '))
73
- .join('\n');
85
+ return hideUnsupported ? '' : item.value.map((row) => row.value.map((btn) => `[${btn.value}]`).join(' ')).join('\n');
74
86
  case 'Attachment':
75
- return `[附件${item.options?.filename ? ': ' + item.options.filename : ''}]`;
87
+ return hideUnsupported ? '' : `[附件${item.options?.filename ? ': ' + item.options.filename : ''}]`;
76
88
  case 'Audio':
77
- return '[音频]';
89
+ return hideUnsupported ? '' : '[音频]';
78
90
  case 'Video':
79
- return '[视频]';
91
+ return hideUnsupported ? '' : '[视频]';
80
92
  default:
81
93
  return '';
82
94
  }
package/lib/index.js CHANGED
@@ -329,12 +329,12 @@ const main = () => {
329
329
  });
330
330
  const formatKookContent = (val) => {
331
331
  const nativeItems = val.filter(item => item.type === 'Mention' || item.type === 'Text' || item.type === 'Link');
332
- const unsupportedItems = val.filter(item => item.type !== 'Mention'
333
- && item.type !== 'Text'
334
- && item.type !== 'Link'
335
- && item.type !== 'Image'
336
- && item.type !== 'ImageURL'
337
- && item.type !== 'ImageFile');
332
+ const unsupportedItems = val.filter(item => item.type !== 'Mention' &&
333
+ item.type !== 'Text' &&
334
+ item.type !== 'Link' &&
335
+ item.type !== 'Image' &&
336
+ item.type !== 'ImageURL' &&
337
+ item.type !== 'ImageFile');
338
338
  const nativeText = nativeItems
339
339
  .map(item => {
340
340
  if (item.type === 'Link') {
@@ -373,8 +373,12 @@ const main = () => {
373
373
  return '';
374
374
  })
375
375
  .join('');
376
+ const hide = getKOOKConfig().hideUnsupported;
376
377
  const fallbackText = unsupportedItems.length > 0
377
- ? unsupportedItems.map(item => dataEnumToKMarkdown(item)).filter(Boolean).join('')
378
+ ? unsupportedItems
379
+ .map(item => dataEnumToKMarkdown(item, hide))
380
+ .filter(Boolean)
381
+ .join('')
378
382
  : '';
379
383
  return [nativeText, fallbackText].filter(Boolean).join('');
380
384
  };
@@ -428,6 +432,10 @@ const main = () => {
428
432
  const content = formatKookContent(val);
429
433
  try {
430
434
  const imageUrl = await uploadAndGetImageUrl(val);
435
+ if (getKOOKConfig().hideUnsupported && !content && !imageUrl) {
436
+ logger.info('[kook] hideUnsupported: 消息内容转换后为空,跳过发送');
437
+ return [];
438
+ }
431
439
  if (imageUrl && content) {
432
440
  const imgRes = await client.createMessage({
433
441
  type: 2,
@@ -470,6 +478,10 @@ const main = () => {
470
478
  const content = formatKookContent(val);
471
479
  try {
472
480
  const imageUrl = await uploadAndGetImageUrl(val);
481
+ if (getKOOKConfig().hideUnsupported && !content && !imageUrl) {
482
+ logger.info('[kook] hideUnsupported: 消息内容转换后为空,跳过发送');
483
+ return [];
484
+ }
473
485
  if (imageUrl && content) {
474
486
  const imgRes = await client.createDirectMessage({
475
487
  type: 2,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/kook",
3
- "version": "2.1.1",
3
+ "version": "2.1.3",
4
4
  "description": "kook platform connection",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",