Create a Confluence plugin to single source content?
Asked by Damon Williams:

I have an idea for a way to use Confluence to single-source content for reuse in other applications, and I'm wondering if I can create a plugin (or plugins) to accomplish this.

Example: Let's say I have a page of documentation that includes a one-line definition for an "Item ID". I want that exact same definition to show up as a tooltip in a completely separate application that lets users populate "Item ID".

My idea is that I could tag the one-line definition in Confluence using a custom hidden macro, and give it a unique key like "item-id-definition". The macro would have no visible effect on the documentation.

Let's say in total I have 50 of these text strings, each with an associated unique key. I could then have some process run every couple of hours that pulls in every snippet of text that is tagged with this custom macro, and export the text and the key into a separate database or data file. The application needing the text for the tooltip could then lookup the text by key and pull in the content.

Can this be accomplished?


Answers:
author: Robert Reiner _smartics_
body: This depends on your use case, but if the text you change is in the \<p\> element (replacing "hi" in the example), then h1 would not change. If this does not suite your requirements (because you need to change the heading), you'd need to check for a macro, such as the Section Macro' [Identifier parameter](https://www.smartics.eu/confluence/x/ugNk#SectionMacro-Identifier) (as I mentioned in my first post).


author: Damon Williams
body: OK, I'm able to create those IDs now, thanks. But the issue is that the IDs change whenever the text changes. We'd need the IDs to be static.


author: Robert Reiner _smartics_
body: Maybe it depends on the version of Confluence you are running?

On a 5.10.8 I get this:

```
<div class="innerCell">
  <h1 id="qqHome-Hello">Hello</h1>
  <p>hi</p>
</div>
```

The 'qqHome' is the (encoded) name of the page the heading (and paragraph) is part of.


author: Damon Williams
body: Thanks for the reply. I'm not seeing any IDs generated by Confluence when I view the HTML source.


author: Robert Reiner _smartics_
body: [HTML](https://www.w3.org/TR/html/) defines a unique [ID attribute](https://www.w3.org/TR/html/dom.html#the-id-attribute) to select an element of a page. Confluence typically sets this attribute for some elements. Please have a look at the HTML source that is generated by Confluence with you browser.

The [section macro](https://www.smartics.eu/confluence/x/ugNk) of our commercial [projectdoc Toolbox](https://marketplace.atlassian.com/1212629) (but probably there are macros in free add-ons that also do this) renders the ID attribute (I'm one of the authors of the projectdoc Toolbox). But you can also go with the [section macro provided by Confluence](https://confluence.atlassian.com/doc/section-macro-51872531.html), if you set a heading in front. That is: the section macro does *not* render this ID, but you can grab the element, since you know the ID of the heading in front.

It depends on the services you use to fetch this information from a Confluence page on how to integrate a plugin that accesses a page, parses it and returns your fragments. We have a macro ([HTML Snippet Macro](https://www.smartics.eu/confluence/x/TIJ4Ag)) that does this, but from the Confluence side. Your use case is the other way round.

If you require to add an extra attribute (that is cannot or do not want to use the ID attribute), this too can be accomplished. But you would need to write a separate macro. Maybe a [user macro](https://confluence.atlassian.com/doc/writing-user-macros-4485.html) would be the easiest approach to this.

Maybe you already have macros (also from third parties) that already render the HTML ID attribute. I would recommend to check if any of these does. And if it does, everything on the Confluence side is already done.


Tags: app

confluence-app