diff --git a/packages/plugin-github/package.json b/packages/plugin-github/package.json index 569cdc7eb0..bb9e9ef9c2 100644 --- a/packages/plugin-github/package.json +++ b/packages/plugin-github/package.json @@ -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" } } diff --git a/packages/plugin-github/src/events.ts b/packages/plugin-github/src/events.ts index dc2f2dc449..7486a838d3 100644 --- a/packages/plugin-github/src/events.ts +++ b/packages/plugin-github/src/events.ts @@ -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 `${infer L}_${infer M}${infer R}` ? `${L}${Uppercase}${Camelize}` : S @@ -72,13 +73,6 @@ export const defaultEvents: EventConfig = { type EventHandler = (payload: Payload) => EventData

-function formatMarkdown(source: string) { - return source - .replace(/^```(.*)$/gm, '') - .replace(/^$/gm, '') - .replace(/\n\s*\n/g, '\n') -} - type FactoryCreator = (callback: (event: T, payload: Payload, handler: EventHandler) => EventData

) => (event: E, handler?: EventHandler) => void @@ -108,7 +102,7 @@ export function addListeners(on: (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: [ @@ -173,7 +167,7 @@ export function addListeners(on: (event: T, h return [[ `${sender.login} opened an issue ${full_name}#${number}`, `Title: ${title}`, - formatMarkdown(body), + transform(body), ].join('\n')] }) @@ -207,7 +201,7 @@ export function addListeners(on: (event: T, h return [[ `${user.login} reviewed pull request ${full_name}#${number}`, - formatMarkdown(body), + transform(body), ].join('\n'), { link: [html_url], reply: [comments_url], @@ -256,7 +250,7 @@ export function addListeners(on: (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')] }) @@ -286,7 +280,7 @@ export function addListeners(on: (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], }] diff --git a/packages/plugin-github/src/markdown.ts b/packages/plugin-github/src/markdown.ts new file mode 100644 index 0000000000..3aea08fe95 --- /dev/null +++ b/packages/plugin-github/src/markdown.ts @@ -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') +}