TOC macro use to show Headings in embedded word docs

Matthew Beda October 15, 2021

Create a User Macro : No Macro Body

This is to be used in conjunction with the TOC Macro and will append to the TOC Macro
{code}

## @noparams
<ac:structured-macro ac:name="html">
<ac:plain-text-body><![CDATA[
<style>
ul#WORDTOC .h1 {
font-weight: bold;
margin-top:6px
}

ul#WORDTOC .h2:before,
ul#WORDTOC .h3:before,
ul#WORDTOC .h4:before ,
ul#WORDTOC .h5:before ,
ul#WORDTOC .h6:before{
content:'\2022';
padding-right:6px;

}
ul#WORDTOC .h2{
margin-left:12px
}
ul#WORDTOC .h3{
margin-left:24px
}
ul#WORDTOC .h4{
margin-left:36px
}
ul#WORDTOC .h5{
margin-left:48px
}
ul#WORDTOC .h6{
margin-left:60px
}
</style>
<script>
AJS.toInit(function () {
var saidHello = false;
AJS.$('.converter-macro-ajax-container').ajaxStop(function(){
runOnce();
});
function runOnce(){
if (!saidHello) htmlTableOfContents();
}
function htmlTableOfContents (documentRef) {
AJS.$('.toc-macro').append("<ul id='WORDTOC'></ul>");
var documentRef = documentRef || document;
var toc = documentRef.getElementById('WORDTOC');
var headings = [].slice.call(documentRef.body.querySelectorAll('.office-container h1, .office-container h2, .office-container h3, .office-container h4, .office-container h5, .office-container h6'));
headings.forEach(function (heading, index) {
var anchor = documentRef.createElement('a');
anchor.setAttribute('name', 'toc' + index);
anchor.setAttribute('id', 'toc' + index);

var link = documentRef.createElement('a');
link.setAttribute('href', '#toc' + index);
link.textContent = heading.textContent;

var div = documentRef.createElement('div');
div.setAttribute('class', heading.tagName.toLowerCase());

div.appendChild(link);
toc.appendChild(div);
heading.parentNode.insertBefore(anchor, heading);
});
saidHello = true;
}
});
</script>
]]></ac:plain-text-body>
</ac:structured-macro>

{code}

1 comment

Bill Bailey
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 15, 2021

@Matthew Beda very interesting.  I think this would work very well as an article where you go through the theory of how it works to help the user community out there.  Some examples of ones I wrote:

Comment

Log in or Sign up to comment
TAGS
AUG Leaders

Atlassian Community Events