User Macro to Pull Headings from Different Page

Here's what I want to accomplish:

A user places a user macro on the page. The macro requires two fields:

  • Page Title
  • Heading Title

I want the macro to have ability to search the page title (like other macros do). After the page has been found, I would like the Heading Title drop-down list to populate with all the headings on that particular page. Then the user can select a heading, click Insert, and the macro produces a link to that heading.

Can this be done?

1 answer

0 vote
Stephen Deutsch Community Champion Apr 08, 2015

Ask and ye shall receive:

## @noparams
<form class="aui top-label">
  <div class="field-group top-label">
    <label for="requested-page">Find headings on this page:</label> 
    <input id="requested-page" class="autocomplete-page text" data-max="10" data-none-message="No results" data-template="{id}">
    <label for="select-heading">Heading</label>
    <select class="select" id="select-heading" name="select-heading">
    </select>
    <p> </p>
    <div id="link-output"></div>
  </div>
</form>
<script>
jQuery("#requested-page").change(function() {
  setTimeout(function() {
    var pageId = jQuery("#requested-page").val();
    jQuery.ajax({
      url: contextPath + "/rest/api/content/" + pageId + "?expand=body.view",
      success: function(response) {
        var pageData = jQuery("<div></div>").html(response.body.view.value);
        var headers = pageData.find(":header");
        jQuery("#select-heading").empty();
        jQuery("#link-output").empty();
        headers.each(function() {
          jQuery("#select-heading").append('<option value="' + this.id + '">' + this.innerHTML + '</option>');
        }).promise().done(function() {
          var linkAddress = response._links.webui + "#" + jQuery("#select-heading > option").first().attr("value");
          jQuery("#link-output").html('<a href="' + linkAddress + '">' + linkAddress + '</a>');
        });
        jQuery("#select-heading").change(function() {
          var linkAddress = response._links.webui + "#" + this.value;
          jQuery("#link-output").html('<a href="' + linkAddress + '">' + linkAddress + '</a>');
        });
      }
    });
  }, 100);
});
</script>

I couldn't test it extensively, but it worked on my system.  You must be running Confluence 5.5 or later.

You are correct, that does work for essentially what I described. Obviously, I described it a bit wrong. What I was looking for was this functionality INSIDE the macro. So when the user adds the macro to a page, the NAME OF MACRO dialog displays and requires you to specify the page on which to find headings, then populates the list of headings. Once selected, the macro is saved. When the page renders, it provides a link to that heading (maybe Page Title: Heading). I'll try to work with what you provided to make a macro do that, but my programming (and user macro writing) skills are a bit dull.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Jul 10, 2018 in Confluence

We want to see the templates you've created in Confluence!

Hi Community, Jessica here from the Confluence Product Marketing team!  July’s community challenge is all about sharing pictures  — and as an extension of our first post on what ...

902 views 23 12
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you