Skip to main content

Gadget example: Markdown notes

This gadget lets people write notes in Markdown on a dashboard and shows them as formatted text. Each placement keeps its own notes. Use it for team notes, links or a short how-to.

Applies to: Script Master for Jira Β· Cloud. The editor's Markdown notes template does the same job.

How it works​

  • Notes live in a property called config on the dashboard item, which the gadget reads and writes through the Jira REST API. Script Master stores nothing.
  • On first use, the gadget opens a text box. After a save, it shows the formatted notes, with an edit button on hover.
  • Markdown can contain raw HTML, and anyone who can edit the dashboard can change the notes. That's why the gadget cleans the HTML with DOMPurify before showing it.
  • Both libraries load with an integrity hash. The browser refuses a file that was changed.

Set it up​

  1. Create a gadget from the Markdown notes template, or paste the code below.
  2. In the preview, pick a dashboard and, in Gadget on it, a Script Master gadget already placed there. Without a placed gadget, there's nowhere to save the notes.
  3. Turn on Enable and select Save.

Result: People can add the gadget to a dashboard and write their notes in it.

The code​

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@atlaskit/css-reset@3.0.1/dist/bundle.css" />
<script src="https://cdn.jsdelivr.net/npm/showdown@2.1.0/dist/showdown.min.js" integrity="sha384-GP2+CwBlakZSDJUr+E4JvbxpM75i1i8+RKkieQxzuyDZLG+5105E1OfHIjzcXyWH" crossorigin="anonymous"></script>
<!-- DOMPurify cleans the HTML that showdown makes -->
<script src="https://cdn.jsdelivr.net/npm/dompurify@3.4.17/dist/purify.min.js" integrity="sha384-P0JNneQf81HOl9VwNXK76cQ9VMD6Nfsnepxb0cCOg8+UzEdzgPHAPV4wVQrvYQSx" crossorigin="anonymous"></script>

<style>
#edit-btn {
display: none;
position: absolute;
top: 0;
right: 0;
}
#content:hover + #edit-btn, #edit-btn:hover {
display: inline-block;
}
</style>

<div id="loader" style="display: none">Loading...</div>
<div id="edit-panel" style="display: none">
<div><textarea id="textarea" rows="10" style="width:90%;"></textarea></div>
<div><button id="save-btn">save</button></div>
</div>
<div id="view-panel" style="display: none">
<div id="content"></div>
<button id="edit-btn">edit</button>
</div>

<script type="module">
// Follow the viewer's light or dark theme
document.querySelector(":root").setAttribute("data-color-mode", theme.colorMode);
document.querySelector(":root").setAttribute("data-theme", `${theme.colorMode}:${theme.colorMode}`);
document.querySelector(":root").insertAdjacentHTML("afterbegin", `<link rel="stylesheet" href="https://forge.cdn.prod.atlassian-dev.net/atlaskit-tokens_${theme.colorMode}.css" />`);

const context = await view.getContext();
const dashboardId = context.extension.dashboard.id;
const gadgetId = context.extension.gadget.id;
const propertyUrl = `/rest/api/3/dashboard/${dashboardId}/items/${gadgetId}/properties/config`;

const loadContent = async () => {
document.querySelector('#loader').style.display = 'block';
document.querySelector('#edit-panel').style.display = 'none';
document.querySelector('#view-panel').style.display = 'none';

const response = await requestJira(propertyUrl, { headers: { 'Accept': 'application/json' } });

if (response.status === 404) { // no notes saved yet
document.querySelector('#edit-panel').style.display = 'block';
} else {
const { content } = (await response.json()).value;
const html = new showdown.Converter().makeHtml(content);

document.querySelector('#textarea').value = content;
// Only cleaned HTML goes into the page: no scripts, event handlers, styles or forms
document.querySelector('#content').innerHTML = DOMPurify.sanitize(html, { FORBID_TAGS: ['style', 'form'], FORBID_ATTR: ['style'] });
document.querySelector('#view-panel').style.display = 'block';
}

document.querySelector('#loader').style.display = 'none';
};
await loadContent();

document.querySelector('#save-btn').addEventListener('click', async () => {
await requestJira(propertyUrl, {
method: 'PUT',
headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' },
body: JSON.stringify({ content: document.querySelector('#textarea').value })
});
await loadContent();
});

document.querySelector('#edit-btn').addEventListener('click', () => {
document.querySelector('#view-panel').style.display = 'none';
document.querySelector('#edit-panel').style.display = 'block';
});
</script>

If you change a library version, replace its integrity value too. The browser won't load a file whose hash doesn't match.