Skip to content

Commit

Permalink
refactor: migrate to use yakumo for building
Browse files Browse the repository at this point in the history
migrate puppeteer to use plain html element
  • Loading branch information
MaikoTan committed Apr 1, 2024
1 parent 6926b5a commit e340b2e
Show file tree
Hide file tree
Showing 13 changed files with 7,392 additions and 91 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,9 @@ dist

lib
package-lock.json

.yarn/*
!.yarn/releases
!.yarn/plugins
!.yarn/sdks
!.yarn/versions
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@
"url": "git+https://github.com/AwesomeHamster/koishi-plugin-ffxiv-macrodict.git"
},
"scripts": {
"build": "yarn build:tsup --minify",
"dev": "yarn build:tsup",
"build:tsup": "tsup",
"build": "yakumo build",
"test": "mocha -r tsx -r yml-register --extension .spec.ts ./__tests__",
"lint": "eslint src/**/*.ts && yarn prettier --check",
"format": "yarn prettier --write",
Expand Down Expand Up @@ -90,14 +88,18 @@
"eslint-plugin-n": "^16.6.2",
"eslint-plugin-promise": "^6.1.1",
"koishi": "^4.17.2",
"koishi-plugin-puppeteer": "^3.3.1",
"koishi-plugin-puppeteer": "^3.8.3",
"markdown-magic": "^2.6.1",
"mocha": "^9.1.3",
"prettier": "^2.7.1",
"tsup": "^8.0.1",
"tsx": "^4.7.1",
"typescript": "^5.3.3",
"yml-register": "^1.1.0"
"yakumo": "^1.0.0-beta.8",
"yakumo-esbuild": "^1.0.0-beta.4",
"yakumo-mocha": "^1.0.0-beta.2",
"yakumo-tsc": "^1.0.0-beta.3",
"yml-register": "^1.2.5"
},
"peerDependencies": {
"koishi": "^4.15.7",
Expand Down
9 changes: 6 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Schema } from 'koishi'

import enUS from './locales/en-US.schema.yml'
import zhCN from './locales/zh-CN.schema.yml'
import { Locale, locales } from './utils'

