Skip to content

Commit

Permalink
feat(kaiheila): support markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 28, 2021
1 parent 8a5f915 commit f7d9dca
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions packages/adapter-kaiheila/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,16 @@ export class KaiheilaBot extends Bot {
}
}

private async _sendCard(handle: SendHandle, chain: segment.Chain) {
let text: KHL.Card.Text = { type: 'plain-text', content: '' }
private async _sendCard(handle: SendHandle, chain: segment.Chain, useMarkdown: boolean) {
const type = useMarkdown ? 'kmarkdown' : 'plain-text'
let text: KHL.Card.Text = { type, content: '' }
let card: KHL.Card = { type: 'card', modules: [] }
const output: KHL.Card[] = []
const flushText = () => {
text.content = text.content.trim()
if (!text.content) return
card.modules.push({ type: 'section', text })
text = { type: 'plain-text', content: '' }
text = { type, content: '' }
}
const flushCard = () => {
flushText()
Expand Down Expand Up @@ -150,12 +151,13 @@ export class KaiheilaBot extends Bot {
await this._sendHandle(handle, KHL.Type.card, JSON.stringify(output))
}

private async _sendSeparate(handle: SendHandle, chain: segment.Chain) {
private async _sendSeparate(handle: SendHandle, chain: segment.Chain, useMarkdown: boolean) {
let textBuffer = ''
const type = useMarkdown ? KHL.Type.kmarkdown : KHL.Type.text
const flush = async () => {
textBuffer = textBuffer.trim()
if (!textBuffer) return
await this._sendHandle(handle, KHL.Type.text, textBuffer)
await this._sendHandle(handle, type, textBuffer)
handle[1].quote = null
textBuffer = ''
}
Expand Down Expand Up @@ -192,19 +194,24 @@ export class KaiheilaBot extends Bot {
const [, params, session] = handle
if (await this.app.serial(session, 'before-send', session)) return

let useMarkdown = false
const chain = segment.parse(content)
if (chain[0].type === 'quote') {
params.quote = chain.shift().data.id
}
if (chain[0].type === 'markdown') {
useMarkdown = true
chain.shift()
}

const { attachMode } = this.app.options.kaiheila
const hasAttachment = chain.some(node => attachmentTypes.includes(node.type))
const useCard = hasAttachment && (attachMode === 'card' || attachMode === 'mixed' && chain.length > 1)

if (useCard) {
await this._sendCard(handle, chain)
await this._sendCard(handle, chain, useMarkdown)
} else {
await this._sendSeparate(handle, chain)
await this._sendSeparate(handle, chain, useMarkdown)
}

return session.messageId
Expand Down

0 comments on commit f7d9dca

Please sign in to comment.