Skip to content

Commit

Permalink
refactore
Browse files Browse the repository at this point in the history
  • Loading branch information
Lipraty committed Jan 24, 2024
1 parent 4239deb commit bd3d483
Show file tree
Hide file tree
Showing 21 changed files with 267 additions and 55 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
insert_final_newline = true
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
7 changes: 7 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
* text eol=lf

*.png -text
*.jpg -text
*.ico -text
*.gif -text
*.webp -text
65 changes: 28 additions & 37 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,44 +1,35 @@
{
"name": "koishi-plugin-imagify",
"description": "imageify of so TM long message",
"version": "2.2.6",
"repository": "https://github.com/koishijs/koishi-plugin-imagify",
"author": "Lipraty <[email protected]>",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
"lib",
"dist"
"name": "@root/imagify",
"version": "0.0.0",
"private": true,
"workspaces": [
"packages/*"
],
"packageManager": "[email protected]",
"license": "MIT",
"koishi": {
"description": {
"zh": "🖼️ 将过多的文本以图片发出",
"en": "🖼️ imageify of so TM long message"
},
"service": {
"required": [
"puppeteer"
]
}
"scripts": {
"build": "yakumo build",
"docs:dev": "vitepress dev docs --open",
"docs:build": "vitepress build docs",
"docs:serve": "vitepress serve docs",
"bump": "yakumo version",
"dep": "yakumo upgrade",
"pub": "yakumo publish"
},
"keywords": [
"chatbot",
"koishi",
"plugin",
"image",
"pptr",
"puppeteer",
"shortcut",
"图像",
"图片"
],
"devDependencies": {
"@koishijs/cache": "^2.1.0",
"koishi": "^4.16.4"
},
"peerDependencies": {
"koishi": "^4.16.4",
"koishi-plugin-puppeteer": "^3.7.3"
"@koishijs/client": "^5.22.3",
"@koishijs/plugin-hmr": "^1.2.8",
"@koishijs/scripts": "^4.5.0",
"@types/node": "^20.11.3",
"@types/react": "^18.2.48",
"cross-env": "^7.0.3",
"esbuild": "^0.18.20",
"esbuild-register": "npm:@shigma/esbuild-register@^1.1.1",
"typescript": "^5.3.3",
"yakumo": "^1.0.0-alpha.10",
"yakumo-esbuild": "^1.0.0-alpha.2",
"yakumo-publish-sync": "^1.0.0-alpha.1",
"yakumo-tsc": "^1.0.0-alpha.2",
"yml-register": "^1.1.0"
}
}
44 changes: 44 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "koishi-plugin-imagify",
"description": "imageify of so TM long message",
"version": "2.2.6",
"repository": "https://github.com/koishijs/koishi-plugin-imagify",
"author": "Lipraty <[email protected]>",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
"lib",
"dist"
],
"license": "MIT",
"koishi": {
"description": {
"zh": "🖼️ 将过多的文本以图片发出",
"en": "🖼️ imageify of so TM long message"
},
"service": {
"required": [
"puppeteer"
]
}
},
"keywords": [
"chatbot",
"koishi",
"plugin",
"image",
"pptr",
"puppeteer",
"shortcut",
"图像",
"图片"
],
"devDependencies": {
"@koishijs/cache": "^2.1.0",
"koishi": "^4.16.4"
},
"peerDependencies": {
"koishi": "^4.16.4",
"koishi-plugin-puppeteer": "^3.7.3"
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion src/index.ts → packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { Page } from 'puppeteer-core'
import { readFileSync } from 'fs'
import { ruler, parser, appendElements, templater, linerElements } from './helper'
import { ImageRule, RuleType, RuleComputed, PageWorker, CacheModel, CacheRule } from './types'
import { CacheService, FREQUENCY_THRESHOLD, cacheKeyHash } from './cache'
import { CacheService, FREQUENCY_THRESHOLD, cacheKeyHash } from './internal/cache'

const { version: pVersion } = require('../package.json')
const css = readFileSync(require.resolve('./default.css'), 'utf8')
Expand Down
4 changes: 2 additions & 2 deletions src/cache.ts → packages/core/src/internal/cache.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Context } from "koishi"
import { createHash } from "node:crypto"
import { resolve } from "node:path"
import { CacheDatabase, CacheModel, Cacher, CacheStore } from "./types"
import { Config } from "."
import { CacheDatabase, CacheModel, Cacher, CacheStore } from "../types"
import { Config } from ".."

