How can I preview .txt documents?

David Erikson February 22, 2017

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.

2 answers

5 votes
jcollum NA April 18, 2019

The lack of preview for a text document is very surprising. This seems like a basic feature. 

1 vote
Davin Studer
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.
February 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
TAGS
AUG Leaders

Atlassian Community Events