Skip to content

Commit 2718643

Browse files
committed
improve the slack preview text
1 parent 70880a3 commit 2718643

File tree

3 files changed

+30
-96
lines changed

3 files changed

+30
-96
lines changed

lib/slack.ts

+30-41
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { serverEnv } from '@/env/server'
22
import type { Post } from '@prisma/client'
3-
import { markdownToBlocks } from '@tryfabric/mack'
43
import { marked } from 'marked'
54
import { WebClient } from '@slack/web-api'
65

@@ -13,16 +12,7 @@ export async function postToSlackIfEnabled({
1312
}) {
1413
if (serverEnv.ENABLE_SLACK_POSTING && serverEnv.SLACK_TOKEN) {
1514
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)
2616

2717
const web = new WebClient(serverEnv.SLACK_TOKEN)
2818
return await web.chat.postMessage({
@@ -36,7 +26,13 @@ export async function postToSlackIfEnabled({
3626
text: `*<${serverEnv.NEXT_APP_URL}/post/${post.id}|${post.title}>*`,
3727
},
3828
},
39-
summaryBlocks[0],
29+
{
30+
type: 'section',
31+
text: {
32+
type: 'mrkdwn',
33+
text: summary,
34+
},
35+
},
4036
{ type: 'divider' },
4137
{
4238
type: 'context',
@@ -50,34 +46,27 @@ export async function postToSlackIfEnabled({
5046
},
5147
],
5248
})
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-
// })
8249
}
8350
}
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+
}

package-lock.json

-54
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"@trpc/next": "^9.18.0",
2020
"@trpc/react": "^9.18.0",
2121
"@trpc/server": "^9.18.0",
22-
"@tryfabric/mack": "^1.2.1",
2322
"cloudinary": "^1.27.1",
2423
"crypto-hash": "^2.0.1",
2524
"date-fns": "^2.28.0",

0 commit comments

Comments
 (0)