export interface Config {
Expand All @@ -15,7 +17,8 @@ export const Config: Schema<Config> = Schema.object({
.default('auto'),
threshold: Schema.number().default(3),
}).i18n({
'zh': require('./locales/zh-CN/macrodict.schema.yml'),
'zh-CN': require('./locales/zh-CN/macrodict.schema.yml'),
'en': require('./locales/en-US/macrodict.schema.yml'),
'zh': zhCN,
'zh-CN': zhCN,
'en': enUS,
'en-US': enUS,
})
File renamed without changes.
File renamed without changes.
11 changes: 7 additions & 4 deletions src/locales/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import { Context } from 'koishi'

import enUS from './en-US.yml'
import zhCN from './zh-CN.yml'

export function apply(ctx: Context) {
ctx.i18n.define('en', require('./en-US/macrodict.yml'))
ctx.i18n.define('zh', require('./zh-CN/macrodict.yml'))
ctx.i18n.define('zh-CN', require('./zh-CN/macrodict.yml'))
ctx.i18n.define('en', enUS)
ctx.i18n.define('en-US', enUS)
ctx.i18n.define('zh', zhCN)
ctx.i18n.define('zh-CN', zhCN)
}
File renamed without changes.
File renamed without changes.
34 changes: 17 additions & 17 deletions src/parser.ts → src/parser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ const keyMap: { [k: string]: string } = {
/**
* Parse a macro definition text.
*/
export function parseMacroDescription(description: string, format: 'html' | 'text' = 'html'): string {
export function parseMacroDescription(description: string, format: 'html' | 'text' = 'html') {
const renderer = new Renderer(format)
let index = 0
let result = ''
const result: (JSX.IntrinsicElements | string)[] = []
while (index < description.length) {
const sub = description.substring(index)
if (/^\ue070/.test(sub)) {
// A weird character in Japanese macro description
result += '→'
result.push('→')
index += 1
} else if (/^<Indent\/>/.test(sub)) {
// The soft indent(?) in French / German macro description
result += ' '
result.push(' ')
index += 9
} else if (/^<SoftHyphen\/>/.test(sub)) {
// remove soft hyphen that is used in deutschen makro
Expand All @@ -43,28 +43,28 @@ export function parseMacroDescription(description: string, format: 'html' | 'tex
if (!m) {
throw new Error('parse error')
}
result += renderer.span(m[1], 'highlight')
result.push(renderer.span(m[1], 'highlight'))
index += m[0].length
} else if (/^<Gui\((\d+)\)\/>/.test(sub)) {
// replace <Gui(x)/> to corresponding keys
const m = /^<Gui\((\d+)\)\/>/.exec(sub)
if (!m) {
throw new Error('parse error')
}
result += renderer.kbd(keyMap[m[1]] || m[1])
result.push(renderer.kbd(keyMap[m[1]] || m[1]))
index += m[0].length
} else if (/^<(\w+)>/.test(sub)) {
const m = /^<(\w+)>/.exec(sub)
if (!m) {
throw new Error('parse error')
}
result += `&lt;${m[1]}&gt;`
result.push(`&lt;${m[1]}&gt;`)
index += m[0].length
} else if (/^\n/.test(sub)) {
result += renderer.br()
result.push(renderer.br())
index += 1
} else {
result += description[index]
result.push(description[index])
index += 1
}
}
Expand All @@ -79,19 +79,19 @@ class Renderer {
this.html = format === 'html'
}

p(text: string, className?: string): string {
return this.html ? `<p${className ? ` class="${className}"` : ''}>${text}</p>` : `${text}\n`
p(text: string, className = '') {
return this.html ? <p className={className}>{text}</p> : `${text}\n`
}

span(text: string, className?: string): string {
return this.html ? `<span${className ? ` class="${className}"` : ''}>${text}</span>` : ` ${text} `
span(text: string, className = '') {
return this.html ? <span className={className}>{text}</span> : ` ${text} `
}

kbd(text: string, className?: string): string {
return this.html ? `<kbd${className ? ` class="${className}"` : ''}>${text}</kbd>` : `[${text}]`
kbd(text: string, className = '') {
return this.html ? <kbd className={className}>{text}</kbd> : `[${text}]`
}

br(): string {
return this.html ? '<br>' : '\n'
br() {
return this.html ? <br /> : '\n'
}
}
80 changes: 21 additions & 59 deletions src/search.ts → src/search.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {} from 'koishi-plugin-puppeteer'
import { get as getMacro, search as searchMacro, nameToIdMap } from 'ffxiv-textcommand-data'
import { Context, Element, h, Service } from 'koishi'
import { Context, Element, Service } from 'koishi'

import { parseMacroDescription } from './parser'
import { Locale } from './utils'
Expand Down Expand Up @@ -45,25 +45,14 @@ export class Search extends Service {
}

async render(macro: Macro, info: { about: string; copyright: string }, lang: string): Promise<Element> {
const { puppeteer } = this.ctx

if (!puppeteer) {
throw new Error('Not found puppeteer.')
}

const { name, description } = macro
const descriptionHtml = parseMacroDescription(description)

const page = await puppeteer.page()

const { about, copyright } = info

await page.setContent(`
<!DOCTYPE html>
<html lang=${lang}>
<head>
<meta charset="UTF-8" />
<style>
return (
<html lang={lang}>
<style>{`
body {
margin: 0;
padding: 0;
Expand Down Expand Up @@ -125,49 +114,22 @@ export class Search extends Service {
footer > div #about {
text-align: right;
line-height: 0.8em;
}
</style>
<title>Macro</title>
</head>
<body>
<main id="container">
<div>
<h1 id="macro-name">${name}</h1>
<hr />
</div>
<div id="macro-description">${descriptionHtml}</div>
</main>
<footer>
<div>
<div id="copyright">${copyright}</div>
<div id="about">${about}</div>
</div>
<p>
FINAL FANTASY XIV © 2010 - 2023 SQUARE ENIX CO., LTD. All Rights Reserved.
</p>
</footer>
</body>
</html>
`)

// set the viewport to the same size as the page
const width = await page.evaluate(() => {
const ele = document.body
return ele.scrollWidth
})
await page.setViewport({
width,
height: 200,
})

// take a screenshot
const screenshot = (await page.screenshot({
fullPage: true,
type: 'png',
})) as Buffer

// don't forget to close the page
await page.close()
return h.image(screenshot, 'image/png')
}`}</style>
<main id='container'>
<div>
<h1 id='macro-name'>{name}</h1>
<hr />
</div>
<div id='macro-description'>{descriptionHtml}</div>
</main>
<footer>
<div>
<div id='copyright'>{copyright}</div>
<div id='about'>{about}</div>
</div>
<p>FINAL FANTASY XIV © 2010 - 2023 SQUARE ENIX CO., LTD. All Rights Reserved.</p>
</footer>
</html>
)
}
}
10 changes: 7 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{
"extends": "@hamster-bot/tsconfig",
"compilerOptions": {
"outDir": "dist",
"types": ["mocha"]
"outDir": "lib",
"composite": true,
"incremental": true,
"types": ["mocha", "yml-register/types"],
"jsx": "react-jsx",
"jsxImportSource": "@satorijs/element",
},
"include": ["src/**/*.ts"]
"include": ["src/**/*.ts", "src/*.tsx"]
}
11 changes: 11 additions & 0 deletions yakumo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- name: yakumo
config:
pipeline:
build:
- tsc
- esbuild
clean:
- tsc --clean
- name: yakumo-esbuild
- name: yakumo-mocha
- name: yakumo-tsc
Loading

0 comments on commit e340b2e

Please sign in to comment.