Preserve QWord internal hyperlinks in DOCX
This commit is contained in:
+41
-4
@@ -89,6 +89,7 @@ type DocxModule = {
|
||||
TextRun: new (options: string | Record<string, unknown>) => unknown;
|
||||
WidthType?: Record<string, string>;
|
||||
ExternalHyperlink?: new (options: { children: unknown[]; link: string }) => unknown;
|
||||
InternalHyperlink?: new (options: { children: unknown[]; anchor: string }) => unknown;
|
||||
ImageRun?: new (options: Record<string, unknown>) => unknown;
|
||||
};
|
||||
|
||||
@@ -391,7 +392,17 @@ function wordInlineRunToTextRunOption(run: WordInlineRun) {
|
||||
function wordInlineRunsToTextRuns(runs: WordInlineRun[], docx: DocxModule) {
|
||||
const normalizedRuns = runs.length > 0 ? runs : [{ text: "" }];
|
||||
return normalizedRuns.flatMap((run) => {
|
||||
const href = normalizedHyperlinkTarget(run.href);
|
||||
const internalAnchor = normalizedInternalHyperlinkAnchor(run.href);
|
||||
if (internalAnchor && docx.InternalHyperlink) {
|
||||
return [
|
||||
new docx.InternalHyperlink({
|
||||
anchor: internalAnchor,
|
||||
children: wordInlineRunToTextRunsForRun(run, docx, { style: "Hyperlink" })
|
||||
})
|
||||
];
|
||||
}
|
||||
|
||||
const href = normalizedExternalHyperlinkTarget(run.href);
|
||||
if (href && docx.ExternalHyperlink) {
|
||||
return [
|
||||
new docx.ExternalHyperlink({
|
||||
@@ -842,8 +853,10 @@ function orderedDocxParagraphInlineRecords(paragraph: unknown, hyperlinkTargets:
|
||||
return [];
|
||||
}
|
||||
|
||||
const relationshipId = docxAttribute(orderedElementAttributes(child), "r:id") || docxAttribute(orderedElementAttributes(child), "id");
|
||||
const href = normalizedHyperlinkTarget(hyperlinkTargets[relationshipId]);
|
||||
const attributes = orderedElementAttributes(child);
|
||||
const relationshipId = docxAttribute(attributes, "r:id") || docxAttribute(attributes, "id");
|
||||
const anchor = normalizedDocxAnchor(docxAttribute(attributes, "w:anchor") || docxAttribute(attributes, "anchor"));
|
||||
const href = normalizedExternalHyperlinkTarget(hyperlinkTargets[relationshipId]) || (anchor ? `#${anchor}` : "");
|
||||
return orderedDirectElements(child, "w:r").map((run) => ({
|
||||
run: orderedElementToRecord(run),
|
||||
text: orderedDocxRunText(run),
|
||||
@@ -884,7 +897,7 @@ function docxRelationshipTargetsById(relationshipsXml: unknown) {
|
||||
}
|
||||
|
||||
if (type === wordHyperlinkRelationshipType) {
|
||||
const normalizedTarget = normalizedHyperlinkTarget(target);
|
||||
const normalizedTarget = normalizedExternalHyperlinkTarget(target);
|
||||
if (normalizedTarget) {
|
||||
targets.hyperlinks[id] = normalizedTarget;
|
||||
}
|
||||
@@ -1430,6 +1443,15 @@ function cssFontFamilyValue(value: string) {
|
||||
}
|
||||
|
||||
function normalizedHyperlinkTarget(value?: string | null) {
|
||||
const internalHref = normalizedInternalHyperlinkHref(value);
|
||||
if (internalHref) {
|
||||
return internalHref;
|
||||
}
|
||||
|
||||
return normalizedExternalHyperlinkTarget(value);
|
||||
}
|
||||
|
||||
function normalizedExternalHyperlinkTarget(value?: string | null) {
|
||||
const raw = value?.trim();
|
||||
if (!raw) {
|
||||
return "";
|
||||
@@ -1444,6 +1466,21 @@ function normalizedHyperlinkTarget(value?: string | null) {
|
||||
}
|
||||
}
|
||||
|
||||
function normalizedInternalHyperlinkHref(value?: string | null) {
|
||||
const anchor = normalizedInternalHyperlinkAnchor(value);
|
||||
return anchor ? `#${anchor}` : "";
|
||||
}
|
||||
|
||||
function normalizedInternalHyperlinkAnchor(value?: string | null) {
|
||||
const raw = value?.trim();
|
||||
return raw?.startsWith("#") ? normalizedDocxAnchor(raw.slice(1)) : "";
|
||||
}
|
||||
|
||||
function normalizedDocxAnchor(value?: string | null) {
|
||||
const normalized = value?.trim() ?? "";
|
||||
return /^[A-Za-z_][A-Za-z0-9_.:-]{0,79}$/.test(normalized) ? normalized : "";
|
||||
}
|
||||
|
||||
function imageBlockFromElement(element: Element): WordImageBlock {
|
||||
return {
|
||||
type: "image",
|
||||
|
||||
Reference in New Issue
Block a user