Skip to content

Commit

Permalink
feat(kook): support kmarkdown in buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Dec 2, 2023
1 parent decc3e4 commit 9b4463b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
2 changes: 1 addition & 1 deletion adapters/kook/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@satorijs/adapter-kook",
"description": "KOOK (开黑啦) Adapter for Satorijs",
"version": "4.2.3",
"version": "4.2.4",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
Expand Down
37 changes: 31 additions & 6 deletions adapters/kook/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,16 @@ export class KookMessageEncoder<C extends Context = Context> extends MessageEnco
this.cardBuffer.modules.push({
type: 'header',
text: {
type: 'plain-text',
type: attrs.type,
content: attrs.content,
},
})
} else if (type === 'kook:countdown') {
this.flushText()
this.cardBuffer.modules.push({
type: 'countdown',
startTime: attrs.startTime,
endTime: attrs.endTime,
startTime: +attrs.startTime,
endTime: +attrs.endTime,
mode: attrs.mode,
})
} else if (type === 'kook:invite') {
Expand Down Expand Up @@ -269,11 +269,36 @@ function encodeButton({ attrs, children }: h): Kook.Card.Button {
return {
type: 'button',
theme,
value: attrs.id,
value: attrs.type === 'link' ? attrs.href : attrs.id,
click: attrs.type === 'link' ? 'link' : 'return-val',
text: {
type: 'plain-text',
content: children.join(''),
type: 'kmarkdown',
content: encodeMarkdown(children),
},
}
}

function encodeMarkdown(children: h[]): string {
let content = ''
for (const element of children) {
const { type, attrs, children } = element
if (type === 'text') {
content += attrs.content.replace(/[\\*`~()]/g, '\\$&')
} else if (type === 'b' || type === 'strong') {
content += '**' + encodeMarkdown(children) + '**'
} else if (type === 'i' || type === 'em') {
content += '*' + encodeMarkdown(children) + '*'
} else if (type === 'u' || type === 'ins') {
content += '(ins)' + encodeMarkdown(children) + '(ins)'
} else if (type === 's' || type === 'del') {
content += '~~' + encodeMarkdown(children) + '~~'
} else if (type === 'spl') {
content += '(spl)' + encodeMarkdown(children) + '(spl)'
} else if (type === 'code') {
content += '`' + element.toString(true) + '`'
} else if (type === 'a') {
content += `[${encodeMarkdown(children)}](${attrs.href})`
}
}
return content
}
4 changes: 2 additions & 2 deletions adapters/kook/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ export namespace Card {

export interface Countdown {
type: 'countdown'
endTime: string
startTime: string
endTime: number
startTime: number
mode: 'day' | 'hour' | 'second'
}
}
Expand Down

0 comments on commit 9b4463b

Please sign in to comment.