How do I create a custom macro
Asked by Zavery Jones:

I don't think I have much access to Confluence other than editing pages. I have a table that has a bunch of dates listed in it. The dates represent when our SSL certficates, for various websites, expire. I would like to have something that highlights dates (in the table) that are close to the current date.

Would this need to be a macro or something else? If it's a macro then how do I go about adding a custom macro and what kind of access do I need in order to do this?


Answers:
author: Victor Prasad
body: Hello,

Am I able to export a custom macro to another Confluence Environment or do I have to recreate it in the same one?

Thanks,

V

comments: author: Nic Brough -Adaptavist-
body: There's no macro export/import beyond a simple "copy and paste" of the main text, and manually copying the rest of the settings.


author: Stephen Deutsch
body: Hi Zavery,

You would probably need to be a Confluence administrator to create any sort of customizations as far as that is concerned (unless your administrator has activated the HTML macro). Here's some options:

1. Just doing Javascript? You could insert it into the Custom HTML in the General Settings.
2. You could activate Javascript for a single page by using the HTML macro and inserting the script inside it.
3. You could create a user macro that has code inside it (allows both Apache Velocity code and HTML/Javascript).

Here is some sample Javascript code that probably would do what you want. This code highlights dates that are within 3 days of today in yellow and days that are past the current date in red (you can change the number of days by changing the number 3 below):

```
<script>
var limitDate = new Date();
limitDate.setDate(limitDate.getDate() + 3);
jQuery("time.date-upcoming").each(function() {
 var timedate = new Date(jQuery(this).attr("datetime"));
 if (timedate < limitDate) {
 jQuery(this).attr("style", "border:1px solid #ffab00;");
 }
});
jQuery("time.date-past").attr("style", "border:1px solid #ff5630;");
</script>
```

If you're going to insert it as a user macro, then you'll need to include the following line at the top:

```
## @noparams
```

comments: author: Zavery Jones
body: Thanks for the info Stephen. I guess I'll probably need to go with Javascript.

I'm thinking we need to just do this for a single page. In this case, would an administrator be able to activate Javascript for a single page? If so, then how would they do this?


author: Stephen Deutsch
body: Hi Zavery,

for one single page, probably the best option is to use the User Macro (unless your administrator has enabled the HTML macro, in which case that is the easiest). For a user macro, they'll need to choose a name, choose whether it is viewable by all users or only the administrator (in which case they would have to add the macro to your page), and then in the template box they should insert

```
##?@noparams
```

on the first line and then the rest should be the code above. Once the user macro is inserted into the page (just like any other macro), then the changes should take effect.


author: Zavery Jones
body: Would enabling a user macro cause a security vulnerability? If so, then I may not want to do this.


author: Nic Brough -Adaptavist-
body: It could, if malicious code was added into it, or it enabled a user to embed malicious content in the body of the macro.

Only admins can add user macros, so you have to trust your admins not to write macros that do those things.


author: Stephen Deutsch
body: Indeed, as Nic said, enabling the HTML macro could be a security vulnerability, but a user macro will only run the code that an admin has entered into it.


author: Nic Brough -Adaptavist-
body: Yes, the risk with the html macro is wider - anyone with "edit" could inject malicious code, not just your admins.


author: Zavery Jones
body: Thank you all!


Tags: macro