Skip to content

Commit

Permalink
feat(github): better markdown transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 21, 2021
1 parent 57e5eba commit 16c7059
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
9 changes: 5 additions & 4 deletions packages/plugin-github/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@
"webhook"
],
"devDependencies": {
"@types/marked": "^2.0.0",
"koishi-plugin-puppeteer": "^2.0.0",
"koishi-test-utils": "^6.0.0-beta.10"
},
"peerDependencies": {
"koishi-core": "^3.3.0",
"koishi-plugin-puppeteer": "^2.0.0",
"koishi-utils": "^4.0.3"
"koishi-core": "^3.3.0"
},
"dependencies": {
"@octokit/webhooks-definitions": "^3.63.1",
"axios": "^0.21.1"
"axios": "^0.21.1",
"marked": "^2.0.1"
}
}
18 changes: 6 additions & 12 deletions packages/plugin-github/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { EventPayloadMap, WebhookEventName, Repository, PullRequest } from '@octokit/webhooks-definitions/schema'
import { EventData } from './server'
import { transform } from './markdown'

type Camelize<S extends string> = S extends `${infer L}_${infer M}${infer R}` ? `${L}${Uppercase<M>}${Camelize<R>}` : S

Expand Down Expand Up @@ -72,13 +73,6 @@ export const defaultEvents: EventConfig = {

type EventHandler<T extends EmitterWebhookEventName, P = {}> = (payload: Payload<T>) => EventData<P>

function formatMarkdown(source: string) {
return source
.replace(/^```(.*)$/gm, '')
.replace(/^<!--(.*)-->$/gm, '')
.replace(/\n\s*\n/g, '\n')
}

type FactoryCreator = <T extends EmitterWebhookEventName, P = {}>
(callback: (event: T, payload: Payload<T>, handler: EventHandler<T, P>) => EventData<P>)
=> <E extends T>(event: E, handler?: EventHandler<E, P>) => void
Expand Down Expand Up @@ -108,7 +102,7 @@ export function addListeners(on: <T extends EmitterWebhookEventName>(event: T, h

const index = html_url.indexOf('#')
const operation = payload.action === 'created' ? 'commented' : 'edited a comment'
return [`${user.login} ${operation} on ${target}\n${formatMarkdown(body)}`, {
return [`${user.login} ${operation} on ${target}\n${transform(body)}`, {
link: [html_url],
react: [url + `/reactions`],
shot: [
Expand Down Expand Up @@ -173,7 +167,7 @@ export function addListeners(on: <T extends EmitterWebhookEventName>(event: T, h
return [[
`${sender.login} opened an issue ${full_name}#${number}`,
`Title: ${title}`,
formatMarkdown(body),
transform(body),
].join('\n')]
})

Expand Down Expand Up @@ -207,7 +201,7 @@ export function addListeners(on: <T extends EmitterWebhookEventName>(event: T, h

return [[
`${user.login} reviewed pull request ${full_name}#${number}`,
formatMarkdown(body),
transform(body),
].join('\n'), {
link: [html_url],
reply: [comments_url],
Expand Down Expand Up @@ -256,7 +250,7 @@ export function addListeners(on: <T extends EmitterWebhookEventName>(event: T, h
return [[
`${sender.login} ${draft ? 'drafted' : 'opened'} a pull request ${full_name}#${number} (${baseLabel}${headLabel})`,
`Title: ${title}`,
formatMarkdown(body),
transform(body),
].join('\n')]
})

Expand Down Expand Up @@ -286,7 +280,7 @@ export function addListeners(on: <T extends EmitterWebhookEventName>(event: T, h

return [[
`${pusher.name} pushed to ${full_name}:${ref.replace(/^refs\/heads\//, '')}`,
...commits.map(c => `[${c.id.slice(0, 6)}] ${formatMarkdown(c.message)}`),
...commits.map(c => `[${c.id.slice(0, 6)}] ${transform(c.message)}`),
].join('\n'), {
link: [compare],
}]
Expand Down
36 changes: 36 additions & 0 deletions packages/plugin-github/src/markdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { segment } from 'koishi-utils'
import marked, { Token, lexer } from 'marked'

declare module 'marked' {
namespace Tokens {
interface Def {
type: 'def'
}

interface Paragraph {
tokens: marked.Token[]
}
}
}

function renderToken(token: Token) {
if (token.type === 'code') {
return token.text + '\n'
} else if (token.type === 'paragraph') {
return render(token.tokens)
} else if (token.type === 'image') {
return segment.image(token.href)
} else if (token.type === 'blockquote') {
return token.text
}
return token.raw
}

function render(tokens: Token[]) {
return tokens.map(renderToken).join('')
}

export function transform(source: string) {
source = source.replace(/^<!--(.*)-->$/gm, '')
return render(lexer(source)).trim().replace(/\n\s*\n/g, '\n')
}

0 comments on commit 16c7059

Please sign in to comment.