Why is the GetExcerpt method erratic inside a User Macro?
Asked by Roy Youngman:

I've been trying to use the GetExcerpt() method inside a user macro and get some odd behavior. If the Excerpt macro on a page has just a few words, the method picks up some more content that is below (not within) the Excerpt macro. If the macro has a long paragraph, the GetExcerpt() method seems to truncate after just a 100 or so characters. All this seems very odd - one would expect that the method would do as its name implies and no more and no less - just provide the content that is bound within the Excerpt.

BTW - I'm using Confluence 5.1.4 running on a Windows Server with MySQL.

To illustrate, I wrote this simple User Macro called **myexample2**:

```
## @noparams#set( $myexcerpt =  $content.getExcerpt().replaceAll('"', '&amp;quot;'))&lt;h2&gt;Result of Get Excerpt Method&lt;/h2&gt;&lt;p&gt;$myexcerpt&lt;/p&gt;
```

Here is the code behind a simple page that illustrates the problem with a short Excerpt:

```
&lt;ac:macro ac:name="excerpt"&gt;  &lt;ac:parameter ac:name="atlassian-macro-output-type"&gt;BLOCK&lt;/ac:parameter&gt;  &lt;ac:rich-text-body&gt;    &lt;p&gt;Foo bar&lt;/p&gt;  &lt;/ac:rich-text-body&gt;&lt;/ac:macro&gt;&lt;h2&gt;Next Header&lt;/h2&gt;&lt;p&gt;And some more stuff that shouldn't show up&lt;/p&gt;&lt;p&gt;?&lt;/p&gt;&lt;p&gt;  &lt;ac:macro ac:name="myexample2"/&gt;&lt;/p&gt;
```

And here is a screen shot of the results - notice that the user macro incorrectly brings in content outside of the Excerpt:

![](../../upfiles/Capture1_5.PNG)

Here is the code behind a copy of the same page only with more text in the Excerpt:

```
&lt;ac:macro ac:name="excerpt"&gt;  &lt;ac:parameter ac:name="atlassian-macro-output-type"&gt;BLOCK&lt;/ac:parameter&gt;  &lt;ac:rich-text-body&gt;    &lt;p&gt;Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.&lt;/p&gt;    &lt;p&gt;Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.&lt;/p&gt;    &lt;p&gt;But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth.&lt;/p&gt;  &lt;/ac:rich-text-body&gt;&lt;/ac:macro&gt;&lt;h2&gt;Next Header&lt;/h2&gt;&lt;p&gt;And some more stuff that shouldn't show up&lt;/p&gt;&lt;p&gt;?&lt;/p&gt;&lt;p&gt;  &lt;ac:macro ac:name="myexample2"/&gt;&lt;/p&gt;
```

And here is a screen shot of the results - notice that it truncates most of the excerpt:

![](../../upfiles/Capture2_6.PNG)

So..., I'm sure I'm doing something wrong. Can someone help?

Thanks!


Answers:
author: Roy Youngman
body: Wow - do I get extra karma points for stumping the wizkids? Well, I discovered a work-a-round solution which will probably stop working one sad day when Atlassian finally destroys the last remnants of wiki markup. Personally, I hope that day never comes because I believe wiki markup is a good thing, not a bad thing. So here is my work-a-round to the getExcerpt method not working properly:

```
## @noparams#set($macroFinal = "{excerpt-include:" + $content.getTitle() + "|nopanel=true}")#set( $myexcerpt =  $action.getHelper().renderConfluenceMacro($macroFinal).replaceAll('"', '&amp;quot;')   )&lt;h2&gt;Result of renderConfluenceMacro Method&lt;/h2&gt;&lt;p&gt;$myexcerpt&lt;/p&gt;&lt;h2&gt;Result of getExcerpt Method without Set&lt;/h2&gt;&lt;p&gt;$content.getExcerpt()&lt;/p&gt;
```

All this does is use the renderConfluenceMacro method on the regular {excerpt-include} macro. Pretty slick trick that works perfect for my needs and hopefully Atlassian won't take this ability away before fixing the defective getExcerpt method. In the meantime, I'll take my 35 karma point reward and work on the next fun issue!

comments: author: Jeroen B
body: Thanks for taking the effort to answer your own question, saved me quite some time.  
I experienced exactly the same behaviour, now 6(!) years. And I can confirm that your workaround still works like a charm.

Thanks again.


Tags: excerpt

user-macro