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:
@@ -8,11 +8,13 @@
|
||||
```text
|
||||
src/
|
||||
document.ts Markdown 文档、分页载荷、结果与耗时模型
|
||||
document-link.ts 跨端链接分类与 PDF 本地链接编码协议
|
||||
export-config.ts 纸张、边距、页眉页脚、页码与图表配置
|
||||
theme.ts 主题清单、主题能力与 CSS 载荷模型
|
||||
index.ts 公共导出入口
|
||||
tests/
|
||||
document.test.ts
|
||||
document-link.test.ts
|
||||
export-config.test.ts
|
||||
```
|
||||
|
||||
@@ -20,11 +22,13 @@ tests/
|
||||
|
||||
```ts
|
||||
import {
|
||||
classifyDocumentLink,
|
||||
createPagedDocumentPayload,
|
||||
defaultExportConfig,
|
||||
exportConfigSchema
|
||||
} from "@md-to-pdf/core";
|
||||
|
||||
const link = classifyDocumentLink("../docs/example.md");
|
||||
const config = exportConfigSchema.parse(defaultExportConfig);
|
||||
const payload = createPagedDocumentPayload({
|
||||
document,
|
||||
@@ -34,6 +38,11 @@ const payload = createPagedDocumentPayload({
|
||||
});
|
||||
```
|
||||
|
||||
`classifyDocumentLink()` 只负责稳定分类,不执行平台动作。Web 根据分类
|
||||
处理锚点和网络链接;Desktop 决定是否调用浏览器、系统程序或打开新的
|
||||
Markdown 窗口。精确 PDF 使用保留的 `.invalid` 地址暂存本地链接,
|
||||
PDF.js 注释层展示时再安全解码,避免 Chromium 将相对路径误转成站点 URL。
|
||||
|
||||
新增跨端字段时,应先在这里定义类型、默认值、Zod 校验和兼容迁移,再由
|
||||
各适配层消费,避免 Web 与 Desktop 分别维护协议。
|
||||
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
export type DocumentLinkKind =
|
||||
| "anchor"
|
||||
| "network"
|
||||
| "protocol"
|
||||
| "local"
|
||||
| "unsafe"
|
||||
| "invalid";
|
||||
|
||||
export interface ClassifiedDocumentLink {
|
||||
kind: DocumentLinkKind;
|
||||
href: string;
|
||||
normalizedHref: string;
|
||||
scheme?: string;
|
||||
}
|
||||
|
||||
const schemePattern = /^([a-z][a-z\d+.-]*):/iu;
|
||||
const windowsAbsolutePathPattern = /^[a-z]:[\\/]/iu;
|
||||
const windowsUncPathPattern = /^\\\\[^\\]/u;
|
||||
const unsafeSchemePattern = /^(?:data|javascript|vbscript)$/u;
|
||||
const controlCharacterPattern = /[\u0000-\u001f\u007f]/u;
|
||||
const pdfLocalLinkOrigin = "https://mdpdf.local.invalid";
|
||||
const pdfLocalLinkPath = "/document-link";
|
||||
|
||||
function result(
|
||||
kind: DocumentLinkKind,
|
||||
href: string,
|
||||
normalizedHref = href,
|
||||
scheme?: string
|
||||
): ClassifiedDocumentLink {
|
||||
return {
|
||||
kind,
|
||||
href,
|
||||
normalizedHref,
|
||||
...(scheme ? { scheme } : {})
|
||||
};
|
||||
}
|
||||
|
||||
export function classifyDocumentLink(
|
||||
rawHref: string
|
||||
): ClassifiedDocumentLink {
|
||||
const href = rawHref.trim();
|
||||
if (!href || controlCharacterPattern.test(href)) {
|
||||
return result("invalid", href);
|
||||
}
|
||||
|
||||
if (href.startsWith("#")) {
|
||||
return result("anchor", href);
|
||||
}
|
||||
|
||||
if (
|
||||
windowsAbsolutePathPattern.test(href) ||
|
||||
windowsUncPathPattern.test(href)
|
||||
) {
|
||||
return result("local", href);
|
||||
}
|
||||
|
||||
if (href.startsWith("//")) {
|
||||
return result("network", href, `https:${href}`, "https");
|
||||
}
|
||||
|
||||
const schemeMatch = schemePattern.exec(href);
|
||||
if (!schemeMatch) {
|
||||
return result("local", href);
|
||||
}
|
||||
|
||||
const scheme = schemeMatch[1]?.toLowerCase() ?? "";
|
||||
if (unsafeSchemePattern.test(scheme)) {
|
||||
return result("unsafe", href, href, scheme);
|
||||
}
|
||||
|
||||
if (scheme === "http" || scheme === "https") {
|
||||
try {
|
||||
const url = new URL(href);
|
||||
if (!url.hostname) {
|
||||
return result("invalid", href, href, scheme);
|
||||
}
|
||||
return result("network", href, href, scheme);
|
||||
} catch {
|
||||
return result("invalid", href, href, scheme);
|
||||
}
|
||||
}
|
||||
|
||||
if (scheme === "file") {
|
||||
return result("local", href, href, scheme);
|
||||
}
|
||||
|
||||
return result("protocol", href, href, scheme);
|
||||
}
|
||||
|
||||
export function isSafeDocumentLink(rawHref: string) {
|
||||
const { kind } = classifyDocumentLink(rawHref);
|
||||
return kind !== "unsafe" && kind !== "invalid";
|
||||
}
|
||||
|
||||
export function encodeLocalDocumentLinkForPdf(rawHref: string) {
|
||||
const link = classifyDocumentLink(rawHref);
|
||||
if (link.kind !== "local") {
|
||||
return undefined;
|
||||
}
|
||||
const encoded = new URL(pdfLocalLinkPath, pdfLocalLinkOrigin);
|
||||
encoded.searchParams.set("href", link.href);
|
||||
return encoded.href;
|
||||
}
|
||||
|
||||
export function decodeLocalDocumentLinkFromPdf(rawHref: string) {
|
||||
let encoded: URL;
|
||||
try {
|
||||
encoded = new URL(rawHref);
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
if (
|
||||
encoded.origin !== pdfLocalLinkOrigin ||
|
||||
encoded.pathname !== pdfLocalLinkPath
|
||||
) {
|
||||
return undefined;
|
||||
}
|
||||
const href = encoded.searchParams.get("href");
|
||||
if (!href || classifyDocumentLink(href).kind !== "local") {
|
||||
return undefined;
|
||||
}
|
||||
return href;
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from "./document.js";
|
||||
export * from "./document-link.js";
|
||||
export * from "./export-config.js";
|
||||
export * from "./theme.js";
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
classifyDocumentLink,
|
||||
decodeLocalDocumentLinkFromPdf,
|
||||
encodeLocalDocumentLinkForPdf,
|
||||
isSafeDocumentLink
|
||||
} from "../src/document-link.js";
|
||||
|
||||
describe("文档链接分类", () => {
|
||||
it("识别文档锚点和网络链接", () => {
|
||||
expect(classifyDocumentLink("#章节一").kind).toBe("anchor");
|
||||
expect(
|
||||
classifyDocumentLink("https://example.com/a").kind
|
||||
).toBe("network");
|
||||
expect(
|
||||
classifyDocumentLink("//example.com/a").normalizedHref
|
||||
).toBe("https://example.com/a");
|
||||
});
|
||||
|
||||
it("识别浏览器协议处理器和未知协议", () => {
|
||||
expect(classifyDocumentLink("mailto:a@example.com")).toMatchObject({
|
||||
kind: "protocol",
|
||||
scheme: "mailto"
|
||||
});
|
||||
expect(classifyDocumentLink("tel:+8612345")).toMatchObject({
|
||||
kind: "protocol",
|
||||
scheme: "tel"
|
||||
});
|
||||
expect(classifyDocumentLink("obsidian://open?vault=x")).toMatchObject({
|
||||
kind: "protocol",
|
||||
scheme: "obsidian"
|
||||
});
|
||||
});
|
||||
|
||||
it("识别跨目录、绝对路径、UNC 和 file URI", () => {
|
||||
for (const href of [
|
||||
"../docs/a.md",
|
||||
"/opt/docs/a.md",
|
||||
"C:/docs/a.md",
|
||||
"D:\\docs\\a.md",
|
||||
"\\\\server\\share\\a.md",
|
||||
"file:///C:/docs/a.md"
|
||||
]) {
|
||||
expect(classifyDocumentLink(href).kind).toBe("local");
|
||||
}
|
||||
});
|
||||
|
||||
it("拒绝危险协议、控制字符和无效网络地址", () => {
|
||||
for (const href of [
|
||||
"javascript:alert(1)",
|
||||
"vbscript:msgbox(1)",
|
||||
"data:text/html;base64,WA=="
|
||||
]) {
|
||||
expect(classifyDocumentLink(href).kind).toBe("unsafe");
|
||||
expect(isSafeDocumentLink(href)).toBe(false);
|
||||
}
|
||||
expect(classifyDocumentLink("https://[invalid").kind).toBe(
|
||||
"invalid"
|
||||
);
|
||||
expect(classifyDocumentLink("a\u0000b").kind).toBe("invalid");
|
||||
});
|
||||
|
||||
it("为 PDF 注释编码并还原本地链接", () => {
|
||||
for (const href of [
|
||||
"../docs/说明.md#章节",
|
||||
"C:/docs/a.md",
|
||||
"file:///D:/docs/a.md"
|
||||
]) {
|
||||
const encoded = encodeLocalDocumentLinkForPdf(href);
|
||||
expect(encoded).toMatch(
|
||||
/^https:\/\/mdpdf\.local\.invalid\/document-link\?/u
|
||||
);
|
||||
expect(decodeLocalDocumentLinkFromPdf(encoded ?? "")).toBe(
|
||||
href
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
it("不编码网络链接且拒绝伪造的 PDF 本地链接", () => {
|
||||
expect(
|
||||
encodeLocalDocumentLinkForPdf("https://example.com")
|
||||
).toBeUndefined();
|
||||
expect(
|
||||
decodeLocalDocumentLinkFromPdf(
|
||||
"https://mdpdf.local.invalid/document-link?href=https%3A%2F%2Fexample.com"
|
||||
)
|
||||
).toBeUndefined();
|
||||
expect(
|
||||
decodeLocalDocumentLinkFromPdf(
|
||||
"https://example.com/document-link?href=..%2Fa.md"
|
||||
)
|
||||
).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -32,6 +32,7 @@ describe("导出配置", () => {
|
||||
});
|
||||
|
||||
it("使用 A4 和 16mm 作为默认纸张配置", () => {
|
||||
expect(defaultExportConfig.themeId).toBe("typora-github");
|
||||
expect(defaultExportConfig.paper).toEqual({
|
||||
format: "A4",
|
||||
orientation: "portrait",
|
||||
|
||||
Reference in New Issue
Block a user