Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

External hyperlink with 2 hashes not correct #2986

Open
tripodsan opened this issue Mar 11, 2025 · 1 comment
Open

External hyperlink with 2 hashes not correct #2986

tripodsan opened this issue Mar 11, 2025 · 1 comment

Comments

@tripodsan
Copy link

tripodsan commented Mar 11, 2025

Create an external hyperlink with an url with 2 hashes, eg:

https://www.example.com/#foo#bar

in the generated docx, the first part of the fragment is lost:

Image

the generated xml looks like:

            <w:hyperlink w:history="1" r:id="rIdlrwbxlrt1kj78rvsbt133">
                <w:r>
                    <w:rPr>
                        <w:rStyle w:val="Hyperlink"/>
                    </w:rPr>
                    <w:t xml:space="preserve">fragment-with-2-hashes</w:t>
                </w:r>
            </w:hyperlink>

and the url with the fragment is stored in the rels:

    <Relationship Id="rIdlrwbxlrt1kj78rvsbt133"
                  Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"
                  Target="https://www.example.com/#foo#bar" TargetMode="External"/>

but when the same is generated in word, it puts an w:anchor on the hyperlink and does not have a fragment in the relationship:

            <w:hyperlink w:anchor="foo#bar" r:id="R99e6d42dcfb444f9">
                <w:r w:rsidRPr="072F9BA4" w:rsidR="7B7B3AAE">
                    <w:rPr>
                        <w:rStyle w:val="Hyperlink"/>
                    </w:rPr>
                    <w:t xml:space="preserve">fragment-with-2-hashes</w:t>
                </w:r>
            </w:hyperlink>

and the rel:

    <Relationship
            Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"
            Target="https://www.example.com/"
            TargetMode="External" Id="Rd6df781fb51145a4"/>
@tripodsan
Copy link
Author

tripodsan commented Mar 11, 2025

I have a workaround when using my own paragraph:

/**
 * Custom paragraph that overwrites `prepForXml` and handles the special case for external links
 * with fragments the same way as word does: it creates a relationship w/o the fragment and adds
 * the fragment as `w:anchor` property to the hyperlink.
 */
class MyParagraph extends Paragraph {
  prepForXml(context) {
    for (const element of this.root) {
      if (element instanceof ExternalHyperlink) {
        let { link } = element.options;
        let anchor;
        const idx = link.indexOf('#');
        if (idx > 0) {
          anchor = link.substring(idx + 1);
          link = link.substring(0, idx);
        }
        const index = this.root.indexOf(element);
        const concreteHyperlink = new ConcreteHyperlink(element.options.children, uniqueId());
        context.viewWrapper.Relationships.createRelationship(
          concreteHyperlink.linkId,
          'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink',
          link,
          'External',
        );
        if (anchor) {
          const attrs = findXMLComponent(concreteHyperlink, '_attr');
          attrs.root.anchor = anchor;
        }
        this.root[index] = concreteHyperlink;
      }
    }
    return super.prepForXml(context);
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant