新增共享 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 多窗口、窗口状态、文件关联、链接与代码块均完成真实环境验收。
269 lines
7.3 KiB
TypeScript
269 lines
7.3 KiB
TypeScript
import { defaultExportConfig } from "@md-to-pdf/core";
|
|
import { describe, expect, it } from "vitest";
|
|
import {
|
|
buildPagedMediaCss,
|
|
continuousDocumentGeometryCss,
|
|
createPagedPreviewRenderRequest,
|
|
documentBaseCss,
|
|
documentGeometryCss,
|
|
documentInteractionCss,
|
|
formatPageNumber,
|
|
isPagedPreviewFrameMessage,
|
|
isPagedPreviewRenderRequest,
|
|
PAGED_PREVIEW_MESSAGE_SCOPE,
|
|
type PagedPreviewPayload
|
|
} from "../src/paged-preview.js";
|
|
|
|
const payload: PagedPreviewPayload = {
|
|
articleHtml: '<article id="write"><h1>分页测试</h1></article>',
|
|
fileName: "分页测试.md",
|
|
metadata: {
|
|
title: "分页测试",
|
|
author: "",
|
|
subject: "",
|
|
keywords: [],
|
|
language: "zh-CN"
|
|
},
|
|
features: [],
|
|
themeCss: "#write { color: #333; }",
|
|
exportConfig: defaultExportConfig
|
|
};
|
|
|
|
describe("分页预览协议", () => {
|
|
it("生成真实纸张尺寸和页边距 CSS", () => {
|
|
const css = buildPagedMediaCss(defaultExportConfig, payload);
|
|
|
|
expect(css).toContain("size: 210mm 297mm");
|
|
expect(css).toContain("margin: 16mm 16mm");
|
|
expect(css).toContain("@bottom-center");
|
|
expect(css).not.toContain("counter-reset: page");
|
|
expect(css).toContain("#write thead");
|
|
expect(css).toContain("display: table-header-group");
|
|
expect(css).toContain("break-inside: avoid");
|
|
expect(css).toContain(".pagedjs_pages");
|
|
expect(css).toContain("padding: 34px 0");
|
|
expect(css).toContain('html[data-render-target="preview"]');
|
|
expect(css).toContain("overflow-x: hidden");
|
|
expect(css).toContain("overflow-y: hidden");
|
|
expect(css).not.toContain("scrollbar-color");
|
|
expect(css).toContain('html[data-render-target="pdf"]');
|
|
expect(css).toContain("height: auto !important");
|
|
expect(css).toContain("overflow: visible !important");
|
|
expect(css).toContain("padding: 0");
|
|
expect(css).toContain("break-after: page");
|
|
expect(css).not.toContain("@media print");
|
|
});
|
|
|
|
it("安全替换页眉变量并生成三栏页边距盒", () => {
|
|
const css = buildPagedMediaCss(
|
|
{
|
|
...defaultExportConfig,
|
|
header: {
|
|
...defaultExportConfig.header,
|
|
enabled: true,
|
|
showDivider: true,
|
|
left: {
|
|
enabled: true,
|
|
content: '${title} — ${filename} "测试"'
|
|
},
|
|
center: {
|
|
enabled: true,
|
|
content: "${author}"
|
|
}
|
|
}
|
|
},
|
|
{
|
|
...payload,
|
|
metadata: {
|
|
...payload.metadata,
|
|
author: "内网团队"
|
|
}
|
|
}
|
|
);
|
|
|
|
expect(css).toContain("@top-left");
|
|
expect(css).toContain("@top-center");
|
|
expect(css).toContain("@top-right");
|
|
expect(css).toContain(
|
|
'content: "分页测试 — 分页测试.md \\"测试\\""'
|
|
);
|
|
expect(css).toContain('content: "内网团队"');
|
|
expect(css).toContain("border-bottom: 0.2mm solid currentColor");
|
|
});
|
|
|
|
it("支持自定义页码模板和起始页码", () => {
|
|
const css = buildPagedMediaCss(
|
|
{
|
|
...defaultExportConfig,
|
|
footer: {
|
|
...defaultExportConfig.footer,
|
|
format: "custom",
|
|
template: "第 ${page} / ${pages} 页",
|
|
alignment: "right",
|
|
startFrom: 5
|
|
}
|
|
},
|
|
payload
|
|
);
|
|
|
|
expect(css).toContain("@bottom-right");
|
|
expect(
|
|
formatPageNumber(
|
|
{
|
|
...defaultExportConfig.footer,
|
|
format: "custom",
|
|
template: "第 ${page} / ${pages} 页",
|
|
startFrom: 5
|
|
},
|
|
2,
|
|
8
|
|
)
|
|
).toBe("第 7 / 8 页");
|
|
});
|
|
|
|
it("逐页递增内置页码格式但保持真实总页数", () => {
|
|
expect(
|
|
formatPageNumber(defaultExportConfig.footer, 0, 5)
|
|
).toBe("1 / 5");
|
|
expect(
|
|
formatPageNumber(defaultExportConfig.footer, 1, 5)
|
|
).toBe("2 / 5");
|
|
expect(
|
|
formatPageNumber(
|
|
{
|
|
...defaultExportConfig.footer,
|
|
format: "chinese-page-total",
|
|
startFrom: 5
|
|
},
|
|
3,
|
|
5
|
|
)
|
|
).toBe("第 8 页 / 共 5 页");
|
|
expect(
|
|
formatPageNumber(
|
|
{
|
|
...defaultExportConfig.footer,
|
|
format: "dash-page"
|
|
},
|
|
4,
|
|
5
|
|
)
|
|
).toBe("- 5 -");
|
|
});
|
|
|
|
it("在主题 CSS 之后强制分页画布保持透明", () => {
|
|
expect(documentGeometryCss).toContain(
|
|
"background: transparent !important"
|
|
);
|
|
});
|
|
|
|
it("避免 Typora 围栏容器重复应用行内代码盒模型", () => {
|
|
expect(documentBaseCss).toContain(
|
|
"#write pre.md-fences > code"
|
|
);
|
|
expect(documentBaseCss).toContain("background: transparent;");
|
|
expect(documentBaseCss).toContain("font-size: inherit;");
|
|
expect(documentBaseCss).not.toMatch(
|
|
/#write pre\.md-fences > code\s*\{[^}]*!important/su
|
|
);
|
|
});
|
|
|
|
it("以低优先级兜底样式保留链接识别和焦点反馈", () => {
|
|
expect(documentInteractionCss).toContain(":where(#write a[href])");
|
|
expect(documentInteractionCss).toContain(
|
|
"text-decoration-line: underline;"
|
|
);
|
|
expect(documentInteractionCss).toContain(":focus-visible");
|
|
expect(documentInteractionCss).toContain("--md-accent");
|
|
expect(documentInteractionCss).not.toContain("!important");
|
|
});
|
|
|
|
it("连续预览由内容高度驱动且不创建内部滚动容器", () => {
|
|
expect(continuousDocumentGeometryCss).toContain(
|
|
"overflow: hidden !important"
|
|
);
|
|
expect(continuousDocumentGeometryCss).toContain(
|
|
"min-height: 0 !important"
|
|
);
|
|
expect(continuousDocumentGeometryCss).toContain(
|
|
'[data-continuous-render-stage="true"]'
|
|
);
|
|
expect(continuousDocumentGeometryCss).toContain(
|
|
"position: fixed !important"
|
|
);
|
|
expect(continuousDocumentGeometryCss).not.toContain("100vh");
|
|
});
|
|
|
|
it("生成并识别分页渲染请求", () => {
|
|
const request = createPagedPreviewRenderRequest(7, payload);
|
|
|
|
expect(request).toEqual({
|
|
scope: PAGED_PREVIEW_MESSAGE_SCOPE,
|
|
type: "render",
|
|
requestId: 7,
|
|
layout: "paged",
|
|
payload
|
|
});
|
|
expect(isPagedPreviewRenderRequest(request)).toBe(true);
|
|
expect(
|
|
createPagedPreviewRenderRequest(
|
|
8,
|
|
payload,
|
|
"continuous"
|
|
).layout
|
|
).toBe("continuous");
|
|
expect(
|
|
isPagedPreviewRenderRequest({
|
|
...request,
|
|
requestId: "7"
|
|
})
|
|
).toBe(false);
|
|
});
|
|
|
|
it("只接受分页 iframe 协议消息", () => {
|
|
expect(
|
|
isPagedPreviewFrameMessage({
|
|
scope: PAGED_PREVIEW_MESSAGE_SCOPE,
|
|
type: "link",
|
|
href: "https://example.com"
|
|
})
|
|
).toBe(true);
|
|
expect(
|
|
isPagedPreviewFrameMessage({
|
|
scope: PAGED_PREVIEW_MESSAGE_SCOPE,
|
|
type: "link",
|
|
href: ""
|
|
})
|
|
).toBe(false);
|
|
expect(
|
|
isPagedPreviewFrameMessage({
|
|
scope: PAGED_PREVIEW_MESSAGE_SCOPE,
|
|
type: "rendered",
|
|
requestId: 3,
|
|
layout: "paged",
|
|
pageCount: 2,
|
|
contentHeight: 2400,
|
|
echartsErrors: [],
|
|
mermaidErrors: []
|
|
})
|
|
).toBe(true);
|
|
expect(
|
|
isPagedPreviewFrameMessage({
|
|
scope: PAGED_PREVIEW_MESSAGE_SCOPE,
|
|
type: "rendered",
|
|
requestId: 3,
|
|
layout: "paged",
|
|
pageCount: 2,
|
|
echartsErrors: [],
|
|
mermaidErrors: []
|
|
})
|
|
).toBe(false);
|
|
expect(
|
|
isPagedPreviewFrameMessage({
|
|
scope: "other",
|
|
type: "ready"
|
|
})
|
|
).toBe(false);
|
|
});
|
|
});
|