Skip to content

Instantly share code, notes, and snippets.

@ysamlan
Last active October 19, 2015 18:41
Show Gist options
  • Save ysamlan/24d575c40caae746531e to your computer and use it in GitHub Desktop.
Save ysamlan/24d575c40caae746531e to your computer and use it in GitHub Desktop.
JIRA to Markdown inline conversions
// ==UserScript==
// @name JIRA to Markdown
// @namespace https://github.com/ysamlan/
// @version 1.0
// @description Converts JIRA wiki syntax to Markdown and vice versa. Based on https://github.com/FokkeZB/J2M.
// @match https://*.atlassian.net/*
// @grant none
// @copyright 2015 Yoni Samlan, Apache license 2.0
// @updateURL https://gist.githubusercontent.com/ysamlan/24d575c40caae746531e/raw
// ==/UserScript==
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Currently tested in Tampermonkey; not usable as a Chrome extension.
function toMarkdown(text) {
converted = text;
converted = converted.replace(/^h([0-6])\.(.*)$/gm, function (match,level,content) {
return Array(parseInt(level) + 1).join('#') + content;
});
converted = converted.replace(/([*_])(.*)\1/g, function (match,wrapper,content) {
var to = (wrapper === '*') ? '**' : '*';
return to + content + to;
});
return converted.replace(
/\{\{(.*?)\}\}/g, '`$1`').replace(
/\?\?(.*?)\?\?/g, '<cite>$1</cite>').replace(
/\{code(:([a-z]+))?\}([^]*)\{code\}/gm, '```$2$3```').replace(
/\[(.+?)\|(.+)\]/g, '[$1]($2)');
}
function toJira(text) {
converted = text;
converted = converted.replace(/^(.*?)\n([=-])+$/gm, function (match,content,level) {
return 'h' + (level[0] === '=' ? 1 : 2) + '. ' + content;
});
converted = converted.replace(/^([#]+)(\s+.*?)$/gm, function (match,level,content) {
return 'h' + level.length + '.' + content;
});
converted = converted.replace(/([*_]+)(.*?)\1/g, function (match,wrapper,content) {
var to = (wrapper.length === 1) ? '_' : '*';
return to + content + to;
});
return converted.replace(
/<cite>(.*?)<\/cite>/g, '??$1??').replace(
/\{code(:([a-z]+))?\}([^]*)\{code\}/gm, '```$2$3```').replace(
/`(.*?)`/g, '{{$1}}').replace(
/\[(.+?)\]\((.+)\)/g, '[$1|$2]')
}
function toJiraHandler() {
jQuery(this).text('To Markdown');
container = jQuery(this).closest('.wiki-edit');
jQuery(container).find('textarea.wiki-textfield')[0].value = toJira(jQuery(container).find(
'textarea.wiki-textfield')[0].value);
jQuery(container).find('.wiki-edit-toolbar .aui-toolbar2-primary .aui-buttons').show();
jQuery(this).one('click', toMarkdownHandler);
return false;
}
function toMarkdownHandler() {
jQuery(this).text('To JIRA');
container = jQuery(this).closest('.wiki-edit');
jQuery(container).find('textarea.wiki-textfield')[0].value = toMarkdown(jQuery(container).find(
'textarea.wiki-textfield')[0].value);
jQuery(container).find('.wiki-edit-toolbar .aui-toolbar2-primary .aui-buttons').not('.jira2markdown').hide();
jQuery(this).one('click', toJiraHandler);
return false;
}
jQuery(document).on('focus', 'textarea.wiki-textfield', function(){
container = jQuery(this).closest('.wiki-edit');
if (container.find('.jira2markdown a').length == 0){
jQuery('<div class="aui-buttons jira2markdown" style="display:block;"><a href="#" tabindex="-1" class="aui-button aui-button-subtle">To Markdown</a></div>'
).prependTo(container.find('.wiki-edit-toolbar .aui-toolbar2-primary')).find('a').one('click', toMarkdownHandler);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment