diff --git a/package.json b/package.json index 83a962c..f3eeaa1 100644 --- a/package.json +++ b/package.json @@ -98,8 +98,9 @@ ] }, "scripts": { - "build": "esbuild src/extension.ts --bundle --platform=node --format=cjs --external:vscode --outfile=dist/extension.js && esbuild src/xhtml.ts --bundle --platform=node --format=cjs --outfile=dist/xhtml.js && esbuild src/epub.ts --bundle --platform=node --format=cjs --external:vscode --outfile=dist/epub.js && esbuild src/state-manager.ts --bundle --platform=node --format=cjs --outfile=dist/state-manager.js && esbuild src/archive.ts --bundle --platform=node --format=cjs --outfile=dist/archive.js", + "build": "esbuild src/vscode/extension.ts --bundle --platform=node --format=cjs --external:vscode --outfile=dist/extension.js && esbuild src/core/xhtml.ts --bundle --platform=node --format=cjs --outfile=dist/xhtml.js && esbuild src/core/epub.ts --bundle --platform=node --format=cjs --external:vscode --outfile=dist/epub.js && esbuild src/core/state-manager.ts --bundle --platform=node --format=cjs --outfile=dist/state-manager.js && esbuild src/core/archive.ts --bundle --platform=node --format=cjs --outfile=dist/archive.js", "check": "tsc --noEmit", + "check:core": "tsc -p tsconfig.core.json --noEmit", "test": "npm run build && node --test test/*.test.mjs", "watch": "npm run build -- --watch" }, diff --git a/src/archive.ts b/src/core/archive.ts similarity index 100% rename from src/archive.ts rename to src/core/archive.ts diff --git a/src/epub.ts b/src/core/epub.ts similarity index 81% rename from src/epub.ts rename to src/core/epub.ts index d1d0c40..782676c 100644 --- a/src/epub.ts +++ b/src/core/epub.ts @@ -1,10 +1,20 @@ -import type * as vscodeTypes from 'vscode'; +export interface ResourceUri { + path: string; + fsPath?: string; + scheme?: string; + toString(): string; + with?(change: { path?: string }): ResourceUri; +} -let vscodeModule: typeof import('vscode') | undefined; -try { - vscodeModule = require('vscode'); -} catch { - // Allow running in pure Node.js environments (e.g. unit tests) +export function createResourceUri(filePath: string): ResourceUri { + const normalized = filePath.replace(/\\/g, '/'); + return { + path: normalized, + fsPath: filePath, + scheme: 'file', + toString: () => normalized, + with: (change: { path?: string }) => createResourceUri(change.path ?? normalized) + }; } export interface EpubManifestItem { @@ -12,27 +22,27 @@ export interface EpubManifestItem { href: string; mediaType: string; properties?: string; - uri: vscodeTypes.Uri; + uri: ResourceUri; } export interface EpubSpineItem { idref: string; linear?: boolean; href: string; - uri: vscodeTypes.Uri; + uri: ResourceUri; } export interface EpubTocItem { id?: string; label: string; href: string; - uri: vscodeTypes.Uri; + uri: ResourceUri; } export interface EpubPackage { - containerRoot: vscodeTypes.Uri; - opfUri: vscodeTypes.Uri; - opfDir: vscodeTypes.Uri; + containerRoot: ResourceUri; + opfUri: ResourceUri; + opfDir: ResourceUri; title: string; creator?: string; language?: string; @@ -44,28 +54,37 @@ export interface EpubPackage { /** * Ascends from the given source file directory to locate a META-INF/container.xml file. */ -export async function locateEpubContainer(source: vscodeTypes.Uri): Promise { - if (!vscodeModule) { - return undefined; - } +export async function locateEpubContainer( + source: ResourceUri, + fileExists?: (candidate: ResourceUri) => Promise +): Promise { + const checkExists = fileExists ?? (async (candidate: ResourceUri) => { + try { + const fs = await import('node:fs/promises'); + const targetPath = candidate.fsPath || candidate.path; + await fs.stat(targetPath); + return true; + } catch { + return false; + } + }); + let current = parentUri(source); - const root = vscodeModule.workspace.workspaceFolders?.[0]?.uri; for (let i = 0; i < 6; i++) { const candidate = joinPath(current, 'META-INF', 'container.xml'); try { - await vscodeModule.workspace.fs.stat(candidate); - return current; - } catch { - const parent = parentUri(current); - if (parent.toString() === current.toString()) { - break; - } - if (root && current.toString() === root.toString()) { - break; + if (await checkExists(candidate)) { + return current; } - current = parent; + } catch { + // ignore + } + const parent = parentUri(current); + if (parent.toString() === current.toString()) { + break; } + current = parent; } return undefined; } @@ -81,7 +100,7 @@ export function parseContainerXml(xml: string): string | undefined { /** * Safely parses the OPF Package Document into metadata, manifest, and spine. */ -export function parseOpfXml(xml: string, opfUri: vscodeTypes.Uri, containerRoot: vscodeTypes.Uri): EpubPackage { +export function parseOpfXml(xml: string, opfUri: ResourceUri, containerRoot: ResourceUri): EpubPackage { const opfDir = parentUri(opfUri); // Metadata @@ -162,7 +181,7 @@ export function parseOpfXml(xml: string, opfUri: vscodeTypes.Uri, containerRoot: /** * Parses EPUB 3 Navigation Document XHTML into TOC items. */ -export function parseNavHtml(html: string, baseDir: vscodeTypes.Uri): EpubTocItem[] { +export function parseNavHtml(html: string, baseDir: ResourceUri): EpubTocItem[] { const items: EpubTocItem[] = []; // Find