Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

2.6 KB
Raw Blame
export interface IPdfAssetLink {
    linkAddress: string;
    pdfParams?: number | string;
}

const PDF_ANNOTATION_PATH = /^(assets\/(?:[^/]+\/)*[^/]+\.[pP][dD][fF])\/(\d{14}-[a-z0-9]{7})$/;

export const getPdfAnnotationReference = (text: string): string | undefined => {
    const match = /^<<(.+?)(?: "(?:[^"\\]|\\.)*")?>>$/.exec(text);
    if (!match || /[<>"\r\n\\]/.test(match[1])) {
        return undefined;
    }
    const path = match[1].split(/[?#]/, 1)[0];
    if (!PDF_ANNOTATION_PATH.test(path) || path.split("/").some(part => !part || part === "." || part === "..")) {
        return undefined;
    }
    return match[1];
};

const decodeQueryComponent = (value: string) => {
    try {
        return decodeURIComponent(value.replace(/\+/g, " "));
    } catch {
        return value;
    }
};

export const appendPdfAnnotationId = (linkAddress: string, annotationId: string) => {
    const suffixIndex = linkAddress.search(/[?#]/);
    if (suffixIndex < 0) {
        return `${linkAddress}/${annotationId}`;
    }
    return `${linkAddress.substring(0, suffixIndex)}/${annotationId}${linkAddress.substring(suffixIndex)}`;
};

export const resolvePdfAssetLink = (linkAddress: string): IPdfAssetLink => {
    const hashIndex = linkAddress.indexOf("#");
    const addressWithoutHash = hashIndex < 0 ? linkAddress : linkAddress.substring(0, hashIndex);
    const queryIndex = addressWithoutHash.indexOf("?");
    const path = queryIndex < 0 ? addressWithoutHash : addressWithoutHash.substring(0, queryIndex);
    const annotationMatch = PDF_ANNOTATION_PATH.exec(path);
    const pdfPath = annotationMatch?.[1] || path;
    if (!/^assets\/.+\.pdf$/i.test(pdfPath)) {
        return {linkAddress};
    }

    const query = queryIndex < 0 ? "" : addressWithoutHash.substring(queryIndex + 1);
    let page: string | undefined;
    const remainingQuery = query ? query.split("&").filter((item) => {
        const separatorIndex = item.indexOf("=");
        const key = separatorIndex < 0 ? item : item.substring(0, separatorIndex);
        if (decodeQueryComponent(key) !== "page") {
            return true;
        }
        if (typeof page === "undefined") {
            const value = separatorIndex < 0 ? "" : item.substring(separatorIndex + 1);
            page = decodeQueryComponent(value);
        }
        return false;
    }).join("&") : "";
    const parsedPage = typeof page === "undefined" ? undefined : Number.parseInt(page, 10);
    const pdfParams = annotationMatch?.[2] || (Number.isNaN(parsedPage) ? undefined : parsedPage);

    return {
        linkAddress: pdfPath + (remainingQuery ? `?${remainingQuery}` : ""),
        pdfParams,
    };
};