declare module 'koishi' {
interface Tables {
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions packages/core/src/internal/react.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@


60 changes: 60 additions & 0 deletions packages/core/src/internal/render.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Context } from 'koishi'
import { } from 'koishi-plugin-puppeteer'
import type { Page } from 'puppeteer-core'
import { PageWorker } from '../types'

/// Puppeteer render

/**
* Load html template to page
*/
export async function pptrRender(page: Page, html: string): Promise<Page> {
await page.evaluate((eleString) => {
document.querySelector('#container').innerHTML = eleString
}, html)
return page
}

/**
* Get a worker from pagepool
* @param pagepool
*/
export async function pptrGetWorker(pagepool: PageWorker<Page>[]): Promise<[PageWorker<Page>[], Promise<PageWorker<Page>>]> {
return [pagepool, new Promise<PageWorker<Page>>((resolve) => {
function check() {
const available = pagepool.find(p => !p.busy)

if (available) {
available.busy = true
resolve(available)
} else {
setTimeout(check, 100)
}
}
check()
})]
}

/**
* Screenshot page
* @param quality image quality. maxmium hundred positive integer
*/
export async function pptrScreenshot(page: Page, quality: number = 100): Promise<[Page, Buffer]> {
const { width, height } = await page.evaluate(() => {
const element = document.querySelector('._image')
return { width: element.clientWidth, height: element.clientHeight }
})
return [page, await page.screenshot({
clip: { x: 0, y: 0, width, height },
quality,
type: 'jpeg'
})]
}

/**
* ?
*/
export const pptrClose = (page: Page) => page.close()

/// Canvas render

File renamed without changes.
File renamed without changes.
File renamed without changes.
19 changes: 19 additions & 0 deletions packages/core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"rootDir": "src",
"outDir": "lib",
"target": "es2020",
"module": "commonjs",
"declaration": true,
"composite": true,
"incremental": true,
"skipLibCheck": true,
"esModuleInterop": true,
"moduleResolution": "node",
"jsx": "react-jsx",
"jsxImportSource": "@satorijs/element",
},
"include": [
"src",
],
}
45 changes: 45 additions & 0 deletions packages/imagify-pug/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "koishi-plugin-imagify-pug",
"description": "Pug engine for koishi-plugin-imagify",
"version": "0.0.0",
"repository": "https://github.com/koishijs/koishi-plugin-imagify",
"author": "Lipraty <[email protected]>",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
"lib",
"dist"
],
"license": "MIT",
"koishi": {
"description": {
"zh": "🖼️ 用于 Imagify 的 Pug 模板引擎",
"en": "🖼️ Pug engine for koishi-plugin-imagify"
},
"service": {
"required": [
"imagify"
]
}
},
"keywords": [
"chatbot",
"koishi",
"plugin",
"image",
"pptr",
"puppeteer",
"shortcut",
"pug",
"图像",
"图片"
],
"devDependencies": {
"@koishijs/cache": "^2.1.0",
"koishi": "^4.16.4"
},
"peerDependencies": {
"koishi": "^4.16.4",
"koishi-plugin-imagify": "^3.0.0"
}
}
Empty file.
19 changes: 19 additions & 0 deletions packages/imagify-pug/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"rootDir": "src",
"outDir": "lib",
"target": "es2020",
"module": "commonjs",
"declaration": true,
"composite": true,
"incremental": true,
"skipLibCheck": true,
"esModuleInterop": true,
"moduleResolution": "node",
"jsx": "react-jsx",
"jsxImportSource": "@satorijs/element",
},
"include": [
"src",
],
}
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Nextgen imagify plugin for koishi.
## Features

- [x] All features from version 2.x
- [ ] More styles and templates
- [ ] Custom your font
- [x] Support `puppeteer` and `canvas` dual stack engine
- [ ] Support dynamic rendering with React<sup>[1]</sup>
Expand Down
19 changes: 19 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"target": "es2019",
"module": "commonjs",
"declaration": true,
"composite": true,
"incremental": true,
"skipLibCheck": true,
"esModuleInterop": true,
"moduleResolution": "node",
"strictBindCallApply": true,
"jsx": "react-jsx",
"jsxImportSource": "@satorijs/element",
"types": [
"node",
"yml-register/types",
],
},
}
21 changes: 6 additions & 15 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
{
"extends": "./tsconfig.base",
"compilerOptions": {
"rootDir": "src",
"outDir": "lib",
"target": "es2020",
"module": "commonjs",
"declaration": true,
"composite": true,
"incremental": true,
"skipLibCheck": true,
"esModuleInterop": true,
"moduleResolution": "node",
"jsx": "react-jsx",
"jsxImportSource": "@satorijs/element",
"baseUrl": ".",
"paths": {
"koishi-plugin-imagify": ["packages/core/src"],
"koishi-plugin-imagify-*": ["packages/*/src"],
},
},
"include": [
"src",
],
}
5 changes: 5 additions & 0 deletions vercel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"github": {
"silent": true
}
}

0 comments on commit bd3d483

Please sign in to comment.