How can I preview .txt documents?

I have .txt documents that I want to attach and easily view in the browser.  Confluence can't preview a .txt, so it has to be downloaded.  I don't want to paste the text into a confluence page.  Browsers natively can display .txt, so it is an odd limitiation.

1 answer

0 vote
Davin Studer Community Champion Feb 22, 2017

I don't think there is a way to preview it. I did create a user macro a few years ago to embed the contents of a txt file into a page. The user macro is below.

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 = '$' )
<style>
#text${id} pre {white-space: pre-wrap;}
</style>
<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>

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 Friday 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...

253 views 11 10
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