feat(getFlavor): add getFlavor method to showdown and Converter

With this new method, you can check what type of base flavor showdown is currently set
to run as.
This commit is contained in:
Estevao Soares dos Santos 2017-01-08 19:09:12 +00:00
parent a58674e597
commit 0eaf1050c7
9 changed files with 107 additions and 26 deletions

View File

@ -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

BIN
dist/showdown.js vendored

Binary file not shown.

BIN
dist/showdown.js.map vendored

Binary file not shown.

BIN
dist/showdown.min.js vendored

Binary file not shown.

Binary file not shown.

View File

@ -38,7 +38,12 @@ showdown.Converter = function (converterOptions) {
* @private
* @type {{}}
*/
listeners = {};
listeners = {},
/**
* The flavor set in this converter
*/
setConvFlavor = setFlavor;
_constructor();
@ -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

View File

@ -7,6 +7,7 @@ var showdown = {},
parsers = {},
extensions = {},
globalOptions = getDefaultOpts(true),
setFlavor = 'vanilla',
flavor = {
github: {
omitExtraWLInCodeBlocks: true,
@ -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

View File

@ -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');
@ -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',

View File

@ -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');
});
});