release: 发布 v0.5.0

新增共享 Preview Engine,统一 Web 连续预览、快速分页、Playwright PDF 与 Electron PDF;实现稳定前缀复用和修改位置后的增量分页,保留媒体块按文档顺序串行回填与单次重排。

完善跨端链接与桌面文档工作流:Web 受控处理锚点和 HTTP/HTTPS 外链;Desktop 支持本地路径、file URI、系统协议、多窗口、同文件单例、Markdown 当前或新窗口打开,以及聚焦时外部文件变化提示。

统一四套内置主题名称并默认使用 Typora Github;修复连续预览双滚动条、ECharts 尺寸、PDF 本地链接、围栏代码块 Typora DOM 与重复行内样式;桌面发行链强制完整重建内嵌 Web,避免安装包携带陈旧资源。

发布 Web/Compose 与 Windows NSIS/ZIP:镜像 yixiong/md-to-pdf:v0.5.0 已健康部署;NSIS SHA-256 为 60992D1FDCA513F46346C78478537EB4159D8C0E76B41ECF3CDC25BE77707D92,ZIP SHA-256 为 D74F82293FB67126E583546CBA894569EFC9A0B1B6343FAC648CCC95F1D188D8,本机安装版已升级至 v0.5.0。

验证:全项目 238 项测试通过,类型检查、生产构建和 git diff --check 通过;Web 快速/连续/精确预览、Compose、Desktop 多窗口、窗口状态、文件关联、链接与代码块均完成真实环境验收。
This commit is contained in:
SkyJourney
2026-07-28 18:01:22 +08:00
parent 925b0d1485
commit 58087d0c7e
86 changed files with 4564 additions and 1204 deletions
+5
View File
@@ -15,6 +15,7 @@ src/
index.ts 公共导出入口
tests/
application-service.test.ts
bundled-themes.test.ts
image-resources.test.ts
```
@@ -36,6 +37,10 @@ Web 端通过 `apps/server` 的 HTTP API 调用;桌面端在主进程中创建
服务,并通过受限 IPC 暴露给渲染进程。Web 默认不接收本地素材目录,
Desktop 才会以 Markdown 所在目录为边界解析相对资源。
内置主题来自仓库 `themes/`,当前名称为 Typora Github、
Typora Pixyll、Typora whitey 和 Typora Clean。额外主题从平台传入的
本地主题根目录扫描;与内置主题 ID 冲突时以内置主题为准。
## 开发与验证
```powershell
+59 -3
View File
@@ -2,6 +2,7 @@ import { lookup } from "node:dns/promises";
import { readFile, realpath, stat } from "node:fs/promises";
import { isIP } from "node:net";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { extractMarkdownImageSources } from "@md-to-pdf/renderer";
export const MAXIMUM_IMAGE_COUNT = 50;
@@ -22,6 +23,7 @@ export interface MarkdownImageResource {
export interface ImageResolutionContext {
localRoot?: string;
allowUnrestrictedLocalFiles?: boolean;
}
export interface ResolvedMarkdownImages {
@@ -313,6 +315,46 @@ async function loadLocalImage(root: string, source: string) {
};
}
function decodeUnrestrictedLocalPath(source: string) {
if (/^file:/iu.test(source)) {
try {
return fileURLToPath(new URL(source));
} catch {
throw new Error("file: 图片路径格式无效");
}
}
const pathOnly = source.split(/[?#]/u, 1)[0] ?? "";
try {
return decodeURIComponent(pathOnly);
} catch {
throw new Error("图片路径 URL 编码无效");
}
}
async function loadUnrestrictedLocalImage(
root: string,
source: string
) {
const decoded = decodeUnrestrictedLocalPath(source);
const target =
path.isAbsolute(decoded) || /^[a-z]:[\\/]/iu.test(decoded)
? path.normalize(decoded)
: path.resolve(root, decoded);
const realTarget = await realpath(target);
const targetStat = await stat(realTarget);
if (!targetStat.isFile()) {
throw new Error("图片资源不是文件");
}
if (targetStat.size > MAXIMUM_IMAGE_BYTES) {
throw new Error("本地图片超过单文件大小限制");
}
const content = await readFile(realTarget);
return {
content,
contentType: detectImageContentType(content)
};
}
function parseUploadedResources(value: unknown) {
if (value === undefined) {
return new Map<string, LoadedImage>();
@@ -413,12 +455,26 @@ export function createImageResourceResolver(
if (/^https?:\/\//iu.test(source)) {
image = await loadRemote(source);
} else {
const normalized = normalizeDocumentAssetPath(source);
const uploadedImage = uploaded.get(normalized);
let normalized: string | undefined;
try {
normalized = normalizeDocumentAssetPath(source);
} catch (error) {
if (!context.allowUnrestrictedLocalFiles) {
throw error;
}
}
const uploadedImage = normalized
? uploaded.get(normalized)
: undefined;
if (uploadedImage) {
image = uploadedImage;
} else if (context.localRoot) {
image = await loadLocalImage(context.localRoot, source);
image = context.allowUnrestrictedLocalFiles
? await loadUnrestrictedLocalImage(
context.localRoot,
source
)
: await loadLocalImage(context.localRoot, source);
} else {
throw new Error("未提供对应素材目录");
}
@@ -0,0 +1,30 @@
import { readFile } from "node:fs/promises";
import { fileURLToPath } from "node:url";
import { describe, expect, it } from "vitest";
const bundledThemeNames = {
"typora-github": "Typora Github",
"typora-pixyll": "Typora Pixyll",
"typora-whitey": "Typora whitey",
"typora-like": "Typora Clean"
} as const;
describe("内置主题清单", () => {
it("使用稳定主题 ID 和面向用户的显示名称", async () => {
const themesRoot = fileURLToPath(
new URL("../../../themes/", import.meta.url)
);
for (const [id, name] of Object.entries(bundledThemeNames)) {
const manifest = JSON.parse(
await readFile(
`${themesRoot}${id}/theme.json`,
"utf8"
)
) as { id?: unknown; name?: unknown };
expect(manifest.id).toBe(id);
expect(manifest.name).toBe(name);
}
});
});
@@ -1,3 +1,7 @@
import { mkdtemp, rm, writeFile } from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { pathToFileURL } from "node:url";
import { describe, expect, it, vi } from "vitest";
import {
createImageResourceResolver,
@@ -59,4 +63,41 @@ describe("Markdown 图片资源安全", () => {
);
expect(failed.warnings[0]).toContain("网络不可用");
});
it("仅在显式授权时允许桌面读取上级、绝对及 file URI 图片", async () => {
const directory = await mkdtemp(
path.join(os.tmpdir(), "md-to-pdf-local-images-")
);
const documentRoot = path.join(directory, "docs");
const imagePath = path.join(directory, "共享图片.png");
try {
await writeFile(imagePath, png);
const resolve = createImageResourceResolver();
const sources = [
"../共享图片.png",
imagePath,
pathToFileURL(imagePath).href
];
const markdown = sources
.map((source) => `![图](${source.replace(/ /gu, "%20")})`)
.join("\n");
const restricted = await resolve(markdown, undefined, {
localRoot: documentRoot
});
expect(restricted.warnings).toHaveLength(3);
const unrestricted = await resolve(markdown, undefined, {
localRoot: documentRoot,
allowUnrestrictedLocalFiles: true
});
expect(unrestricted.warnings).toEqual([]);
expect(unrestricted.sources).toHaveLength(3);
for (const dataUrl of unrestricted.sources.values()) {
expect(dataUrl).toMatch(/^data:image\/png;base64,/u);
}
} finally {
await rm(directory, { recursive: true, force: true });
}
});
});