@alemonjs/kook 2.1.0 → 2.1.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.
@@ -0,0 +1,4 @@
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;
package/lib/format.js ADDED
@@ -0,0 +1,85 @@
1
+ const markdownToKMarkdown = (items) => {
2
+ return items
3
+ .map(item => {
4
+ switch (item.type) {
5
+ case 'MD.text':
6
+ return item.value;
7
+ case 'MD.title':
8
+ return `**${item.value}**\n`;
9
+ case 'MD.subtitle':
10
+ return `**${item.value}**\n`;
11
+ case 'MD.bold':
12
+ return `**${item.value}**`;
13
+ case 'MD.italic':
14
+ case 'MD.italicStar':
15
+ return `*${item.value}*`;
16
+ case 'MD.strikethrough':
17
+ return `~~${item.value}~~`;
18
+ case 'MD.link': {
19
+ const v = item.value;
20
+ return `[${v.text}](${v.url})`;
21
+ }
22
+ case 'MD.image':
23
+ return '[图片]';
24
+ case 'MD.list':
25
+ return (item.value
26
+ .map(li => {
27
+ if (typeof li.value === 'object') {
28
+ return `${li.value.index}. ${li.value.text ?? ''}`;
29
+ }
30
+ return `- ${li.value}`;
31
+ })
32
+ .join('\n') + '\n');
33
+ case 'MD.blockquote':
34
+ return `> ${item.value}\n`;
35
+ case 'MD.divider':
36
+ return '---\n';
37
+ case 'MD.newline':
38
+ return '\n';
39
+ case 'MD.code': {
40
+ const lang = item?.options?.language || '';
41
+ return `\`\`\`${lang}\n${item.value}\n\`\`\`\n`;
42
+ }
43
+ case 'MD.mention':
44
+ if (item.value === 'everyone') {
45
+ return '(met)all(met)';
46
+ }
47
+ return `(met)${item.value ?? ''}(met)`;
48
+ case 'MD.content':
49
+ return item.value;
50
+ case 'MD.button':
51
+ return `[${item.value}]`;
52
+ default:
53
+ return String(item?.value ?? '');
54
+ }
55
+ })
56
+ .join('');
57
+ };
58
+ const markdownRawToKMarkdown = (raw) => {
59
+ let text = raw;
60
+ text = text.replace(/!\[([^\]]*)\]\([^)]*\)/g, '[图片]');
61
+ return text;
62
+ };
63
+ const dataEnumToKMarkdown = (item) => {
64
+ switch (item.type) {
65
+ case 'Markdown':
66
+ return markdownToKMarkdown(item.value);
67
+ case 'MarkdownOriginal':
68
+ return markdownRawToKMarkdown(String(item.value));
69
+ case 'BT.group':
70
+ case 'ButtonGroup':
71
+ return item.value
72
+ .map((row) => row.value.map((btn) => `[${btn.value}]`).join(' '))
73
+ .join('\n');
74
+ case 'Attachment':
75
+ return `[附件${item.options?.filename ? ': ' + item.options.filename : ''}]`;
76
+ case 'Audio':
77
+ return '[音频]';
78
+ case 'Video':
79
+ return '[视频]';
80
+ default:
81
+ return '';
82
+ }
83
+ };
84
+
85
+ export { dataEnumToKMarkdown, markdownRawToKMarkdown, markdownToKMarkdown };
package/lib/index.js CHANGED
@@ -4,6 +4,7 @@ import { KOOKClient } from './sdk/wss.js';
4
4
  import { readFileSync } from 'fs';
5
5
  import { getKOOKConfig, getMaster, platform } from './config.js';
6
6
  import { getBufferByURL } from 'alemonjs/utils';
7
+ import { dataEnumToKMarkdown } from './format.js';
7
8
  export { useClient, useValue } from './hook.js';
8
9
 
9
10
  const main = () => {
@@ -13,9 +14,12 @@ const main = () => {
13
14
  });
14
15
  void client.connect();
15
16
  let botId = '';
16
- client.userMe().then(res => {
17
+ client
18
+ .userMe()
19
+ .then(res => {
17
20
  botId = String(res?.data?.id ?? '');
18
- }).catch(() => { });
21
+ })
22
+ .catch(() => { });
19
23
  const port = process.env?.port || 17117;
20
24
  const url = `ws://127.0.0.1:${port}`;
21
25
  const cbp = cbpPlatform(url);
@@ -324,8 +328,14 @@ const main = () => {
324
328
  cbp.send(e);
325
329
  });
326
330
  const formatKookContent = (val) => {
327
- return val
328
- .filter(item => item.type === 'Mention' || item.type === 'Text' || item.type === 'Link')
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');
338
+ const nativeText = nativeItems
329
339
  .map(item => {
330
340
  if (item.type === 'Link') {
331
341
  return `[${item.value}](${item?.options?.link ?? item.value})`;
@@ -363,6 +373,10 @@ const main = () => {
363
373
  return '';
364
374
  })
365
375
  .join('');
376
+ const fallbackText = unsupportedItems.length > 0
377
+ ? unsupportedItems.map(item => dataEnumToKMarkdown(item)).filter(Boolean).join('')
378
+ : '';
379
+ return [nativeText, fallbackText].filter(Boolean).join('');
366
380
  };
367
381
  const resolveImageBuffer = async (val) => {
368
382
  const images = val.filter(item => item.type === 'Image' || item.type === 'ImageFile' || item.type === 'ImageURL');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/kook",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "description": "kook platform connection",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",