1
1
import { serverEnv } from '@/env/server'
2
2
import type { Post } from '@prisma/client'
3
- import { markdownToBlocks } from '@tryfabric/mack'
4
3
import { marked } from 'marked'
5
4
import { WebClient } from '@slack/web-api'
6
5
@@ -13,16 +12,7 @@ export async function postToSlackIfEnabled({
13
12
} ) {
14
13
if ( serverEnv . ENABLE_SLACK_POSTING && serverEnv . SLACK_TOKEN ) {
15
14
const tokens = marked . lexer ( post . content )
16
- const summaryToken = tokens . find ( ( token ) => {
17
- return (
18
- token . type === 'paragraph' ||
19
- token . type === 'html' ||
20
- token . type === 'image'
21
- )
22
- } )
23
- const summaryBlocks = summaryToken
24
- ? await markdownToBlocks ( summaryToken . raw )
25
- : [ ]
15
+ const summary = summarize ( tokens )
26
16
27
17
const web = new WebClient ( serverEnv . SLACK_TOKEN )
28
18
return await web . chat . postMessage ( {
@@ -36,7 +26,13 @@ export async function postToSlackIfEnabled({
36
26
text : `*<${ serverEnv . NEXT_APP_URL } /post/${ post . id } |${ post . title } >*` ,
37
27
} ,
38
28
} ,
39
- summaryBlocks [ 0 ] ,
29
+ {
30
+ type : 'section' ,
31
+ text : {
32
+ type : 'mrkdwn' ,
33
+ text : summary ,
34
+ } ,
35
+ } ,
40
36
{ type : 'divider' } ,
41
37
{
42
38
type : 'context' ,
@@ -50,34 +46,27 @@ export async function postToSlackIfEnabled({
50
46
} ,
51
47
] ,
52
48
} )
53
- // return fetch(serverEnv.SLACK_WEBHOOK_URL, {
54
- // method: 'POST',
55
- // headers: {
56
- // 'Content-Type': 'application/json',
57
- // },
58
- // body: JSON.stringify({
59
- // blocks: [
60
- // {
61
- // type: 'section',
62
- // text: {
63
- // type: 'mrkdwn',
64
- // text: `*<${serverEnv.NEXT_APP_URL}/post/${post.id}|${post.title}>*`,
65
- // },
66
- // },
67
- // summaryBlocks[0],
68
- // { type: 'divider' },
69
- // {
70
- // type: 'context',
71
- // elements: [
72
- // {
73
- // type: 'plain_text',
74
- // text: authorName,
75
- // emoji: true,
76
- // },
77
- // ],
78
- // },
79
- // ],
80
- // }),
81
- // })
82
49
}
83
50
}
51
+
52
+ const MAX_CHARS = 250
53
+
54
+ function summarize ( tokens : marked . Token [ ] ) {
55
+ let summary = ''
56
+ let charCount = 0
57
+
58
+ for ( const token of tokens ) {
59
+ if ( token . type === 'paragraph' ) {
60
+ const text = token . text || ''
61
+ const remainingChars = MAX_CHARS - charCount
62
+ summary += ' ' + text . substring ( 0 , remainingChars )
63
+ charCount += text . length
64
+
65
+ if ( charCount >= MAX_CHARS ) {
66
+ break
67
+ }
68
+ }
69
+ }
70
+
71
+ return summary
72
+ }
0 commit comments