Skip to content

Commit

Permalink
feat: replace links
Browse files Browse the repository at this point in the history
  • Loading branch information
OpportunityLiu committed Sep 3, 2024
1 parent 0b06007 commit 7cd8b8e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
7 changes: 6 additions & 1 deletion tools/docs-to-pdf/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
/**
* 生成文档的页面域名,不包含末尾的 `/`
* 生成文档的使用的域名,不包含末尾的 `/`
*/
export const HOST = 'http://localhost:3000';

/**
* 生成文档的域名,不包含末尾的 `/`
*/
export const HOST_REPLACE = 'https://kb.cloudpss.net';
13 changes: 8 additions & 5 deletions tools/docs-to-pdf/src/generate-pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import {
PDFName,
PDFNull,
PDFNumber,
type PDFString,
PDFString,
StandardFonts,
type PDFPage,
type PDFRef,
StandardFonts,
} from 'pdf-lib';
import { HOST } from './constants.ts';
import type { PrintedDocument } from './print-pages.ts';
Expand Down Expand Up @@ -155,8 +155,8 @@ export class PdfGenerator {
for (const page of this.pdf.getPages()) {
const annotations = page.node.Annots();
if (!annotations) continue;
for (const ref of annotations.asArray()) {
const annotation = this.pdf.context.lookup(ref, PDFDict);
for (let index = 0; index < annotations.size(); index++) {
const annotation = annotations.lookupMaybe(index, PDFDict);
if (
!annotation ||
(annotation.get(PDFName.of('Subtype')) as PDFName | undefined)?.asString() !== '/Link'
Expand All @@ -166,7 +166,10 @@ export class PdfGenerator {
if (!a) continue;
const uri = a.get(PDFName.of('URI')) as PDFString | undefined;
if (!uri) continue;
const linkRef = toDocumentRef(new URL(uri.asString()));
const uriString = uri.asString();
if (!uriString.startsWith(HOST)) continue;
a.set(PDFName.of('URI'), PDFString.of(uriString.replace(HOST, 'https://kb.cloudpss.net')));
const linkRef = toDocumentRef(new URL(uriString));
const node = this.outline.get(linkRef);
if (!node) continue;
annotation.set(PDFName.of('Dest'), node.pageDist.clone());
Expand Down

0 comments on commit 7cd8b8e

Please sign in to comment.