hande hyplerlink references in header or footers
This commit is contained in:
@ -55,6 +55,15 @@ export class FooterWrapper {
|
|||||||
return mediaData;
|
return mediaData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public addHyperlinkRelationship(target: string, refId: number, targetMode?: "External" | undefined): void {
|
||||||
|
this.relationships.createRelationship(
|
||||||
|
refId,
|
||||||
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
|
||||||
|
target,
|
||||||
|
targetMode,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public createImage(image: Buffer, width?: number, height?: number): void {
|
public createImage(image: Buffer, width?: number, height?: number): void {
|
||||||
const mediaData = this.addImageRelationship(image, this.relationships.RelationshipCount, width, height);
|
const mediaData = this.addImageRelationship(image, this.relationships.RelationshipCount, width, height);
|
||||||
this.addImage(new Image(new ImageParagraph(mediaData)));
|
this.addImage(new Image(new ImageParagraph(mediaData)));
|
||||||
|
@ -55,6 +55,15 @@ export class HeaderWrapper {
|
|||||||
return mediaData;
|
return mediaData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public addHyperlinkRelationship(target: string, refId: number, targetMode?: "External" | undefined): void {
|
||||||
|
this.relationships.createRelationship(
|
||||||
|
refId,
|
||||||
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
|
||||||
|
target,
|
||||||
|
targetMode,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public createImage(image: Buffer, width?: number, height?: number): void {
|
public createImage(image: Buffer, width?: number, height?: number): void {
|
||||||
const mediaData = this.addImageRelationship(image, this.relationships.RelationshipCount, width, height);
|
const mediaData = this.addImageRelationship(image, this.relationships.RelationshipCount, width, height);
|
||||||
this.addImage(new Image(new ImageParagraph(mediaData)));
|
this.addImage(new Image(new ImageParagraph(mediaData)));
|
||||||
|
@ -20,6 +20,7 @@ const schemeToType = {
|
|||||||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/header": "header",
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/header": "header",
|
||||||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer": "footer",
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer": "footer",
|
||||||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image": "image",
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image": "image",
|
||||||
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink": "hyperlink",
|
||||||
};
|
};
|
||||||
|
|
||||||
interface IDocumentRefs {
|
interface IDocumentRefs {
|
||||||
@ -29,8 +30,8 @@ interface IDocumentRefs {
|
|||||||
|
|
||||||
interface IRelationshipFileInfo {
|
interface IRelationshipFileInfo {
|
||||||
id: number;
|
id: number;
|
||||||
targetFile: string;
|
target: string;
|
||||||
type: "header" | "footer" | "image";
|
type: "header" | "footer" | "image" | "hyperlink";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Document Template
|
// Document Template
|
||||||
@ -70,7 +71,7 @@ export class ImportDotx {
|
|||||||
throw new Error(`Can not find target file for id ${headerRef.id}`);
|
throw new Error(`Can not find target file for id ${headerRef.id}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const xmlData = await zipContent.files[`word/${relationFileInfo.targetFile}`].async("text");
|
const xmlData = await zipContent.files[`word/${relationFileInfo.target}`].async("text");
|
||||||
const xmlObj = fastXmlParser.parse(xmlData, importParseOptions);
|
const xmlObj = fastXmlParser.parse(xmlData, importParseOptions);
|
||||||
|
|
||||||
const importedComp = convertToXmlComponent(headerKey, xmlObj[headerKey]) as ImportedXmlComponent;
|
const importedComp = convertToXmlComponent(headerKey, xmlObj[headerKey]) as ImportedXmlComponent;
|
||||||
@ -87,7 +88,7 @@ export class ImportDotx {
|
|||||||
if (relationFileInfo === null || !relationFileInfo) {
|
if (relationFileInfo === null || !relationFileInfo) {
|
||||||
throw new Error(`Can not find target file for id ${footerRef.id}`);
|
throw new Error(`Can not find target file for id ${footerRef.id}`);
|
||||||
}
|
}
|
||||||
const xmlData = await zipContent.files[`word/${relationFileInfo.targetFile}`].async("text");
|
const xmlData = await zipContent.files[`word/${relationFileInfo.target}`].async("text");
|
||||||
const xmlObj = fastXmlParser.parse(xmlData, importParseOptions);
|
const xmlObj = fastXmlParser.parse(xmlData, importParseOptions);
|
||||||
const importedComp = convertToXmlComponent(footerKey, xmlObj[footerKey]) as ImportedXmlComponent;
|
const importedComp = convertToXmlComponent(footerKey, xmlObj[footerKey]) as ImportedXmlComponent;
|
||||||
|
|
||||||
@ -106,15 +107,20 @@ export class ImportDotx {
|
|||||||
wrapper: HeaderWrapper | FooterWrapper,
|
wrapper: HeaderWrapper | FooterWrapper,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
let wrapperImagesReferences: IRelationshipFileInfo[] = [];
|
let wrapperImagesReferences: IRelationshipFileInfo[] = [];
|
||||||
const refFile = zipContent.files[`word/_rels/${relationFile.targetFile}.rels`];
|
let hyperLinkReferences: IRelationshipFileInfo[] = [];
|
||||||
|
const refFile = zipContent.files[`word/_rels/${relationFile.target}.rels`];
|
||||||
if (refFile) {
|
if (refFile) {
|
||||||
const xmlRef = await refFile.async("text");
|
const xmlRef = await refFile.async("text");
|
||||||
wrapperImagesReferences = this.findReferenceFiles(xmlRef).filter((r) => r.type === "image");
|
wrapperImagesReferences = this.findReferenceFiles(xmlRef).filter((r) => r.type === "image");
|
||||||
|
hyperLinkReferences = this.findReferenceFiles(xmlRef).filter((r) => r.type === "hyperlink");
|
||||||
}
|
}
|
||||||
for (const r of wrapperImagesReferences) {
|
for (const r of wrapperImagesReferences) {
|
||||||
const buffer = await zipContent.files[`word/${r.targetFile}`].async("nodebuffer");
|
const buffer = await zipContent.files[`word/${r.target}`].async("nodebuffer");
|
||||||
wrapper.addImageRelationship(buffer, r.id);
|
wrapper.addImageRelationship(buffer, r.id);
|
||||||
}
|
}
|
||||||
|
for (const r of hyperLinkReferences) {
|
||||||
|
wrapper.addHyperlinkRelationship(r.target, r.id, "External");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public findReferenceFiles(xmlData: string): IRelationshipFileInfo[] {
|
public findReferenceFiles(xmlData: string): IRelationshipFileInfo[] {
|
||||||
@ -127,7 +133,7 @@ export class ImportDotx {
|
|||||||
return {
|
return {
|
||||||
id: this.parseRefId(item._attr.Id),
|
id: this.parseRefId(item._attr.Id),
|
||||||
type: schemeToType[item._attr.Type],
|
type: schemeToType[item._attr.Type],
|
||||||
targetFile: item._attr.Target,
|
target: item._attr.Target,
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
.filter((item) => item.type !== null);
|
.filter((item) => item.type !== null);
|
||||||
|
Reference in New Issue
Block a user