Edit online

How to Insert Custom HTML Content

You can add custom HTML content in the WebHelp Responsive output by inserting it in a well-formed XML file (or specifying it in a well-formed XHTML fragment) that will be referenced in the transformation (either from an Oxygen Publishing Template or using one of the HTML fragment placeholder parameters). This content may include references to additional JavaScript, CSS, and other types of resources, or such resources can be inserted inline within the HTML content that is inserted in the XML file.

The XML File

There are several things to consider regarding this XML file:
  • Well-Formedness - If the content of the file is not XML Well-formedXML Well-formed, the transformation will automatically convert non-well-formed HTML content to a well-formed XML equivalent (assuming the webhelp.enable.html.fragments.cleanup transformation parameter is set to true).

    For example, if the HTML content includes several <script> or <link> elements, the XML fragment would have multiple root elements and to make it well-formed, it would be wrapped it in an <html> element. This element tag will be filtered out and only its children will be copied to the output documents. Similarly, you can wrap your content in <head>, <body>, <html/head>, or <html/body> elements.

    Note:
    The converted fragments are stored in a file located in the whr-html-fragments subfolder of the transformation's temporary directory.
    Tip:
    If you do not want the transformation to automatically convert non-well-formed content into well-formed XML content, you can set the webhelp.enable.html.fragments.cleanup transformation parameter to false. This will instead cause the transformation to fail if at least one HTML fragment is not well-formed.
  • Inline JavaScript or CSS Content:

    JavaScript:
    <script type="text/javascript">
        /* Include JavaScript code here. */
     
        function myFunction() {
          return true;
        }
    </script> 
    CSS:
    <style>
        /* Include CSS style rules here. */
    
        *{
          color:red
        } 
    </style>
    Note:

    If you have special characters (e.g. &, <) that break the well-formedness of the XML fragment, it is important to place the content inside an XML comment.

    Otherwise, the WebHelp transformation automatically wraps inline JavaScript or CSS content in an XML comment. Also, if the commented content contains constructs that are not allowed in an XML comment, those constructs are escaped.

    [Important] XML comment tags (both the start and end tags) must be on lines by themselves. If they are on the same line as any of the script's content, it will likely result in a JavaScript error.

    <script type="text/javascript">
      <!--
        /* Include JavaScript code here. */
     
        function myFunction() {
          return true;
        }
      --/>
    </script> 

Using WebHelp Macros

The XML file can use WebHelp macros, which are variables that will be expanded when the content of the HTML fragment file will be copied in the final output.

There are two possibilities for using macros:
  • Directly in attribute values - For example, if you want to reference a JavaScript file from the Publishing Template directory, you can use the following construct:
    <script type="text/javascript" src="${path(oxygen-webhelp-template-dir)}/"></script>
  • In text content - Using the <whc:macro> template component:
    <script type="text/javascript"> 
        var outDirPath = '<whc:macro value="${path(oxygen-webhelp-output-dir)}" 
        xmlns:whc="http://www.oxygenxml.com/webhelp/components"/>';
        console.log("The output directory path is:", outDirPath);
    </script>
    Note:
    When using the <whc:macro> element, you should also include the xmlns:whc="http://www.oxygenxml.com/webhelp/components" namespace declaration for the whc prefix. This is necessary for the XML fragment to be well-formed.

Referencing the HTML fragment using a Publishing Template

  1. If you have not already created a Publishing Template, see Working with Publishing Templates.
  2. Insert the HTML content in a file that is XML well-formed (for example, custom-html.xml).
  3. Using the ProjectProject Explorer view, copy your custom XML file in a folder inside publishing the template root folder (for example, in the custom_footer_template/html-fragments folder).
  4. Open the template descriptor file associated with your publishing template and add a reference to the custom HTML fragment in the html-fragments section.
    <publishing-template>
        ...    
        <webhelp>
          ...
          <html-fragments>
            <fragment 
              file="html-fragments/custom-html.xml" 
              placeholder="webhelp.fragment.head"/>
    Note:
    If you want to insert the content in another location within the output document, you can reference the XML file from any other HTML Fragment extension points.
  5. Open the DITA Map WebHelp Responsive transformation scenario.
  6. Click the Choose Custom Publishing Template link and select your template.
  7. Click OK to save the changes to the transformation scenario.
  8. Run the transformation scenario.

Results: Your additional content will be included at the end of the <head> element of your output document.

Referencing the HTML Fragment using a Transformation Parameter

  1. Insert the HTML content in a well-formed XML file.
  2. Edit the DITA Map WebHelp Responsive transformation scenario and open the Parameters tab.
  3. Edit the value of the webhelp.fragment.head parameter and set it to the absolute path of your XML file.
    Note:
    If you want to insert the content in another location within the output document, you can reference the XML file from any other HTML Fragment extension points.
  4. Click OK to save the changes to the transformation scenario.
  5. Run the transformation scenario.

Results: Your additional content will be included at the end of the <head> element of your output document.