weixin_39559119
2020-11-29 01:52 阅读 10

Adding hyperlink

Environment

  • Version of docxtemplater: 3.0.2
  • Used docxtemplater-modules: None
  • Runner: Node.JS v7.1.0

Problem

I can't see a way of adding hyperlink. If we have {website} in the template and pass a valid hyperlink for example https://github.com as a value, it will be displayed as raw text. Passing field code for example '{ HYPERLINK "https://github.com" }' does not make any difference, the text will be displayed as is.

该提问来源于开源项目:open-xml-templating/docxtemplater

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

8条回答 默认 最新

  • weixin_39559119 weixin_39559119 2020-11-29 01:52

    Seems like it's possible with https://github.com/open-xml-templating/docxtemplater/blob/master/docs/source/syntax.rst#raw-xml-syntax. But is there any simpler way to achieve this?

    点赞 评论 复制链接分享
  • weixin_39559119 weixin_39559119 2020-11-29 01:52

    Even with raw XML, inserting appropriate XML for hyperlink is not enough:

    xml
    <hyperlink r:id="rId5">
      <r w:rsiddel="00000000" w:rsidr="00000000" w:rsidrpr="00000000">
        <rpr>
          <color w:val="1155cc"></color>
          <u w:val="single"></u>
          <rtl w:val="0"></rtl>
        </rpr>
        <t xml:space="preserve">https://github.com</t>
      </r>
    </hyperlink>
    

    because link has unique id and according reference must also exist in word/_rels/document.xml.rels:

    xml
    <relationship id="rId5" type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" target="https://github.com" targetmode="External"></relationship>
    

    I also tried to insert {website} as a value for hyperlink with text {website}, but it failed on validation stage (I use Google Docs) which is quite predictable.

    点赞 评论 复制链接分享
  • weixin_39620984 weixin_39620984 2020-11-29 01:52

    Yes, inserting a link is quite difficult because of the need for a link in word/_rels/document.xml.rels

    This is why such kinds of features are normally done in modules (https://github.com/open-xml-templating/docxtemplater#modules)

    Here is the link module , however, it is only compatible with version 2.x of docxtemplater, but you might work on making it compatible with 3.x

    https://github.com/sujith3g/docxtemplater-link-module

    点赞 评论 复制链接分享
  • weixin_39559119 weixin_39559119 2020-11-29 01:52

    OK, thanks for the info. Hovewer, link functionality is not so crucial as loops for me now. Could you take a look at issue 272?

    点赞 评论 复制链接分享
  • weixin_39559119 weixin_39559119 2020-11-29 01:52

    My bad, I missed the mention about hyperlinks module in README section.

    点赞 评论 复制链接分享
  • weixin_39620984 weixin_39620984 2020-11-29 01:52

    As the link module is only compatible with v2, I'd like to suggest to use the HTML module (paid module) , which allows to insert many HTML tags, including <a href="">Link</a> tag : https://docxtemplater.com/modules/html/

    点赞 评论 复制链接分享
  • weixin_39905226 weixin_39905226 2020-11-29 01:52

    Would it work to use data filters in combination with html to create links on the fly?

    {~~ Data.Label | hyperlink: Data.Url }

    Where filter.hyperlink is simply

    javascript
    (label, url) => `<a href="%24%7Burl%7D">${label}</a>`;
    

    Essentially, I'm asking if the filter is handled before html parsing

    点赞 评论 复制链接分享
  • weixin_39620984 weixin_39620984 2020-11-29 01:52

    Yes, the filter is always applied before html parsing (this is the case for all tags), so it would work indeed !

    I would advise the following to avoid surprises :

    
    function escapeAttrNodeValue(value) {
        return value.replace(/(&)|(")|(\u00A0)/g, function(match, amp, quote) {
            if (amp) return "&";
            if (quote) return """;
            return " ";
        });
    }
    function escapeHTML(s) { 
        return s.replace(/&/g, '&')
                .replace(/"/g, '"')
                .replace(//g, '>');
    }
    filter.hyperlink = (label, url) => `<a href="%24%7BescapeAttrNodeValue(url)%7D">${escapeHTML(label)}</a>
    
    点赞 评论 复制链接分享

相关推荐