Skip to content

Commit

Permalink
chore: housekeeping
Browse files Browse the repository at this point in the history
  • Loading branch information
dragon-fish committed Dec 6, 2024
1 parent 9078551 commit 77c7926
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 22 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@koishijs/plugin-adapter-dingtalk": "^2.4.2",
"@koishijs/plugin-adapter-discord": "^4.5.9",
"@koishijs/plugin-adapter-kook": "^4.6.4",
"@koishijs/plugin-adapter-qq": "^4.8.6",
"@koishijs/plugin-adapter-satori": "^1.3.1",
"@koishijs/plugin-admin": "^1.4.0",
"@koishijs/plugin-analytics": "^2.0.6",
Expand Down
24 changes: 24 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 30 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import PluginYoudao from '~/youdao'
import AdapterDingtalk from '@koishijs/plugin-adapter-dingtalk'
import AdapterDiscord from '@koishijs/plugin-adapter-discord'
import AdapterKook from '@koishijs/plugin-adapter-kook'
import AdapterQQ, { QQ } from '@koishijs/plugin-adapter-qq'
import * as PluginAdmin from '@koishijs/plugin-admin'
import PluginAnalytics from '@koishijs/plugin-analytics'
import PluginAuth from '@koishijs/plugin-auth'
Expand Down Expand Up @@ -105,8 +106,13 @@ const { env } = process
const app = new App({
nickname: env.KOISHI_NICKNAME?.split('|'),
prefix: (ctx) => {
if (['qq', 'qqguild'].includes(ctx.platform)) {
return ''
}
const items = env.KOISHI_PREFIX?.split('|') || []
if (ctx.platform === 'villa') items.unshift('/')
if (['villa', 'discord'].includes(ctx.platform)) {
items.unshift('/')
}
return items
},
})
Expand All @@ -129,11 +135,29 @@ app.plugin(PluginMongo, {
/** 安装适配器 */
app.plugin(function PluginCollectionAdapters(ctx) {
// QQ
ctx.plugin(AdapterOnebot, {
protocol: env.ONEBOT_PROTOCOL,
selfId: env.ONEBOT_SELFID?.trim(),
endpoint: env.ONEBOT_ENDPOINT,
})
if (process.env.ONEBOT_SELFID) {
ctx.plugin(AdapterOnebot, {
protocol: env.ONEBOT_PROTOCOL,
selfId: env.ONEBOT_SELFID?.trim(),
endpoint: env.ONEBOT_ENDPOINT,
})
}
if (process.env.QQBOT_APPID) {
ctx.plugin(AdapterQQ, {
sandbox: true,
id: env.QQBOT_APPID,
token: env.QQBOT_TOKEN,
secret: env.QQBOT_SECRET,
type: env.QQBOT_TYPE,
intents:
QQ.Intents.GUILDS |
QQ.Intents.GUILD_MEMBERS |
QQ.Intents.PUBLIC_GUILD_MESSAGES |
QQ.Intents.OPEN_FORUMS_EVENT |
QQ.Intents.INTERACTIONS |
QQ.Intents.MESSAGE_AUDIT,
})
}

// Discord
if (env.TOKEN_DISCORD_BOT) {
Expand Down
50 changes: 34 additions & 16 deletions src/services/HTMLService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Context, Service, h } from 'koishi'

import type { ScreenshotOptions, WaitForOptions } from 'puppeteer-core'
import type { BundledLanguage } from 'shiki'
import type { BundledLanguage, BundledLanguageInfo } from 'shiki'

declare module 'koishi' {
export interface Context {
Expand Down Expand Up @@ -245,46 +245,64 @@ code.hljs[class~='lang-wiki']:before {
'shiki'
)

lang = lang || ('' as any)
if ((lang as any) !== '' && !(lang in bundledLanguages)) {
throw new Error(`Language not supported: ${lang}`)
}

const langInfo = bundledLanguagesInfo.find((i) => i.aliases?.includes(lang))
const langLabel = langInfo?.aliases?.[0] || langInfo.name || lang
const langInfo = bundledLanguagesInfo.find(
(i) => i.aliases?.includes(lang) || i.id === lang || i.name === lang
)
const langLabel = (() => {
if (!langInfo) return lang
return [langInfo.aliases?.[0], langInfo.name, langInfo.id, lang]
.filter(Boolean)
.sort((a, b) => a.length - b.length)[0]
})()

const html = await codeToHtml(code, {
lang: langInfo.id || '',
lang,
theme: 'one-dark-pro',
transformers: [
{
pre(node) {
node.properties.style += ';'
node.properties.style += `padding-right: ${(10 * langLabel.length + 12).toFixed()}px;`
if (typeof startFrom === 'number') {
node.properties.class += ' line-number'
}
},
code(node) {
node.properties.style += ';'
node.properties.style += `--start: ${typeof startFrom === 'number' ? startFrom : 1};`
node.properties.style += `;--start: ${startFrom};`
},
line(hast, line) {
hast.properties['data-node-line-number'] = line
},
postprocess(html) {
return html.replace(
/<\/pre>/,
`<code class="lang-badge">${langLabel}</code></pre>`
)
if (langLabel) {
return html.replace(
'</pre>',
`<code class="lang-badge">${langLabel}</code></pre>`
)
}
},
},
],
})
const css = `
<style>
/* base styles */
pre.shiki {
position: relative;
font-family: 'Fira Code', 'Consolas', 'Monaco', 'Andale Mono', 'Ubuntu Mono', monospace;
font-family: 'JetBrainsMono Nerd Font', 'JetBrains Mono NF', 'JetBrains Mono', 'Fira Code', 'Consolas', 'Monaco', 'Andale Mono', 'Ubuntu Mono', monospace;
font-size: 16px;
display: inline-block;
padding: 1em;
border-radius: 0.5em;
white-space: pre;
}
pre.shiki code.lang-badge {
/* lang badge */
pre.shiki > .lang-badge {
position: absolute;
right: 0.5em;
top: 0.5em;
Expand All @@ -294,15 +312,15 @@ pre.shiki code.lang-badge {
padding: 0.2em 0.5em;
}
/* line number */
pre.shiki code {
pre.shiki.line-number code {
counter-reset: step;
counter-increment: step calc(var(--start, 1) - 1);
}
pre.shiki code .line::before {
pre.shiki.line-number code .line::before {
content: counter(step);
counter-increment: step;
width: 1rem;
margin-right: 1.5rem;
width: 1em;
margin-right: 1em;
display: inline-block;
text-align: right;
color: rgba(115,138,148,.4)
Expand Down

0 comments on commit 77c7926

Please sign in to comment.