Skip to content

Commit

Permalink
Merge pull request #474 from openannotation/new-markdown-plugin
Browse files Browse the repository at this point in the history
New Markdown Plugin
  • Loading branch information
gergely-ujvari committed Jan 11, 2015
2 parents 4182887 + 44c39e9 commit ab0838c
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 156 deletions.
5 changes: 4 additions & 1 deletion src/plugin/defaultui.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ function DefaultUI(element) {
}
});

var markdown = UI.markdown();

var viewer = new UI.Viewer({
onEdit: function (ann) {
registry.annotations.update(ann);
Expand All @@ -241,7 +243,8 @@ function DefaultUI(element) {
);
},
autoViewHighlights: element,
extensions: [tags.createViewerField]
extensions: [tags.createViewerField],
renderText: markdown.convert
});
viewer.attach();

Expand Down
63 changes: 0 additions & 63 deletions src/plugin/markdown.coffee

This file was deleted.

1 change: 1 addition & 0 deletions src/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ exports.Adder = require('./ui/adder').Adder;
exports.Editor = require('./ui/editor').Editor;
exports.Filter = require('./ui/filter').Filter;
exports.Highlighter = require('./ui/highlighter').Highlighter;
exports.markdown = require('./ui/markdown').createMarkdownPlugin;
exports.tags = require('./ui/tags').createTagsPlugin;
exports.TextSelector = require('./ui/textselector').TextSelector;
exports.Viewer = require('./ui/viewer').Viewer;
Expand Down
38 changes: 38 additions & 0 deletions src/ui/markdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"use strict";

var Util = require('../util');

var g = Util.getGlobal(),
_t = Util.gettext;

function createMarkdownPlugin () {
var converter = null;

if (g.Showdown && typeof g.Showdown.converter === 'function') {
converter = new g.Showdown.converter();
} else {
console.warn(_t("To use the Markdown plugin, you must" +
" include Showdown into the page first."));
}

// Converts provided text into markdown.
//
// text - A String of Markdown to render as HTML.
//
// Examples
//
// plugin.convert('This is _very_ basic [Markdown](http://daringfireball.com)')
// # => Returns "This is <em>very<em> basic <a href="http://...">Markdown</a>"
//
// Returns HTML string.
function convert (text) {
text = Util.escapeHtml(text || '');
return converter ? converter.makeHtml(text) : text;
}

return {
convert: convert
};
}

exports.createMarkdownPlugin = createMarkdownPlugin;
92 changes: 0 additions & 92 deletions test/spec/plugin/markdown_spec.js

This file was deleted.

62 changes: 62 additions & 0 deletions test/spec/ui/markdown_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
var assert = require('assertive-chai').assert;

var UI = require('../../../src/ui'),
Util = require('../../../src/util');

var g = Util.getGlobal();

describe('UI.markdown', function () {
var plugin = null;

describe("constructor", function () {
it("should log warning if Showdown is not loaded", function () {
sinon.stub(console, 'warn');
var showdown = g.Showdown;
g.Showdown = null;
plugin = UI.markdown();
assert(console.warn.calledOnce);
console.warn.restore();
g.Showdown = showdown;
});
});

describe("convert", function () {
var showdown = null;
var escapeHtml = null;
var makeHtml = null;

beforeEach(function () {
escapeHtml = sinon.stub(Util, 'escapeHtml').returns('escaped');
makeHtml = sinon.stub().returns('converted');

var fakeShowDown = {
converter: function () {
return {
makeHtml: makeHtml
};
}
};

showdown = g.Showdown;
g.Showdown = fakeShowDown;
});

afterEach(function () {
Util.escapeHtml.restore();
g.Showdown = showdown;
});

it("should escape and convert the provided text into markdown", function () {
plugin = UI.markdown();
assert.equal(plugin.convert('foo'), 'converted');
assert.isTrue(escapeHtml.calledWith('foo'));
assert.isTrue(makeHtml.calledWith('escaped'));
});

it("should escape even if showdown is not loaded", function () {
g.Showdown = null;
plugin = UI.markdown();
assert.equal(plugin.convert('foo'), 'escaped');
});
});
});

0 comments on commit ab0838c

Please sign in to comment.