embedding mime type text in Confluence

Hi,

is there any option to embed plain text files via macro which are attached to a page (like it is possible with images or pdf already)?

Something like

<ac:text>

<ri:attachment ri:filename="test.sh"/>

</ac:text>

This is not about embedding Office Files, Images, Multi Media or PDF which are supported.

We would like to attach files (e.g. just by dropping them in page window).
Embedding should show the content of the actual version of the attached / embedded file.
Means, we do not want to have to edit page each time an attachment has changed.

If this is not supported anyway, is it part of feature planning or where can I open an issue for Confluence?

Regards

Peter

5 answers

1 accepted

1 vote
Davin Studer Community Champion Jul 31, 2014

Here is a user macro I created for this.

Macro Name:
text

Macro Title:
Text Document

Macro Body Processing:
No macro body

Template:

## Developed by: Davin Studer
## Date created: 05/07/2014
## @param TXTName:title=Text File Name|type=string|required=true|desc=The name of the attached TXT file. (ex. Stuff.txt, Stuff.csv, Stuff.sh, Stuff.bat)
## @param Page:title=Page Title|type=confluence-content|required=false|desc=If not specified, the current page is used. (for a page in another space use the format "spacekey:page name")

#set( $id=$action.dateFormatter.calendar.timeInMillis )
#if( $paramPage && $paramPage != "" )
    ##########################
    ## Get the page manager ##
    ##########################
    #set( $containerManagerClass=$content.class.forName('com.atlassian.spring.container.ContainerManager') )
    #set( $getInstanceMethod=$containerManagerClass.getDeclaredMethod('getInstance',null) )
    #set( $containerManager=$getInstanceMethod.invoke(null,null) )
    #set( $containerContext=$containerManager.containerContext )
    #set( $pageManager=$containerContext.getComponent('pageManager') )

    ##########################
    ## Find the page        ##
    ##########################
    #set( $parts = $paramPage.split(":") )
    #set( $i = 0 )
    #set( $len = 0 )
    #set( $key = "" )
    #set( $name = "" )

    ##Having trouble finding out the length/size of $parts ... brute force it
    #foreach( $part in $parts )
        #set( $len = $len + 1 )
    #end
    
    #if( $len == 1 )
        #set( $key = $content.spaceKey )
        #set( $name = $paramPage )
    #else
        #foreach( $part in $parts )
            #if( $i == 0 )
              #set( $key = $part )
              #set( $i = $i + 1 )
           #else
              #set( $name = $part )
           #end
        #end
    #end

    #set( $tempcontent = "" )
    #if( $pageManager )
        #set( $tempcontent = $pageManager.getPage($key, $name) )
    #end
    #if( $tempcontent && $tempcontent != "" )
        #set( $content = $tempcontent )
    #else
        The page "$paramPage" was not found in this space.
        #set( $content = "" )
    #end
#end

#set( $attachments = $attachmentManager.getLatestVersionsOfAttachments($content) )
#set( $baseUrl = $action.getGlobalSettings().getBaseUrl() )

#set( $url = "" )
#foreach( $attachment in $attachments )
    #if( $attachment.getTitle().toLowerCase() == $paramTXTName.toLowerCase() )
        #set( $url = $attachment.getDownloadPathWithoutVersion() )
    #end
#end
#set( $d = '$' )
<div id="text$id">
    <ac:structured-macro ac:name="noformat">
		<ac:parameter ac:name="nopanel">true</ac:parameter>
		<ac:plain-text-body><![CDATA[ ]]></ac:plain-text-body>
	</ac:structured-macro>
</div>
<script type="text/javascript">
AJS.toInit(function(){
    AJS.${d}.get("$baseUrl$url", function( data ) {
        AJS.$('#text$id pre').text(data);
    });
});
</script>

Davin Studer Community Champion Aug 04, 2014

Does this work for your needs?

Hi Davin,

that's really great. Exactly what we are looking for. Setting this in a

panel or

expandable text block

you can even give it a (border and) a title.

Thank you so much for sharing this. We love you ;)

Kind regards

Peter Neelmeyer

PS:

If confluence team is interested:

this could be a bundled macro with syntax highlighting and select field (dropdown) for choice of attachment file like there are plugins for PDF and Office files.

Hi Davin,

A little workaround. For title option we added parameter Title and changed xml a bit. It uses panel macro with pre tag instead of noformat macro.

<div id="text$id">
  <ac:structured-macro ac:name="panel">
  <ac:parameter ac:name="titleBGColor">#F7F7F7</ac:parameter>
  <ac:parameter ac:name="title">
#if( $paramTitle && $paramTitle != "" )
$paramTitle
#else
$paramTXTName
#end
   </ac:parameter>
   <ac:parameter ac:name="titleColor">#000000</ac:parameter>
   <ac:parameter ac:name="bgColor">#F0F0F0</ac:parameter>
   <ac:parameter ac:name="borderStyle">dashed</ac:parameter>
   <ac:parameter ac:name="borderColor">#BBBBBB</ac:parameter>
    <ac:rich-text-body>
       <pre><![CDATA[ ]]></pre>
     </ac:rich-text-body>
   </ac:structured-macro>
</div>

(and if totally no title would be given just no title row will be shown)

Added trimming data

// guessed there is a method for trimming data
AJS.$('#text$id pre').text(AJS.trim(data));

Thanks again for your great help

Peter

Hi Peter,

Confluence can interact with any MSOffice file. So you can use a macro to integrate it. More info: https://confluence.atlassian.com/display/DOC/Displaying+Office+Files+in+Confluence

Or you can create a text link with the attachement's address.

Hope this helps!

Cheers!

To embed plain text files, I suggest you to use the noformat macro. You will need to copy and paste the content of the file (it does not work from an attachment).

Well, it seems to be not supported anyway and an issue can be opened here

https://jira.atlassian.com/issues/?jql=project%20%3D%20CONF

See

https://jira.atlassian.com/browse/CONF-34445

Hello everybody,

 

how can i resolve the issue with the PDF-Export.
PDF don't show any text from included files.

 

can anyone help me to resolve this issue?

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Kesha Thillainayagam
Posted Apr 13, 2018 in Confluence

We want to hear how your non-technical teams are using Confluence!

Hi Community! Kesha (kay-sha) from the Confluence marketing team here! Can you share stories with us on how your non-technical (think Marketing, Sales, HR, legal, etc.) teams are using Confluen...

2,831 views 27 11
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