Skip to content

Commit

Permalink
feat(getFlavor): add getFlavor method to showdown and Converter
Browse files Browse the repository at this point in the history
With this new method, you can check what type of base flavor showdown is currently set
to run as.
  • Loading branch information
tivie committed Jan 8, 2017
1 parent a58674e commit 0eaf105
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 44 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,10 @@ var defaultOptions = showdown.getDefaultOptions();
* **requireSpaceBeforeHeadingText**: (boolean) [default false] Makes adding a space between `#` and the header text mandatory (since v1.5.3)
* **ghMentions**: (boolean) [default false] Enables github @mentions, which link to the username mentioned (since v1.5.6)
* **ghMentions**: (boolean) [default false] Enables github @mentions, which link to the username mentioned (since v1.6.0)
**NOTE**: Please note that until version 1.6.0, all of these options are ***DISABLED*** by default in the cli tool.
## Flavors
Expand Down
67 changes: 53 additions & 14 deletions dist/showdown.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/showdown.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/showdown.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js.map

Large diffs are not rendered by default.

29 changes: 22 additions & 7 deletions src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ showdown.Converter = function (converterOptions) {
* @private
* @type {{}}
*/
listeners = {};
listeners = {},

/**
* The flavor set in this converter
*/
setConvFlavor = setFlavor;

_constructor();

Expand Down Expand Up @@ -362,16 +367,26 @@ showdown.Converter = function (converterOptions) {
* @param {string} name
*/
this.setFlavor = function (name) {
if (flavor.hasOwnProperty(name)) {
var preset = flavor[name];
for (var option in preset) {
if (preset.hasOwnProperty(option)) {
options[option] = preset[option];
}
if (!flavor.hasOwnProperty(name)) {
throw Error(name + ' flavor was not found');
}
var preset = flavor[name];
setConvFlavor = name;
for (var option in preset) {
if (preset.hasOwnProperty(option)) {
options[option] = preset[option];
}
}
};

/**
* Get the currently set flavor of this converter
* @returns {string}
*/
this.getFlavor = function () {
return setConvFlavor;
};

/**
* Remove an extension from THIS converter.
* Note: This is a costly operation. It's better to initialize a new converter
Expand Down
36 changes: 30 additions & 6 deletions src/showdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var showdown = {},
parsers = {},
extensions = {},
globalOptions = getDefaultOpts(true),
setFlavor = 'vanilla',
flavor = {
github: {
omitExtraWLInCodeBlocks: true,
Expand Down Expand Up @@ -90,16 +91,39 @@ showdown.resetOptions = function () {
*/
showdown.setFlavor = function (name) {
'use strict';
if (flavor.hasOwnProperty(name)) {
var preset = flavor[name];
for (var option in preset) {
if (preset.hasOwnProperty(option)) {
globalOptions[option] = preset[option];
}
if (!flavor.hasOwnProperty(name)) {
throw Error(name + ' flavor was not found');
}
var preset = flavor[name];
setFlavor = name;
for (var option in preset) {
if (preset.hasOwnProperty(option)) {
globalOptions[option] = preset[option];
}
}
};

/**
* Get the currently set flavor
* @returns {string}
*/
showdown.getFlavor = function () {
'use strict';
return setFlavor;
};

/**
* Get the options of a specified flavor. Returns undefined if the flavor was not found
* @param {string} name Name of the flavor
* @returns {{}|undefined}
*/
showdown.getFlavorOptions = function (name) {
'use strict';
if (flavor.hasOwnProperty(name)) {
return flavor[name];
}
};

/**
* Get the default options
* @static
Expand Down
41 changes: 29 additions & 12 deletions test/node/showdown.Converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,14 @@ describe('showdown.Converter', function () {
});
});

describe('setFlavor method', function () {
describe('converter.setFlavor()', function () {

/**
* Test setFlavor('github')
*/
describe('github', function () {
var converter = new showdown.Converter(),
ghOpts = {
omitExtraWLInCodeBlocks: true,
prefixHeaderId: 'user-content-',
simplifiedAutoLink: true,
literalMidWordUnderscores: true,
strikethrough: true,
tables: true,
tablesHeaderId: true,
ghCodeBlocks: true,
tasklists: true
};
ghOpts = showdown.getFlavorOptions('github');

converter.setFlavor('github');

Expand All @@ -61,6 +51,33 @@ describe('showdown.Converter', function () {
});
});

describe('getFlavor method', function () {

// reset showdown
showdown.setFlavor('vanilla');

describe('flavor', function () {
it('should be vanilla by default', function () {
var converter = new showdown.Converter();
converter.getFlavor().should.equal('vanilla');
});

it('should be changed if global option is changed', function () {
showdown.setFlavor('github');
var converter = new showdown.Converter();
converter.getFlavor().should.equal('github');
showdown.setFlavor('vanilla');
});

it('should not be changed if converter is initialized before global change', function () {
var converter = new showdown.Converter();
showdown.setFlavor('github');
converter.getFlavor().should.equal('vanilla');
showdown.setFlavor('vanilla');
});
});
});

describe('extension methods', function () {
var extObjMock = {
type: 'lang',
Expand Down
22 changes: 22 additions & 0 deletions test/node/showdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,25 @@ describe('showdown.getAllExtensions()', function () {
showdown.getAllExtensions().should.eql({bar: [extObjMock]});
});
});

describe('showdown.setFlavor()', function () {
'use strict';
it('should set flavor to github', function () {
showdown.setFlavor('github');
showdown.getFlavor().should.equal('github');
showdown.setFlavor('vanilla');
});

it('should set options correctly', function () {
showdown.setFlavor('github');
var ghOpts = showdown.getFlavorOptions('github'),
shOpts = showdown.getOptions();
for (var opt in ghOpts) {
if (ghOpts.hasOwnProperty(opt)) {
shOpts.should.have.property(opt);
shOpts[opt].should.equal(ghOpts[opt]);
}
}
showdown.setFlavor('vanilla');
});
});

0 comments on commit 0eaf105

Please sign in to comment.