Merge pull request #293 from mathematicalcoffee/bugfix-gfm-hardwrap

Bugfix: github-flavoured-markdown was ignoring the hardwrap configuration.
This commit is contained in:
Jeff Williams 2013-01-17 18:12:23 -08:00
commit 3fed4ebf32
3 changed files with 126 additions and 29 deletions

View File

@ -48,6 +48,7 @@ function escapeUnderscores(source) {
*/
function getParseFunction(parser, conf) {
conf = conf || {};
var parse;
if (parser === parsers.gfm) {
parser = require(parser);
@ -61,19 +62,23 @@ function getParseFunction(parser, conf) {
parser.hardwrap = !!conf.hardwrap;
return function(source) {
parse = function(source) {
source = escapeUnderscores(source);
return parser.parse(source, githubConf);
};
parse._parser = parsers.gfm;
return parse;
} else if (parser === parsers.evilstreak) {
parser = require(parser).markdown;
return function(source) {
parse = function(source) {
source = escapeUnderscores(source);
// evilstreak parser expects line endings to be \n
source = source.replace(/\r\n|\r/g, '\n');
return parser.toHTML(source, conf.dialect);
};
parse._parser = parsers.evilstreak;
return parse;
} else {
throw new Error("unknown Markdown parser: '" + parser + "'");
}

View File

@ -151,7 +151,7 @@ this.makeHtml = function(text, gh) {
g_html_blocks = new Array();
// benblank - Allow hardwrapping to be disabled.
g_hardwrap = typeof this.hardwrap === "undefined" || this.hardwrap;
g_hardwrap = (typeof exports.hardwrap === "undefined" || exports.hardwrap);
// attacklab: Replace ~ with ~T
// This lets us use tilde as an escape char to avoid md5 hashes

View File

@ -13,56 +13,148 @@ describe('jsdoc/util/markdown', function() {
});
describe('getParser', function() {
xit('should retrieve a function when called with default settings', function() {
// TODO
// couple of convenience functions letting me set conf variables and restore
// them back to the originals later.
function setMarkdownConf(hash) {
if (!env.conf.markdown) {
env.conf.markdown = {};
}
var keys = Object.keys(hash);
var storage = {};
for (var i = 0; i < keys.length; ++i) {
storage[keys[i]] = env.conf.markdown[keys[i]];
// works because hash[key] is a scalar not an array/object
env.conf.markdown[keys[i]] = hash[keys[i]];
}
return storage;
}
function restoreMarkdownConf(storage) {
var keys = Object.keys(storage);
for (var i = 0; i < keys.length; ++i) {
env.conf.markdown[keys[i]] = storage[keys[i]];
}
if (keys.length === 0) {
delete env.conf.markdown;
}
}
it('should retrieve a function when called with default settings', function() {
var storage = setMarkdownConf({parser: 'evilstreak'});
var parser = markdown.getParser();
expect(typeof parser).toEqual('function');
setMarkdownConf({parser: 'gfm'});
parser = markdown.getParser();
expect(typeof parser).toEqual('function');
restoreMarkdownConf(storage);
});
xit('should use the evilstreak parser when requested', function() {
// TODO
it('should use the evilstreak parser when requested', function() {
var storage = setMarkdownConf({parser: 'evilstreak'});
var parser = markdown.getParser();
expect(parser._parser).toEqual('markdown');
restoreMarkdownConf(storage);
});
xit('should use the GFM parser when requested', function() {
// TODO
it('should use the GFM parser when requested', function() {
var storage = setMarkdownConf({parser: 'gfm'});
var parser = markdown.getParser();
expect(parser._parser).toEqual('github-flavored-markdown');
restoreMarkdownConf(storage);
});
xit('should convert GitHub repo references to links when the correct options are defined', function() {
// TODO
it('should use GFM parser when parser is not specified but github owner and repo are', function() {
var storage = setMarkdownConf({githubRepoOwner: 'jsdoc', githubRepoName: 'jsdoc3'});
var parser = markdown.getParser();
expect(parser._parser).toEqual('github-flavored-markdown');
restoreMarkdownConf(storage);
});
it('should convert GitHub repo references to links when the correct options are defined', function() {
var storage = setMarkdownConf({parser: 'gfm', githubRepoOwner: 'jsdoc', githubRepoName: 'jsdoc3'});
var parser = markdown.getParser();
expect(parser._parser).toEqual('github-flavored-markdown');
var sha = '228c940816b5f799a12f83f071a1c67cbb478f39';
var sha7 = sha.substr(0, 7);
// Auto-link sha1 if GitHub.nameWithOwner is defined
expect(parser(sha)).toEqual(
"<p><a href='http://github.com/jsdoc/jsdoc3/commit/" + sha + "'>" + sha7 + "</a></p>");
// ** GFM ** Auto-link user@sha1 if GitHub.nameWithOwner is defined
expect(parser('mathematicalcoffee@' + sha)).toEqual(
"<p><a href='http://github.com/mathematicalcoffee/jsdoc3/commit/" + sha + "'>mathematicalcoffee@" + sha7 + "</a></p>");
// ** GFM ** Auto-link user/repo@sha1
expect(parser('jsdoc/jsdoc3@' + sha)).toEqual(
"<p><a href='http://github.com/jsdoc/jsdoc3/commit/" + sha + "'>jsdoc/jsdoc3@" + sha7 + "</a></p>");
// ** GFM ** Auto-link #issue if GitHub.nameWithOwner is defined
expect(parser('Fixes #1')).toEqual(
"<p>Fixes <a href='http://github.com/jsdoc/jsdoc3/issues/#issue/1'>#1</a></p>");
// ** GFM ** Auto-link user#issue if GitHub.nameWithOwner is defined
expect(parser('mathematicalcoffee#12')).toEqual(
"<p><a href='http://github.com/mathematicalcoffee/jsdoc3/issues/#issue/12'>mathematicalcoffee#12</a></p>");
// ** GFM ** Auto-link user/repo#issue
expect(parser('jsdoc/jsdoc3#1')).toEqual(
"<p><a href='http://github.com/jsdoc/jsdoc3/issues/#issue/1'>jsdoc/jsdoc3#1</a></p>");
restoreMarkdownConf(storage);
});
it('should not apply formatting to inline tags when the evilstreak parser is enabled', function() {
// store the old configuration
var old = (env.conf.markdown ? env.conf.markdown.parser : undefined);
env.conf.markdown = {parser: 'evilstreak'};
var storage = setMarkdownConf({parser: 'evilstreak'});
// get the evilstreak parser and do the test
var parser = markdown.getParser();
expect(parser('{@link MyClass#_x} and {@link MyClass#_y}')).toEqual(
'<p>{@link MyClass#_x} and {@link MyClass#_y}</p>');
// restore the old value
if (old === undefined) {
env.conf.markdown.parser = old;
} else {
delete env.conf.markdown;
}
restoreMarkdownConf(storage);
});
it('should not apply formatting to inline tags when the GFM parser is enabled', function() {
// store the old configuration
var old = (env.conf.markdown ? env.conf.markdown.parser : undefined);
env.conf.markdown = {parser: 'gfm'};
var storage = setMarkdownConf({parser: 'gfm'});
// get the gfm parser and do the test
var parser = markdown.getParser();
expect(parser('{@link MyClass#_x} and {@link MyClass#_y}')).toEqual(
'<p>{@link MyClass#_x} and {@link MyClass#_y}</p>');
// restore the old value
if (old === undefined) {
env.conf.markdown.parser = old;
} else {
delete env.conf.markdown;
}
restoreMarkdownConf(storage);
});
it('GFM parser with no conf.markdown.hardwrap has it to false', function() {
var storage = setMarkdownConf({parser: 'gfm'});
var parser = markdown.getParser();
expect(parser('Testing\nhardwrap')).toEqual('<p>Testing\nhardwrap</p>');
restoreMarkdownConf(storage);
});
it('GFM parser respects conf.markdown.hardwrap=false', function() {
var storage = setMarkdownConf({parser: 'gfm', hardwrap: false});
var parser = markdown.getParser();
expect(parser('Testing\nhardwrap')).toEqual('<p>Testing\nhardwrap</p>');
restoreMarkdownConf(storage);
});
it('GFM parser respects conf.markdown.hardwrap=true', function() {
var storage = setMarkdownConf({parser: 'gfm', hardwrap: true});
var parser = markdown.getParser();
expect(parser('Testing\nhardwrap')).toEqual('<p>Testing<br />hardwrap</p>');
restoreMarkdownConf(storage);
});
});
});