Skip to content

Commit

Permalink
Added support to link, quote, code, header, help and unordered lists
Browse files Browse the repository at this point in the history
  • Loading branch information
ccoloma committed Mar 9, 2012
1 parent 3099569 commit 64f9adb
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 7 deletions.
8 changes: 7 additions & 1 deletion demo/index.html
Expand Up @@ -33,7 +33,13 @@ <h2 id="qunit-userAgent"></h2>
<script>
// visual testing
$('.cols').markdownEditor({
value: 'Lots of *stuff* to try',
value: '##MarkdownEditor##\n\n' +
'[Link me!](http://github.com/icoloma/markdownEditor "MarkdownEditor")\n\n' +
'Lots of *stuff* to **try**\n\n' +
'Quotes \n> Two things are infinite: the universe and human stupidity; and I am not sure about the the universe.\n\n' +
'Code \n `<div>Your code goes here</div>` \n\n' +
'UnorderedLists \n\n* Item1 \n* Item2\n\n' +
'OrderedLists \n\n1. Item1 \n2. Item2'
});

// qunit test
Expand Down
87 changes: 83 additions & 4 deletions src/jquery.markdownEditor.js
Expand Up @@ -200,21 +200,100 @@ $.fn.markdownEditor = function(options) {
// insert buttons
$.each(options.buttons, function(index, button) {
var name = 'markdown-button-' + button;
$toolbar.append('<a href="#" class="markdown-button ' + name + '" title="' + options.resources[name] + '"><span class="ui-helper-hidden-accessible">' + options.resources[name] + '</span></a>');
})
$toolbar.append('<a class="markdown-button ' + name + '" title="' + options.resources[name] + '"><span class="ui-helper-hidden-accessible">' + options.resources[name] + '</span></a>');
});

/**
options:
- mark to set/remove. i.e.: '**'
- pattern. Pattern to check if the mark must be set or removed
- notBilateral: add the mark only as prefix, not suffix. Default is false.
*/
var updateSelection = function(options) {
var selection = getSelection().text;
var value = options.mark +
selection +
(options.notBilateral? '' : options.mark)
;
if (options.regex.exec(selection)) {
value = selection.substring(options.mark.length,
(options.notBilateral? selection.length : selection.length - options.mark.length)
);
}
replaceSelection(value);
};

$toolbar
.on('click', '.markdown-button-b', function() {
replaceSelection('**' + getSelection().text + '**');
updateSelection({
mark: '**',
regex: /^\*\*.*\*\*$/
});
onChange();
pushHistory();
})
.on('click', '.markdown-button-i', function() {
replaceSelection('*' + getSelection().text + '*');
updateSelection({
mark: '*',
regex: /^\*(\*\*)?[^*]*(\*\*)?\*$/
});
onChange();
pushHistory();
})
.on('click', '.markdown-button-a', function() {
var selection = getSelection().text;
replaceSelection('[' + selection + '](' + selection + ' "' + selection + '")');
onChange();
pushHistory();
})
.on('click', '.markdown-button-pre', function() {
updateSelection({
mark: '`',
regex: /^`[^`]*`$/
});
onChange();
pushHistory();
})
.on('click', '.markdown-button-blockquote', function() {
updateSelection({
mark: '\n>',
regex: /^\n>.*$/,
notBilateral: true
});
onChange();
pushHistory();
})
.on('click', '.markdown-button-h', function() {
updateSelection({
mark: '#',
regex: /^#.*#$/
});
onChange();
pushHistory();
})
.on('click', '.markdown-button-ol', function() {
updateSelection({
mark: '\n>',
regex: /^\n>.*$/,
notBilateral: true
});
onChange();
pushHistory();
})
.on('click', '.markdown-button-ul', function() {
var set = updateSelection({
mark: '\n* ',
regex: /^\n\*\s.*$/,
notBilateral: true
});
onChange();
pushHistory();
})
.on('click', '.markdown-button-undo', popHistory)
.on('click', '.markdown-button-redo', redoHistory)
.on('click', '.markdown-button-help', function() {
window.open('http://en.wikipedia.org/wiki/Markdown');
})
;

// this for testing only
Expand Down
4 changes: 2 additions & 2 deletions src/markdownEditor.css
@@ -1,8 +1,8 @@
.cols .markdown-editor, .cols .markdown-preview {
width: 200px;
width: 250px;
padding: 10px;
display: inline-block;
height: 200px;
height: 350px;
vertical-align: top;
}
.rows .markdown-editor, .rows .markdown-preview {
Expand Down

0 comments on commit 64f9adb

Please sign in to comment.