Files
genoffice/packages/html2docx/tools/cli.ts
T
merrick-2002andGenOffice d24c964a3e Sync snapshot (2026-09-09) (#271)
- **New app: GenOffice HTML.** Open, edit and generate .html pages: a source pane with a sandboxed live preview, click-to-select elements with a floating toolbar and style panel, device-size preview, image tools and a present mode. AI generates whole pages from a brief with style directions and offers page-wide Restyle, Theme and Summarize. Export to Word runs locally through the built-in html2docx converter (native, editable OOXML; no print-to-PDF detour).
- **AI:** separate BYOK providers for web search, image generation and image/video analysis; new Codex App Server provider; Ask AI quotes the selected text and keeps it highlighted while it runs; custom AI panel text size; blocked requests retry through the Chromium network stack.
- **App:** global AutoSave setting shared by every editor; AI panel text size and chat spellcheck toggles (genoffice#249); Czech (cs) UI locale (genoffice#259); Save As defaults to the document's folder; PDF export no longer aborts on CJK file paths on Windows.
- **Docs:** Export as HTML; comments and replies edit in place from the comments pane; the caret can leave a trailing table (genoffice#266); spellcheck re-marks text as soon as it is re-enabled; AI edits are character-precise on partial selections; zoom keeps the scroll position; pasting a cell from Sheets takes the destination font.
- **Sheets:** Find no longer freezes on wide or streamed grids; Replace searches as you type; cached values of external-workbook formulas are kept instead of #ERROR! (genoffice#235); "number stored as text" is a warning, not an error (genoffice#236); paste repeats into the target range like Excel; filter criteria survive save/reopen; saves retry when Windows locks the file; many Excel-fidelity fixes for column widths, row heights, charts, slicers, number formats and CJK fonts.
- **Slides:** PDF export works for large presentations (genoffice#264); EMF/WMF picture fonts and East Asian theme fonts resolve correctly; decks with missing layouts or embedded styles still open; outline-only pie slices render.
- **PDF:** highlight and underline geometry aligns with the text; Shift+arrows extend the selection instead of paging.
- **Markdown:** Find and replace (Ctrl+F / Ctrl+H) (genoffice#253); mermaid code blocks render as diagrams (genoffice#263); better print quality.
- Thanks to community contributors @GoodScholar, @aniruddhaadak80 and @Sun8854, and to @hanyscz, @qgzln, @zyp997, @bisslot, @krakotay and @Ben03151 for the reports.

Co-authored-by: GenOffice <merrick-2002@users.noreply.github.com>
2026-09-09 18:19:13 +08:00

82 lines
2.9 KiB
TypeScript

/**
* html2docx <input.html> <output.docx> [--verbose]
*
* Runs through Vite's SSR loader so the extractor's `?raw` fragment imports
* resolve the same way they do in the app bundle and in vitest:
* npx tsx packages/html2docx/tools/cli.ts in.html out.docx
*
* Debug env: H2D_SHOT_TEXT_PATH (write rasterized text), H2D_IR_PATH (dump
* IR + trace), H2D_TRACE_SELECTOR (in-page probes).
*/
import { writeFileSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'
import { createServer } from 'vite'
import type { ConvertOptions, ConvertResult } from '../src'
import type { PlaywrightDriver as PlaywrightDriverType } from '../src/drivers/playwright'
const A4 = { width: 794, height: 1123, deviceScaleFactor: 2 }
async function main(): Promise<void> {
const verbose = process.argv.includes('--verbose') || process.argv.includes('-v')
const args = process.argv.slice(2).filter((a) => a !== '--verbose' && a !== '-v')
if (args.length < 2) {
console.error('Usage: html2docx <input.html> <output.docx> [--verbose]')
process.exit(1)
}
const [input, output] = args
const root = resolve(dirname(fileURLToPath(import.meta.url)), '..')
const vite = await createServer({
root,
configFile: false,
logLevel: 'error',
server: { middlewareMode: true, hmr: false },
optimizeDeps: { noDiscovery: true },
})
try {
const lib = (await vite.ssrLoadModule('/src/index.ts')) as {
convertHtmlToDocx: (
input: { url: string },
driver: PlaywrightDriverType,
options: ConvertOptions,
) => Promise<ConvertResult>
}
const { PlaywrightDriver, launchChrome } = (await vite.ssrLoadModule(
'/src/drivers/playwright.ts',
)) as typeof import('../src/drivers/playwright')
const browser = await launchChrome()
try {
const driver = await PlaywrightDriver.create(browser, A4)
const url = input.startsWith('http') ? input : pathToFileURL(resolve(input)).href
const result = await lib.convertHtmlToDocx({ url }, driver, {
log: verbose ? (m) => console.error(m) : undefined,
traceSelector: process.env.H2D_TRACE_SELECTOR,
onIr: process.env.H2D_IR_PATH
? (ir, trace) => {
writeFileSync(`${process.env.H2D_IR_PATH}.trace`, JSON.stringify(trace, null, 1))
writeFileSync(
process.env.H2D_IR_PATH!,
JSON.stringify(ir, (key, value) => (key === 'buffer' ? undefined : value), 1),
)
}
: undefined,
})
await driver.close()
writeFileSync(output, result.docx)
if (process.env.H2D_SHOT_TEXT_PATH) {
writeFileSync(process.env.H2D_SHOT_TEXT_PATH, result.screenshotText, 'utf8')
}
console.log(output)
} finally {
await browser.close()
}
} finally {
await vite.close()
}
}
main().catch((e) => {
console.error(e)
process.exit(1)
})